当前位置: 动力学知识库 > 问答 > 编程问答 >

android : roundedImageView with VectorDrawables

问题描述:

I am getting error while setting Vector Drawables to RoundedImageView. I am clear with error I am getting, its because I am trying to cast a drawable to BitmapDrawable but it should be VectorDrawable (If I am not wrong), So can anyone suggest me the way to handle any type of images whether it is jpg, png or vector.

RoundedImageView.java:

public class RoundedImageView extends ImageView {

public RoundedImageView(Context context) {

super(context);

}

public RoundedImageView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

@Override

protected void onDraw(Canvas canvas) {

Drawable drawable = getDrawable();

if (drawable == null) {

return;

}

if (getWidth() == 0 || getHeight() == 0) {

return;

}

Bitmap b = ((BitmapDrawable) drawable).getBitmap(); // line no 43

Bitmap bitmap = b.copy(Config.ARGB_8888, true);

int w = getWidth(), h = getHeight();

Bitmap roundBitmap = getCroppedBitmap(bitmap, w);

canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

Bitmap sbmp;

if (bmp.getWidth() != radius || bmp.getHeight() != radius)

sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);

else

sbmp = bmp;

Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),

Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xffa19774;

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

paint.setAntiAlias(true);

paint.setFilterBitmap(true);

paint.setDither(true);

canvas.drawARGB(0, 0, 0, 0);

paint.setColor(Color.parseColor("#BAB399"));

canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,

sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

canvas.drawBitmap(sbmp, rect, rect, paint);

return output;

}

}

My Error Log:

01-05 16:12:55.474 29943-29943/com.knowaguy.app E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.knowaguy.app, PID: 29943

java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

at com.knowaguy.app.customViews.RoundedImageView.onDraw(RoundedImageView.java:43)

at android.view.View.draw(View.java:16178)

at android.view.View.updateDisplayListIfDirty(View.java:15174)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.draw(View.java:16181)

at android.view.View.updateDisplayListIfDirty(View.java:15174)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:3588)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.draw(View.java:16181)

at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3097)

at android.view.View.updateDisplayListIfDirty(View.java:15174)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1229)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.draw(View.java:16181)

at android.view.View.updateDisplayListIfDirty(View.java:15174)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.updateDisplayListIfDirty(View.java:15169)

at android.view.View.draw(View.java:15948)

at android.view.ViewGroup.drawChild(ViewGroup.java:3609)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)

at android.view.View.draw(View.java:16181)

at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)

at android.view.View.updateDisplayListIfDirty(View.java:15174)

at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)

at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)

at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)

at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)

at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)

at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)

at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)

at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)

at android.view.Choreographer.doCallbacks(Chore

网友答案:

There may be an alternate approach too but I am not sure it will be efficient, You can convert your VectorDrawable to Bitmap since the method getBitmapDrawable() on a non instance of BitmapDrwable will throw exception( as you see in your case).

Use the following method to obtain a bitmap from drawable -

private Bitmap getBitmap(int drawableId) {
    Drawable vectorDrawable;
    if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){
        vectorDrawable = getResources().getDrawable(drawableId,null);
    }else {
        vectorDrawable = getResources().getDrawable(drawableId);
    }
    int h = vectorDrawable.getIntrinsicHeight();
    int w = vectorDrawable.getIntrinsicWidth();

    //Setting a default height in pixel if intrinsinc height or width is not found , eg in case of a shape drawable
    h=h>0?h:96;
    w=w>0?w:96;

    vectorDrawable.setBounds(0, 0, w, h);
    Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    vectorDrawable.draw(canvas);
    return bm;
}
网友答案:

I'm posting this answer, which I found elsewhere on stackoverflow, in order to show Sanjeet an alternate solution that I mentioned. Because the min API is so high this way, I don't claim that it is the best solution.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Bitmap _getBitmap(Context c, int svg_id) {
    Drawable drawable = ContextCompat.getDrawable(c, svg_id);
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawable) {
        return _getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap _getBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: