ImageView使用属性动画解决OOM问题

Home / Android @百晓生 2017-10-14 5457

   Animation-list是帧动画的默认选择,使用起来也非常方便。但是有一点要注意,就是图层过多的话,会出现OOM异常!为了解决这个问题最好使用属性动画来实现。

level-list的简单用法。 

1》新建xml文件 配置图片与ID对应 

2》在布局文件中ImageView 的src设置成level_list的文件名 

3》代码中使用setImagelevel(int imgId) 或者imageview.getDrawable.setLevel(int imgId);

   首先创建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控件,代码如下:

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;
    }
}

   使用方法:

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();

以下图像样本

本文链接:https://www.it72.com/12264.htm

推荐阅读
最新回复 (0)
返回