搜索
您的当前位置:首页正文

ImagView播放动画的几种方式

来源:二三娱乐

title: ImagView播放动画的几种方式
date: 2017-01-19 10:33:27
tags: tips


  • 比如这个,总共77帧。下面对比三种方案

    • 方案一:

      像这样把每一帧图片放进animation-list

      <animation-list 
          android:oneshot="false">
        <item android:drawable="@drawable/shenlie76" android:duration="83"/>
        <item android:drawable="@drawable/shenlie77" android:duration="83"/>
        <item android:drawable="@drawable/shenlie01" android:duration="83"/>
        .....
        </animation-list>
      

      然后

      mIv.setImageResource(R.drawable.animation);
      Animatable drawable = (Animatable) mIv.getDrawable();
      drawable.start();
      

      当然是可行的,效果嘛。。看看内存吧

      一开始动画,内存占用从原来的18M飚到180M,太可怕了。我们知道初始一般会分配给一个app 十几M的内存,最多能拿200M左右,超过就OOM了,方案一唾弃之。

    • 方案二

      读取资源,使用bitmap

      CountDownTimer countDownTimer = new CountDownTimer(300000, 83){
           @Override
           public void onTick(long millisUntilFinished) {
               if(count==imgRes.length) count = 0; //播放到最后一帧从头播放
               Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgRes[count++]);
               bitmapCache.add(new SoftReference<>(bitmap));
               mIv.setImageBitmap(bitmap);  
           }
           @Override
           public void onFinish() {}
      };
      countDownTimer.start();
      

      还行,才占用21M,没播放时占18M,也就占了3M,一帧的bitmap对象占了3M,用完就释放掉了,不过这个锯齿也是不怎么爽的。

    • 方案三

      重用bitmap内存,避免gc频繁

      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inMutable=true;
      CountDownTimer countDownTimer = new CountDownTimer(300000, 83){
         @Override
         public void onTick(long millisUntilFinished) {
            if(count==imgRes.length) count = 0;
            if (!bitmapCache.isEmpty()) {
                Iterator<SoftReference<Bitmap>> iterator = bitmapCache.iterator();
                if (iterator.hasNext()) {
                     Bitmap bitmapSoftReference = iterator.next().get();
                     Bitmap bitmap =  BitmapFactory.decodeResource(getResources(),imgRes[count++],options);
                     options.inBitmap = bitmapSoftReference;
                     mIv.setImageBitmap(bitmap);
                 }
             }else {
                 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgRes[count++], options);
                 bitmapCache.add(new SoftReference<>(bitmap));
                 mIv.setImageBitmap(bitmap);
              }
         }
         @Override
         public void onFinish() {}
      };
      countDownTimer.start();
      

      完美。

Top