Recently, there is a requirement to implement an ImageView with rounded corners. I searched for three parties online. Although the demos are correct, it is not possible to port it because when requesting links, Bitmap in xUtils is used for parsing, which will always report type conversion errors.
That's all I can define is one by myself.
Demo:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by Snow Baby on 2016/3/27. * Custom Rounding Tool */ public class RoundImageView extends ImageView { private Paint paint; public RoundImageView(Context context) { this(context,null); } public RoundImageView(Context context, AttributeSet attrs) { this(context, attrs,0); } public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); } /** * Draw a rounded rectangle picture */ @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); Bitmap bitmap = null; if (null != drawable && drawable instanceof BitmapDrawable ) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; bitmap = (); //Bitmap bitmap =( (BitmapDrawable)drawable).getBitmap(); Bitmap b = getRoundBitmap(bitmap, 10); final Rect rectSrc = new Rect(0, 0, (), ()); final Rect rectDest = new Rect(0,0,getWidth(),getHeight()); (); (b, rectSrc, rectDest, paint); }//Prevent type conversion exceptions else if(() instanceof AsyncDrawable){ bitmap = Bitmap .createBitmap( getWidth(), getHeight(), () != ? .ARGB_8888 : .RGB_565); Canvas canvas1 = new Canvas(bitmap); // (bitmap); (0, 0, getWidth(), getHeight()); (canvas1); } else { (canvas); } } /** * Method to get rounded rectangle picture * @param bitmap * @param roundPx, usually set to 14 * @return Bitmap * @author caizhiming */ private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) { Bitmap output = ((), (), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Rect rect = new Rect(0, 0, (), ()); final RectF rectF = new RectF(rect); (true); (0, 0, 0, 0); (color); int x = (); (rectF, roundPx, roundPx, paint); (new PorterDuffXfermode(Mode.SRC_IN)); (bitmap, rect, rect, paint); return output; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android: android:layout_width="fill_parent" android:layout_height="wrap_content" > < android: android:layout_width="@dimen/dp_47" android:layout_height="@dimen/dp_50" android:scaleType="fitXY" android:src="@mipmap/fuwutongzhi" android:layout_margin="@dimen/dp_10" /> </RelativeLayout>
I won’t post photos to the final renderings. Friends can see the renderings by trying them.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.