解决AnimationDrawable的OOM

利用AnimationDrawable播放大张而且10几张以上大图,会容易造成OOM。加载速度慢,占用大量内存。

内存与位图关系:

RGB8888 每一个像素占4个字节

RGB565 每一个像素占2个字节

如果一张png图片像素为1920*1080,内存占用:1920*1080*4/1024/1024=7.9M,加载一张图片就有可能OOM了,更不用说几十张了

推荐使用一下方式加载帧动画:

public class AnimView extends View {

    private int temp = -1;

    public void setmList(List<Integer> mList) {
        if (mList==null){
            throw  new NullPointerException("onResume");
        }
        this.mList = mList;
    }

    private List<Integer> mList = new ArrayList<>();

    public AnimView(Context context) {
        this(context, null);
    }

    public AnimView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    private ObjectAnimator animator = new ObjectAnimator();

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        animator.setTarget(this);
         //设置属性名称,通过反射调用
        animator.setPropertyName("animationFrame");
        //从第1张(索引为0)到20张
        animator.setIntValues(0, 19);
        //需要多少时间完成
        animator.setDuration(6000);
        //重新开始(RESTART),避免索引越界
        animator.setRepeatMode(ValueAnimator.RESTART);
        //无限循环
        animator.setRepeatCount(ValueAnimator.INFINITE);
    }
    //开始播放动画
    public void start() {
        animator.start();
    }
    //结束播放动画
    public void stop() {
        animator.cancel();
    }
    //frame的值范围是0-20(包头不包尾)
    private void setAnimationFrame(int frame) {

    //当前帧数和上一帧一样时,不处理,避免重复加载图片
        if (frame == temp) {
            return;
        }
        Log.i("zhanlv", "frame: " + frame);
        temp = frame;

    //使用Glide加载图片

        Glide.with(getContext()).load(mList.get(frame)).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                setBackground(resource);
            }
        });
    }
}
       //查找Aniview的控件id
        animView = findViewById(R.id.ani);
        asList = Arrays.asList(
                R.drawable.scene_seaworld1
                , R.drawable.scene_seaworld2
                , R.drawable.scene_seaworld3
                , R.drawable.scene_seaworld4
                , R.drawable.scene_seaworld5
                , R.drawable.scene_seaworld6
                , R.drawable.scene_seaworld7
                , R.drawable.scene_seaworld8
                , R.drawable.scene_seaworld9
                , R.drawable.scene_seaworld10
                , R.drawable.scene_seaworld11
                , R.drawable.scene_seaworld12
                , R.drawable.scene_seaworld13
                , R.drawable.scene_seaworld14
                , R.drawable.scene_seaworld15
                , R.drawable.scene_seaworld16
                , R.drawable.scene_seaworld17
                , R.drawable.scene_seaworld18
                , R.drawable.scene_seaworld19
                , R.drawable.scene_seaworld20
        );
        //设置图片数据
        animView.setmList(asList);
        animView.start();

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AnimationDrawable是一个用于实现动画的类。它允许您将多张图片以特定的顺序显示在屏幕上,从而创建连续的动画效果。 要使用AnimationDrawable,首先需要在XML或Java代码中定义一个AnimationDrawable对象,并将每一图片添加到其中。然后,您可以通过调用start()方法启动动画,或者通过调用stop()方法停止动画。 以下是一个使用AnimationDrawable创建动画的示例: 1. 在XML文件中定义AnimationDrawable对象和每一图片: ```xml <animation-list android:id="@+id/animation" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="100"/> <item android:drawable="@drawable/frame2" android:duration="100"/> <item android:drawable="@drawable/frame3" android:duration="100"/> <!-- 添加更多的图片 --> </animation-list> ``` 2. 在Java代码中获取AnimationDrawable对象,并将其应用到ImageView上: ```java ImageView imageView = findViewById(R.id.imageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); ``` 3. 启动动画: ```java animationDrawable.start(); ``` 这样,您就可以在ImageView上显示动画了。每一图片将按照指定的持续时间进行切换,从而呈现出动画效果。 请注意,AnimationDrawable可以通过调用stop()方法停止动画。此外,您还可以设置动画是否循环播放,通过将android:oneshot属性设置为true或false来实现。 希望这个例子能够帮助您理解如何使用AnimationDrawable创建动画!如果有任何问题,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值