帧动画

来源:互联网 发布:知乎 显微镜原理 编辑:程序博客网 时间:2024/06/14 11:08
public class MainActivity extends BaseActivity implements OnClickListener {
    private static final int FLAG = 123;
    private ImageView imgShow;
    private TextView txtTime;
    private Button btnStart;
    private Button btnStop;
    private AnimationDrawable animation;
    private Timer timer;
    /**
     * 初始化视图相关控件
     */
    @Override
    protected void initView() {
        setContentView(R.layout.activity_main);
        imgShow = (ImageView) findViewById(R.id.img_show);
        txtTime = (TextView) findViewById(R.id.txt_time);
        // imgShow.setImageResource(R.drawable.ic_launcher);
        btnStart = (Button) findViewById(R.id.btn_start_anim);
        btnStop = (Button) findViewById(R.id.btn_stop_anim);
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData() {
        // xml资源文件的形式实现动画效果
        // animation = (AnimationDrawable)
        // getResources().getDrawable(R.drawable.anim_frame);
        // 代码方式实现帧动画效果
        initAnimation();
        timer = new Timer();
    }
    /**
     * 设置监听器
     */
    @Override
    public void setListeners() {
        super.setListeners();
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
    }  
    /**
     * 其他操作
     */
    @Override
    public void setMoreAction() {
        super.setMoreAction();
        imgShow.setImageDrawable(animation);
    }
    /**
     * 初始化动画,添加每一帧
     */
    private void initAnimation() {
        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.img0), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img1), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img2), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img3), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img4), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img5), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img6), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img7), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img8), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img9), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img10), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img11), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img12), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img13), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img14), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img15), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img16), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img17), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img18), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img19), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img20), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img21), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img22), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img23), 50);
        animation.addFrame(getResources().getDrawable(R.drawable.img24), 50);
        // 设置单次播放
        animation.setOneShot(true);
    }
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case FLAG:
                int total = (Integer) msg.obj;
                if (total < 0) {
                    return;
                }
                txtTime.setText("倒计时:" + total + "秒");
                if (total == 0) {
                    animation.start();
                }
                break;

            default:
                break;
            }
        };
    };
    TimerTask task = new TimerTask() {
        int total = 10;
        @Override
        public void run() {
            total--;
            Message msg = Message.obtain();
            msg.what = FLAG;
            msg.obj = total;
            handler.sendMessage(msg);
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_start_anim:
            timer.schedule(task, 0, 1000);
            // animation.start();
            break;
        case R.id.btn_stop_anim:
            if (animation.isRunning()) {
                animation.stop();
            }
            break;
        default:
            break;
        }
    }

}

<TextView
    android:id="@+id/txt_time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="倒计时:10秒"/>  
    <ImageView
        android:id="@+id/img_show"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <Button
        android:id="@+id/btn_start_anim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="开始动画" /> 
    <Button
        android:id="@+id/btn_stop_anim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="结束动画" />



原创粉丝点击