這篇文章給大家介紹如何在Android中自定義一個帶圓角的ImageView,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
站在用戶的角度思考問題,與客戶深入溝通,找到高碑店網(wǎng)站設(shè)計與高碑店網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋高碑店地區(qū)。
Demo:
package com.yizooo.yizooo.ui; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.RectF; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; import com.lidroid.xutils.bitmap.core.AsyncDrawable; /** * Created by 雪寶寶 on 2016/3/27. * 自定義圓角工具 */ 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(); } /** * 繪制圓角矩形圖片 */ @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); Bitmap bitmap = null; if (null != drawable && drawable instanceof BitmapDrawable ) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; bitmap = bitmapDrawable.getBitmap(); //Bitmap bitmap =( (BitmapDrawable)drawable).getBitmap(); Bitmap b = getRoundBitmap(bitmap, 10); final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight()); final Rect rectDest = new Rect(0,0,getWidth(),getHeight()); paint.reset(); canvas.drawBitmap(b, rectSrc, rectDest, paint); }//防止出現(xiàn)類型轉(zhuǎn)換異常 else if(this.getDrawable() instanceof AsyncDrawable){ bitmap = Bitmap .createBitmap( getWidth(), getHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas1 = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, getWidth(), getHeight()); drawable.draw(canvas1); } else { super.onDraw(canvas); } } /** * 獲取圓角矩形圖片方法 * @param bitmap * @param roundPx,一般設(shè)置成14 * @return Bitmap * @author caizhiming */ private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); int x = bitmap.getWidth(); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } }
關(guān)于如何在Android中自定義一個帶圓角的ImageView就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。