自定義圓形ImageView控制項

2021-07-12 06:37:17 字數 3631 閱讀 4925

首先自定義roundimageview繼承imageview

package com.bawei.view;

import android.annotation.suppresslint;

import android.content.context;

import android.content.res.typedarray;

import android.graphics.bitmap;

import android.graphics.bitmap.config;

import android.graphics.canvas;

import android.graphics.paint;

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.graphics.drawable.ninepatchdrawable;

import android.util.attributeset;

import android.widget.imageview;

import com.example.projectone_day11.r;

/**

* 圓形imageview,可設定最多兩個寬度不同且顏色不同的圓形邊框。

*/  

public class roundimageview extends imageview

public roundimageview(context context, attributeset attrs)

public roundimageview(context context)

@suppresslint("recycle")

private void setcustomattributes(attributeset attrs)

@override  

protected void ondraw(canvas canvas)  

if (getwidth() == 0 || getheight() == 0)  

this.measure(0, 0);  

if (drawable.getclass() == ninepatchdrawable.class)  

return;  

bitmap b = ((bitmapdrawable) drawable).getbitmap();  

bitmap bitmap = b.copy(bitmap.config.argb_8888, true);  

if (defaultwidth == 0)  

if (defaultheight == 0)  

// 保證重新讀取後不會因為大小而改變控制項寬、高的大小(針對寬、高為wrap_content布局的imageview,但會導致margin無效)  

// if (defaultwidth != 0 && defaultheight != 0)  

int radius = 0;  

if (mborderinsidecolor != defaultcolor  

&& mborderoutsidecolor != defaultcolor) else if (mborderinsidecolor != defaultcolor  

&& mborderoutsidecolor == defaultcolor) else if (mborderinsidecolor == defaultcolor  

&& mborderoutsidecolor != defaultcolor) else  

bitmap roundbitmap = getcroppedroundbitmap(bitmap, radius);  

canvas.drawbitmap(roundbitmap, defaultwidth / 2 - radius, defaultheight  

/ 2 - radius, null);  

}/**

* 獲取裁剪後的圓形

*/private bitmap getcroppedroundbitmap(bitmap bmp, int radius) else if (bmpheight < bmpwidth) else  

if (squarebitmap.getwidth() != diameter  

|| squarebitmap.getheight() != diameter) else  

bitmap output = bitmap.createbitmap(scaledsrcbmp.getwidth(),  

scaledsrcbmp.getheight(), config.argb_8888);  

canvas canvas = new canvas(output);  

paint paint = new paint();  

rect rect = new rect(0, 0, scaledsrcbmp.getwidth(),  

scaledsrcbmp.getheight());  

paint.setantialias(true);  

paint.setfilterbitmap(true);  

paint.setdither(true);  

canvas.drawargb(0, 0, 0, 0);  

canvas.drawcircle(scaledsrcbmp.getwidth() / 2,  

scaledsrcbmp.getheight() / 2, scaledsrcbmp.getwidth() / 2,  

paint);  

paint.setxfermode(new porterduffxfermode(mode.src_in));  

canvas.drawbitmap(scaledsrcbmp, rect, rect, paint);  

// bitmap**(recycle導致在布局檔案xml看不到效果)  

// bmp.recycle();  

// squarebitmap.recycle();  

// scaledsrcbmp.recycle();  

bmp = null;  

squarebitmap = null;  

scaledsrcbmp = null;  

return output;  

}  /**

* 邊緣畫圓

*/  

private void drawcircleborder(canvas canvas, int radius, int color)  

}布局設定如下:

最後自定義屬性

<?xml version="1.0" encoding="utf-8"?>

自定義圓形Imageview

1 學習一定要善於總結,和敢於使用新的知識 2 一直使用的都是別人寫好的控制項,今天趁著國慶放假有時間,嘗試自己寫經常要用到的框架 3 知識總結 一 用於建立canvas的bitmap不能是已經存在的bitmap 二 這個自定view中遇到乙個坑就是的大小和遮罩大小不匹配是,需要我們對bitmap做...

自定義圓形 ImageView

android預設的imageview是矩形的,為了達到圓形的目的,需要自定義控制項,繼承imageview,重寫ondraw函式。最終效果 具體步驟 1.先根據控制項的短的一邊為半徑繪製乙個圓形 bitmap bitmap bitmapdrawable drawable getbitmap int...

自定義圓形或圓角imageview

package com.yelong.medicalscience.widgets import android.annotation.suppresslint import android.content.context import android.graphics.bitmap import ...