Android自訂圓角ImageView,androidimageview

來源:互聯網
上載者:User

Android自訂圓角ImageView,androidimageview


public class MyCirleImageView extends ImageView {


private static int RADIUS = 4; // 預設圓角的寬高是8dip,可以設定有參構造,傳入需要的值

public MyCirleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
/**取得Drawable*/
BitmapDrawable drawable = (BitmapDrawable) getDrawable();
if (drawable == null) { return; }
if (getWidth() == 0 || getHeight() == 0) { return; }
/**取得對應bitmap*/
Bitmap fullBitmap = drawable.getBitmap();
/**取得View在parent view裡面占的大小
* getWidth得到的事某個View的實際尺寸。getMeasuredWidth是某個View想要在parent view裡面占的大小
*/
int scaledWidth = getMeasuredWidth();
int scaledHeight = getMeasuredHeight();

Bitmap mScaledBitmap;
if (scaledWidth == fullBitmap.getWidth() && scaledHeight == fullBitmap.getHeight()) {
mScaledBitmap = fullBitmap;
} else {
mScaledBitmap = Bitmap.createScaledBitmap(fullBitmap, scaledWidth, scaledHeight, true);
}

try {
Bitmap roundBitmap = getRoundedCornerBitmap(getContext(), mScaledBitmap, RADIUS, scaledWidth, scaledHeight, false, false, false, false);
canvas.drawBitmap(roundBitmap, 0, 0, null);
} catch (Exception e) {
super.onDraw(canvas);
}
}


/**取得圓角圖片*/
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h, boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final float mulitDensity = context.getResources().getDisplayMetrics().density;

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
final float roundPx = pixels * mulitDensity;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

/**下面幾個判斷是實現:是否要保留對應直角*/
if (squareTL) {
canvas.drawRect(0, 0, w / 2, h / 2, paint);
}
if (squareTR) {
canvas.drawRect(w / 2, 0, w, h / 2, paint);
}
if (squareBL) {
canvas.drawRect(0, h / 2, w / 2, h, paint);
}
if (squareBR) {
canvas.drawRect(w / 2, h / 2, w, h, paint);
}
/**設定兩張圖片相交時的模式。
* 在正常的情況下,在已有的映像上繪圖將會在其上面添加一層新的形狀。如果新的Paint是完全不透明的,那麼它將完全遮擋住下面的Paint;如果它是部分透明的,那麼它將會被染上下面的顏色。
* 而setXfermode就可以來解決這個問題 
* paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    
* canvas.drawBitmap(srcBitmap, 0f, 0f, paint); canvas原有的圖片可以理解為背景,就是dst;新畫上去的圖片可以理解為前景,就是src。
* */
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(input, 0, 0, paint);
return output;
}

}

-----------------------把MyCirleImageView 當成ImageView放到布局檔案中使用就可以-----------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.