Android的图形与图像处理之三 逐帧动画(Frame)

来源:互联网 发布:淘宝特惠囤 编辑:程序博客网 时间:2024/06/15 18:54
定义逐帧动画非常简单,只要在<animation-list …/>元素中使用<item.../>子元素定义动画的全部帧,并指定各帧的持续时间即可。
android:onshot控制该动画是否循环播放,设为false则会循环播放
也支持在Java代码中创建逐帧动画,调用addFrame(Drawable frame, int duration)向该动画中添加帧
程序获取AnimationDrawable后,接下来就可用ImageView把AnimationDrawable显示出来
示例代码:
res/anim/fat_po.xml
<?xmlversion="1.0"encoding="utf-8"?>
<!-- 指定动画循环播放 -->
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!-- 添加多个帧 -->
<itemandroid:drawable="@drawable/fat_po_f01"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f02"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f03"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f04"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f05"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f06"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f07"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f08"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f09"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f10"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f11"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f12"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f13"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f14"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f15"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f16"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f17"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f18"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f19"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f20"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f21"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f22"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f23"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f24"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f25"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f26"android:duration="60"/>
<itemandroid:drawable="@drawable/fat_po_f27"android:duration="60"/>
</animation-list>
main_activity.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical">
   
<LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:orientation="horizontal"
       
android:gravity="center">
       
<Buttonandroid:id="@+id/play"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           android:text="start"
           
/>
       
<Buttonandroid:id="@+id/stop"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="stop"
           
/>
       
   
</LinearLayout>

   
<ImageView
       
android:id="@+id/anim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@anim/fat_po"
android:scaleType="center"
android:layout_gravity="center_horizontal"
/>
       
   
</LinearLayout>
main.java

publicclassMainActivity extends Activity
{
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
// 获取两个按钮
Button play = (Button) findViewById(R.id.
play);
Button stop = (Button) findViewById(R.id.
stop);
ImageView imageView = (ImageView) findViewById(R.id.
anim);
// 获取AnimationDrawable动画对象
finalAnimationDrawable anim = (AnimationDrawable) imageView
.getBackground();
play.setOnClickListener(
newOnClickListener()
{
@Override
publicvoidonClick(View v)
{
// 开始播放动画
anim.start();
}
});
stop.setOnClickListener(
newOnClickListener()
{
@Override
publicvoidonClick(View v)
{
// 停止播放动画
anim.stop();
}
});
}
}
0 0
原创粉丝点击