安卓逐帧动画

通常有2种实现方式,代码或者XML

(1)、XML结合java代码 方式实现
在res目录下新建anim文件夹,右键选这里写图片描述`

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false" >
    <item android:drawable="@drawable/horn0" android:duration="500" />
    <item android:drawable="@drawable/horn1" android:duration="500" />
    <item android:drawable="@drawable/horn2" android:duration="500" />
    <item android:drawable="@drawable/horn3" android:duration="500" />
    <item android:drawable="@drawable/horn4" android:duration="500" />
</animation-list>

然后在代码中设置

    private void playImgAnim(ImageView imageView) {
        animationDrawable = new AnimationDrawable(); 
        imageView.setBackgroundResource(R.anim.frame_animation);
        animationDrawable = (AnimationDrawable) imageView.getBackground();
        if(!animationDrawable.isRunning())animationDrawable.start();
    }
@Override
public void onStop() {
    super.onStop();
    if (animationDrawable.isRunning()) 
 animationDrawable.stop();
}

(2)全代码实现

public class MainActivity extends Activity {

    private AnimationDrawable mAnimationDrawable;
    private Drawable mDrawable;
    private ImageView img;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAnimationDrawable = new AnimationDrawable();
        img = (ImageView) findViewById(R.id.imageView1);
    }



    public void click(View view)
    {
        switch (view.getId()) {
        case R.id.btn1:
            start();
            break;
        case R.id.btn2:
            stop();
            break;


        }
    }



    private void stop() {
      if(mAnimationDrawable.isRunning())
      {
          mAnimationDrawable.stop();
      }
    }



    private void start() {

        for (int i = 0; i < 5; i++) {
            //第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名
            int resourcesId = getResources().getIdentifier("horn" + i,
                    "drawable", this.getPackageName());

            mDrawable = getResources().getDrawable(resourcesId);
            mAnimationDrawable.addFrame(mDrawable, 500);
        }
        mAnimationDrawable.setOneShot(false);
        img.setBackgroundDrawable(mAnimationDrawable);
        if(!mAnimationDrawable.isRunning()){
         mAnimationDrawable.start();
        }
    }
}

//xml布局

<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />


     <Button 
         android:id="@+id/btn1"
         android:onClick="click"
         android:text="start"
         android:layout_alignParentBottom="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
     <Button 
        android:id="@+id/btn2"
         android:onClick="click"
         android:text="stop"
         android:layout_alignParentRight="true"
         android:layout_alignParentBottom="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值