Search code examples
androidandroid-bitmapandroid-ion

Circle ImageView using Ion


iam using Ion https://github.com/koush/ion

to load images in my app

i need some of the images to be circled using Transform

but i get some CRASHES in google i want to get off this bug for ever.

how could i get off it

this is my class

public class CircleTransform implements Transform {

@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    /* int x = (source.getWidth() - size) / 2 */;
    /* int y = (source.getHeight() - size) / 2 */;

    int x = (source.getWidth() - size);
    int y = (source.getHeight() - size);

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap,
            BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f/* 2f */;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
}

@Override
public String key() {
    return "circle";
}
}

and this is the Crash that i get

  java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@41ff4570
 at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1084)
 at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:844)
 at com.koushikdutta.ion.IonDrawable.draw(IonDrawable.java:673)
 at android.widget.ImageView.onDraw(ImageView.java:1019)
 at android.view.View.draw(View.java:14728)
 at android.view.View.getDisplayList(View.java:13588)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.view.View.getDisplayList(View.java:13580)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.view.View.draw(View.java:14739)
 at android.view.View.getDisplayList(View.java:13588)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.view.View.getDisplayList(View.java:13580)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.view.View.getDisplayList(View.java:13580)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.view.View.getDisplayList(View.java:13580)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.view.View.getDisplayList(View.java:13580)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.View.draw(View.java:14430)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3252)
 at android.widget.ListView.drawChild(ListView.java:3387)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3070)
 at android.widget.AbsListView.dispatchDraw(AbsListView.java:2554)
 at android.widget.ListView.dispatchDraw(ListView.java:3382)
 at android.view.View.draw(View.java:14739)
 at android.widget.AbsListView.draw(AbsListView.java:4093)
 at android.view.View.getDisplayList(View.java:13588)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
 at android.view.View.getDisplayList(View.java:13509)
 at android.view.View.getDisplayList(View.java:13635)
 at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1627)
 at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1506)
 at android.view.ViewRootImpl.draw(ViewRootImpl.java:2722)
 at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2587)
 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2210)
 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
 at android.view.Choreographer.doCallbacks(Choreographer.java:591)
 at android.view.Choreographer.doFrame(Choreographer.java:560)
 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
 at android.os.Handler.handleCallback(Handler.java:808)
 at android.os.Handler.dispatchMessage(Handler.java:103)
 at android.os.Looper.loop(Looper.java:193)
 at android.app.ActivityThread.main(ActivityThread.java:5292)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
 at dalvik.system.NativeStart.main(Native Method)

Solution

  • I just added support for RoundedImageView.

    https://github.com/koush/ion/blob/master/ion-sample/src/com/koushikdutta/ion/sample/RoundedImageViewSample.java