自定义View的开发学习(2)帧动画

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 自定义View的开发学习(2)帧动画,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

源代码地址github:https://github.com/307572384/vietest
csdn地址:https://download.csdn.net/download/qq_16519957/10930840

1.实现逐帧动画方式有2种

A.xml资源文件方式

B.代码方式
我就只说第一种方式。第二种方式可以参考这篇文章Android 逐帧动画:关于 逐帧动画 的使用都在这里了!
在这里插入图片描述
主要就是实现点击开始就开始播放,点击停止就停止播放,图片有点诡异将就看一下。然后图片我是直接在那个gif切割器的网站上copy的。
推荐使用gif的切割器:GifSplitter V2.0 中文版
http://www.pc6.com/softview/SoftView_52841.html
还有文件批量命名的工具:批量重命名软件 1.0 免费版
http://www.onlinedown.net/soft/116027.htm
如果日后这两个网站出现404找不到的话就自己搜一下吧。

首先我们先写一个view_zhuzhen.xml

这里存放图片也就是每一帧的图片位置

注意一下这里的这个xml文件如果放在drawable(v24)有这个v24后缀的话可能会找不到文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <!--// item = 动画图片资源;duration = 设置一帧持续时间(ms)-->
    <item
        android:drawable="@drawable/c0"
        android:duration="100" />
    <item
        android:drawable="@drawable/c1"
        android:duration="100" />
    <iitem
        android:drawable="@drawable/c2"
        android:duration="100" />
    <item
        android:drawable="@drawable/c3"
        android:duration="100" />
    <item
        android:drawable="@drawable/c4"
        android:duration="100" />
    <item
        android:drawable="@drawable/c5"
        android:duration="100" />
    <item
        android:drawable="@drawable/c6"
        android:duration="100" />
    <item
        android:drawable="@drawable/c7"
        android:duration="100" />
    <item
        android:drawable="@drawable/c8"
        android:duration="100" />
    <item
        android:drawable="@drawable/c9"
        android:duration="100" />
    <item
        android:drawable="@drawable/c10"
        android:duration="100" />
    <item
        android:drawable="@drawable/c11"
        android:duration="100" />
    <item
        android:drawable="@drawable/c12"
        android:duration="100" />
    <item
        android:drawable="@drawable/c13"
        android:duration="100" />
    <item
        android:drawable="@drawable/c14"
        android:duration="100" />
    <item
        android:drawable="@drawable/c15"
        android:duration="100" />
    <item
        android:drawable="@drawable/c16"
        android:duration="100" />
    <item
        android:drawable="@drawable/c17"
        android:duration="100" />
    <item
        android:drawable="@drawable/c18"
        android:duration="100" />
</animation-list>

然后我们看一下我们的布局:zzlayout.xml

非常简单就一个imageview和两个button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/zz_img"
            android:layout_width="150dp"
            android:layout_height="150dp"
         />
    </LinearLayout>

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="开始" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止" />
</LinearLayout>

然后我们再来看看ZZActivity

//逐帧动画
public class ZZActivity extends AppCompatActivity  {
	private ImageView zz_iv;
	private Button    zz_start;
	private Button    zz_stop;
	private AnimationDrawable mAnimationDrawable;
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zzlayout);
		initView();

	}

	private void initView() {
		zz_iv = (ImageView) findViewById(R.id.zz_img);
		zz_start = (Button) findViewById(R.id.start);
		zz_stop = (Button) findViewById(R.id.stop);

		zz_start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				zz_iv.setImageResource(R.drawable.view_zhuzhen);
				//这里引入xml也就是存放图片的一个列表
				mAnimationDrawable=(AnimationDrawable) zz_iv.getDrawable();
				//获取动画对象
				mAnimationDrawable.start();
				//开始动画
			}
		});
		zz_stop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				zz_iv.setImageResource(R.drawable.view_zhuzhen);
				mAnimationDrawable=(AnimationDrawable)zz_iv.getDrawable();
				mAnimationDrawable.stop();
				//暂停动画
			}
		});
	}


}

然后我们就可以看到一个帧动画了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/134151.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!