小菜鸡

温故而知新,可以为师矣。

安卓逐帧动画

通常有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>
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xwmj987/article/details/46785921
个人分类: 安卓基础
想对作者说点什么? 我来说一句

Android下逐帧动画的Demo

2013年12月03日 5.14MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭