【问题标题】:How to Convert a Transition Drawable to Drawable or to Bitmap如何将转换 Drawable 转换为 Drawable 或位图
【发布时间】:2015-02-05 14:22:32
【问题描述】:

我正在寻找如何将 TransitionDrawable 转换为 Drawable 或 To bitmap 以便能够保存图像的方法。

找了好久,试了很多方法都没有头绪。

这是我尝试过的代码:

TransitionDrawable drawable = (TransitionDrawable) profil.getDrawable();
                        Drawable drawalb =  drawable.mutate();
                        final Bitmap bitmap = ((BitmapDrawable) drawalb).getBitmap();
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byte[] image = stream.toByteArray();

【问题讨论】:

    标签: android bitmap android-drawable


    【解决方案1】:

    我不想这样做,但我找到了另一个线索,我能够在将位图转换为 TransitionDrawable 之前直接获取它。

    【讨论】:

      【解决方案2】:

      transitionDrawable 是一个可绘制数组 (Defnition)。

      所以;

      1.根据索引获取你想要的drawable

      2.将其转换为位图(As cited here byAndre

       TransitionDrawable Tdrawable =(TransitionDrawable) imageView.getDrawable();
       Drawable mDrawable =  Tdrawable.getDrawable;
       bitmap = drawableToBitmap(mDrawable);
      
      public static Bitmap drawableToBitmap (Drawable drawable) {
          Bitmap bitmap = null;
      
          if (drawable instanceof BitmapDrawable) {
              BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
              if(bitmapDrawable.getBitmap() != null) {
                  return bitmapDrawable.getBitmap();
              }
          }
      
          if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
              bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
          } else {
              bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
          }
      
          Canvas canvas = new Canvas(bitmap);
          drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
          drawable.draw(canvas);
          return bitmap;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-03
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 2012-07-06
        相关资源
        最近更新 更多