【问题标题】:How to convert the LayerDrawable to Drawable in Android?如何在 Android 中将 LayerDrawable 转换为 Drawable?
【发布时间】:2013-03-26 03:56:54
【问题描述】:

在我的应用程序中,我使用 LayerDrawable 来显示覆盖层图像。我这样做是成功使用 layerDrawable。

在我将 layerDrawable 设置为 imageView.setImageDrawable(layerDrawable);

现在我想使用这个可绘制的图像作为位图并在下一个图像处理中使用。 但是当我尝试让 Bitmap 使用它时

((BitmapDrawable)imageLayout.getDrawable()).getBitmap();

我收到以下错误。

04-04 12:56:02.102: E/AndroidRuntime(15127): java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

所以我改变了我的图像处理的后续,并尝试将 LayerDrawable 转换为 Drawable 并将这个 drawable 设置为 imageLayout 背景。 然后它完美地工作。 我的问题是如何将 LayerDrawable 转换为 drwable?

请任何人帮忙。 给我一些想法。 谢谢。

【问题讨论】:

    标签: bitmap drawable


    【解决方案1】:

    只是一个测试,我没试过这个

    public Drawable geSingleDrawable(LayerDrawable layerDrawable){
    
              int resourceBitmapHeight = 136, resourceBitmapWidth = 153;
    
              float widthInInches = 0.9f;
    
              int widthInPixels = (int)(widthInInches * getResources().getDisplayMetrics().densityDpi);
              int heightInPixels = (int)(widthInPixels * resourceBitmapHeight / resourceBitmapWidth);
    
              int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;
    
              layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);     
    
              Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);
    
              Canvas canvas = new Canvas(bitmap);
              layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
              layerDrawable.draw(canvas);
    
              BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
              bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
    
              return bitmapDrawable;
    }
    

    【讨论】:

    • @Lester 如果您发现此答案不适用于您的案例,因此要为您获得解决方案,请简要说明您的案例和代码,或者最好的方法是开始您自己的问题。!投反对票不会让我知道您的案例/问题。
    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多