您的位置:首页 > 移动开发 > Android开发

android ImageView 播放动画

2017-10-11 10:41 1156 查看
最近要做一个loading的动画,刚开始使用gif图做,但是gif图效果不好,播放速度太慢,后来通过ImageView一帧一帧的播放。

首先上几张图:











Animation-List

Animation-list是帧动画的默认选择,我们在drawable文件夹下创建一个my_animation_list.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a0" android:duration="100"/>
<item android:drawable="@drawable/a1" android:duration="100"/>
<item android:drawable="@drawable/a2" android:duration="100"/>
<item android:drawable="@drawable/a3" android:duration="100"/>
<item android:drawable="@drawable/a4" android:duration="100"/>

</animation-list>
android:oneshot="false"表示循环播放,如果为true就只会拨一次。

然后在布局文件中添加一个ImageView,ImageView背景设为my_animation_list

<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/animation_list_filling"
/>

播放

播放很简单,直接上代码:

ImageView imageView= (ImageView) findViewById(R.id.image_view);
((AnimationDrawable) imageView.getBackground()).start();


属性动画:

帧动画很简单,但是帧动画有个问题就是帧动画如果放的图片过多,容易出现OOM,所以为了保险起见,我们使用属性动画来实现。

首先我们创建Level_list

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a0" android:maxLevel="1"/>
<item android:drawable="@drawable/a1" android:maxLevel="2"/>
<item android:drawable="@drawable/a2" android:maxLevel="3"/>
<item android:drawable="@drawable/a3" android:maxLevel="4"/>
<item android:drawable="@drawable/a4" android:maxLevel="5"/>
</level-list>然后我们自己将ImageView的Level包裹一下,形成Set  Get属性
public class LoadingImageView extends ImageView {
private int imageLevel = 0;
private int maxLevel = 5;
public LoadingImageView(Context context) {
super(context);
}

public LoadingImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public LoadingImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public void setImageLevel(int level) {
if (this.imageLevel == level)
return;
super.setImageLevel(level);
this.imageLevel = level;
}

public int getImageLevel() {
return imageLevel;
}

public void nextLevel() {
setImageLevel(imageLevel++ % maxLevel);
}

public void setMaxLevel(int maxLevel) {
this.maxLevel = maxLevel;
}

}将自定义的ImageView放入布局文件
<com.android.test.main.view.LoadingImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/lark_loading_iv"
android:layout_gravity="center"
android:layout_marginTop="136dp"/>

自己开始写属性动画
LoadingImageView imageView2= (LoadingImageView) findViewById(R.id.level_image);
imageView2.setImageResource(R.drawable.level_lists);
ObjectAnimator headerAnimator= ObjectAnimator.ofInt(imageView2,"imageLevel",1,5);
headerAnimator.setRepeatCount(ObjectAnimator.INFINITE);
headerAnimator.setInterpolator(new LinearInterpolator());
headerAnimator.setRepeatMode(ObjectAnimator.RESTART);
headerAnimator.setDuration(1000);
headerAnimator.start();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息