android - Creating RoundImageView causes NullPointerException -
i want create roundedimageview imageview used in app. creating custom view that.
below xml file , source code custom imageview.
<linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_torightof="@id/deletecalllog" android:orientation="horizontal" > <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="4dip" > <com.nimbuzz.ui.roundedimageview android:id="@+id/avatarimage" android:layout_width="48dip" android:layout_height="48dip" android:layout_centerhorizontal="true" android:src="@drawable/default_avatar" /> <imageview android:id="@+id/subcalltypeicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@id/avatarimage" android:layout_alignright="@id/avatarimage" android:visibility="gone" /> </relativelayout> </linearlayout>
line: bitmap bitmap = b.copy(config.argb_8888, true); causing nullpointerexception. does'nt happens . on screens code works.
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(); bitmap bitmap = b.copy(config.argb_8888, true); int w = getwidth(), h = getheight(); bitmap roundbitmap = getcroppedbitmap(b, w); canvas.drawbitmap(roundbitmap, 0, 0, null); } public static bitmap getcroppedbitmap(bitmap bmp, int radius) { bitmap sbmp; if (bmp.getwidth() != radius || bmp.getheight() != radius) { float smallest = math.min(bmp.getwidth(), bmp.getheight()); float factor = smallest / radius; sbmp = bitmap.createscaledbitmap(bmp, (int)(bmp.getwidth() / factor), (int)(bmp.getheight() / factor), false); } else { sbmp = bmp; } bitmap output = bitmap.createbitmap(radius, radius, config.argb_8888); canvas canvas = new canvas(output); final int color = 0xffa19774; final paint paint = new paint(); final rect rect = new rect(0, 0, radius, radius); paint.setantialias(true); paint.setfilterbitmap(true); paint.setdither(true); canvas.drawargb(0, 0, 0, 0); paint.setcolor(color.parsecolor("#bab399")); canvas.drawcircle(radius / 2 + 0.7f, radius / 2 + 0.7f, radius / 2 + 0.1f, paint); paint.setxfermode(new porterduffxfermode(mode.src_in)); canvas.drawbitmap(sbmp, rect, rect, paint); return output; } }
try o create own imageview , directly set bitmap or resource in imageview.. import android.annotation.suppresslint; import android.content.context; import android.graphics.canvas; import android.graphics.path; import android.graphics.rectf; import android.util.attributeset; import android.widget.imageview; @suppresslint("drawallocation") public class roundedcornerimageview extends imageview { public roundedcornerimageview(context context) { super(context); } public roundedcornerimageview(context context, attributeset attrs) { super(context, attrs); } public roundedcornerimageview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void ondraw(canvas canvas) { float radius = 90.0f; // angle of round corners path clippath = new path(); rectf rect = new rectf(0, 0, this.getwidth(), this.getheight()); clippath.addroundrect(rect, radius, radius, path.direction.cw); canvas.clippath(clippath); super.ondraw(canvas); } }
Comments
Post a Comment