如何将LayerDrawable在Android中,以可绘制的转换? [英] How to convert the LayerDrawable to Drawable in Android?

查看:746
本文介绍了如何将LayerDrawable在Android中,以可绘制的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我使用 LayerDrawable ,以显示覆盖层image.and我这样做是成功使用layerDrawable的。

In my app i use the LayerDrawable to display overlayer image.and i do this is successfully use of layerDrawable.

后,我得到layerDrawable设置为imageView.setImageDrawable(layerDrawable);

after i get layerDrawable set as a imageView.setImageDrawable(layerDrawable);

现在我想用这个图像绘制取为位图,并在接下来的图像处理使用。
但是当我试图让位图使用这种

Now i want to use this image drawable take as Bitmap and use in next imageprocessing. but when i try to get the Bitmap use of this

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

我得到了下面的错误。

i got the following Error.

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 可绘制并设置该绘制为imageLayout背景。
那么它的正常工作。
我的问题是如何将LayerDrawable转换为drwable?

so i change the follow of my image processing and try to convert the LayerDrawable to Drawable and set this drawable as imageLayout backGround. then it's work perfectly. My problem is how to convert the LayerDrawable to drwable?

请任何一个帮助。
给我一些想法。
谢谢你。

please any one help. give me some idea. Thanks.

推荐答案

只是一个测试,我还没有试过这种

Just a test, I haven't tried this

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

这篇关于如何将LayerDrawable在Android中,以可绘制的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆