IonDrawable无法强制转换为android.graphics.drawable.BitmapDrawable

时间:2014-09-28 02:08:24

标签: android image imageview classcastexception ion

尝试使用RoundedDrawable类(从https://github.com/vinc3m1/RoundedImageView获取)将图像插入到圆角图像中。

图片来自网址。如果我使用Koushik dutta的ION库来处理图像,我会收到以下错误:

 09-27 21:35:06.187: E/AndroidRuntime(12606): FATAL EXCEPTION: main
 09-27 21:35:06.187: E/AndroidRuntime(12606): Process: com.ylg.maps, PID: 12606
 09-27 21:35:06.187: E/AndroidRuntime(12606): java.lang.ClassCastException: com.koushikdutta.ion.IonDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.otherlibs.RoudImageViewForMap.loadBitmap(RoudImageViewForMap.java:70)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.otherslibs.RoudImageViewForMap.onDraw(RoudImageViewForMap.java:80)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.view.View.draw(View.java:14465)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.view.View.draw(View.java:14350)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.view.View.buildDrawingCache(View.java:13673)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.view.View.buildDrawingCache(View.java:13522)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.maps.Mapsthree.createDrawableFromView(Mapsthree.java:454)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.maps.Mapsthree.loadFriends(Mapsthree.java:417)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.maps.Mapsthree.access$24(Mapsthree.java:402)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.maps.Mapsthree$LoadUsers.onPostExecute(Mapsthree.java:660)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.ylg.maps.Mapsthree$LoadUsers.onPostExecute(Mapsthree.java:1)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.os.AsyncTask.finish(AsyncTask.java:632)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.os.AsyncTask.access$600(AsyncTask.java:177)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.os.Handler.dispatchMessage(Handler.java:102)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.os.Looper.loop(Looper.java:136)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at android.app.ActivityThread.main(ActivityThread.java:5001)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at java.lang.reflect.Method.invoke(Native Method)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
 09-27 21:35:06.187: E/AndroidRuntime(12606):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

看起来ION库支持ImageView不支持自定义图像视图?有没有人有这样的问题,如果是这样,它是如何解决的?

谢谢!

1 个答案:

答案 0 :(得分:6)

我个人使用Ion的回调来自己设置ImageView的{​​{1}},然后将其设为循环(或其他):

Bitmap

这种通用结构允许您以任何方式使用final ImageView imageView = (ImageView) ... Ion.with(context) .load(url) .asBitmap() .setCallback(new FutureCallback<Bitmap>() { @Override public void onCompleted(final Exception e, final Bitmap bitmap) { // check for (e != null) final Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); final BitmapShader shader = new BitmapShader (bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); final Paint paint = new Paint(); paint.setShader(shader); final Canvas canvas = new Canvas(circleBitmap); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint); // maybe bitmap.recycle() here? imageView.setImageBitmap(circularBitmap); } });

编辑:如果您使用支持资源库v4,则应改为使用RoundedBitmapDrawable

Bitmap