1

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

图片来自 URL。如果我将 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 不支持自定义的 Image View?有没有人遇到过这样的问题,如果有,是怎么解决的?

谢谢!

4

1 回答 1

6

ImageView我个人使用 Ion的回调来设置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);
            }
    });

这种通用结构允许您以Bitmap任何您想要的方式工作。

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

final RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(Math.min(roundedBitmapDrawable.getMinimumWidth(), roundedBitmapDrawable.getMinimumHeight()) / 2.0F);
imageView.setImageDrawable(roundedBitmapDrawable);
于 2014-11-03T09:12:09.887 回答