关闭

安卓逐帧动画

标签: drawableandroidanimation
188人阅读 评论(0) 收藏 举报
分类:

通常有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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:948次
    • 积分:61
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条