Android自定義View圓形圖片控件代碼詳解
前言
在日常開發(fā)中,圓形的圖片效果還是很常見的??梢酝ㄟ^給Paint設(shè)置Xfermode來實(shí)現(xiàn),這里簡(jiǎn)單記錄如下。
實(shí)現(xiàn)
實(shí)現(xiàn)圓形效果的核心是PorterDuffXfermode,對(duì)于PorterDuffXfermode,這里不展開,可以查詢相關(guān)資料。
核心代碼
//繪制背景
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
//設(shè)置模式為:顯示背景層和上層的交集,且顯示上層圖像
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//繪制要顯示的圖像
canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);
//重置Xfermode
mPaint.setXfermode(null);
自定義屬性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CircleView"> <!--定義資源--> <attr name="src" format="reference" /> <!--定義類型--> <attr name="type" format="enum"> <!--圓形--> <enum name="round" value="1" /> <!--矩形--> <enum name="rect" value="2" /> </attr> </declare-styleable> </resources>
自定義控件
public class CircleView extends View { private static final int DEFAULT_SIZE = 200; private static final int DEFAULT_RADIUS = 20; private static final int TYPE_ROUND = 1; private static final int TYPE_RECT = 2; private int mSize; private int mResourceId; private int mType; private Paint mPaint; private Bitmap mSrcBitmap; public CircleView(Context context) { this(context, null); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mResourceId = ta.getResourceId(R.styleable.CircleView_src, R.mipmap.ic_launcher); mType = ta.getInt(R.styleable.CircleView_type, TYPE_ROUND); ta.recycle(); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getMeasureSize(widthMeasureSpec); int height = getMeasureSize(heightMeasureSpec); mSize = Math.min(width, height); setMeasuredDimension(mSize, mSize); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //繪制背景 if (mSrcBitmap == null) { mSrcBitmap = getScaleBitmap(); } if (mType == TYPE_ROUND) { canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint); } else if (mType == TYPE_RECT) { canvas.drawRoundRect(0, 0, mSize, mSize, DEFAULT_RADIUS, DEFAULT_RADIUS, mPaint); } //設(shè)置模式為:顯示背景層和上層的交集,且顯示上層圖像 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //繪制要顯示的圖像 canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint); //重置Xfermode mPaint.setXfermode(null); } private void init() { //禁用硬件加速,否則可能無法繪制圓形 setLayerType(LAYER_TYPE_HARDWARE, null); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); } private int getMeasureSize(int measureSpec) { int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); return mode == MeasureSpec.EXACTLY ? size : DEFAULT_SIZE; } /** * 獲取縮放后的Bitmap * * @return */ private Bitmap getScaleBitmap() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), mResourceId, options); options.inSampleSize = calcSampleSize(options, mSize, mSize); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(getResources(), mResourceId, options); } /** * 計(jì)算縮放比例 * * @param option * @param width * @param height * @return */ private int calcSampleSize(BitmapFactory.Options option, int width, int height) { int originWidth = option.outWidth; int originHeight = option.outHeight; int sampleSize = 1; while ((originWidth = originWidth >> 1) > width && (originHeight = originHeight >> 1) > height) { sampleSize = sampleSize << 1; } return sampleSize; } }
注意:如果沒有圓形的效果,那么可能需要禁用硬件加速:setLayerType(LAYER_TYPE_HARDWARE, null)
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <com.wangyz.custom.CircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:src="@drawable/image" /> <com.wangyz.custom.CircleView android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" app:src="@drawable/image" /> <com.wangyz.custom.CircleView android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" app:src="@drawable/image" app:type="rect" /> </LinearLayout>
效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- canvas雪花效果核心代碼分享
- Canvas實(shí)現(xiàn)動(dòng)態(tài)的雪花效果
- jquery實(shí)現(xiàn)漫天雪花飛舞的圣誕祝福雪花效果代碼分享
- android自定義view實(shí)現(xiàn)圓周運(yùn)動(dòng)
- Android自定義view實(shí)現(xiàn)輸入框效果
- Android自定義View實(shí)現(xiàn)雪花特效
- Android自定義view之太極圖的實(shí)現(xiàn)教程
- Android自定義View實(shí)現(xiàn)分段選擇按鈕的實(shí)現(xiàn)代碼
- Android自定義View實(shí)現(xiàn)跟隨手指移動(dòng)的小兔子
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- Android如何用自定義View實(shí)現(xiàn)雪花效果
相關(guān)文章
Android編程實(shí)現(xiàn)簡(jiǎn)單文件瀏覽器功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡(jiǎn)單文件瀏覽器功能,結(jié)合實(shí)例形式分析了Android文件管理器的布局、文件與目錄的遍歷、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android 7.0 SEAndroid app權(quán)限配置方法
今天小編就為大家分享一篇Android 7.0 SEAndroid app權(quán)限配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android 如何保證service在后臺(tái)不被kill
本文主要介紹了Android 如何保證service在后臺(tái)不被kill的方法。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02Suspend函數(shù)與回調(diào)的互相轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了Suspend函數(shù)與回調(diào)的互相轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android使用第三方服務(wù)器Bmob實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼
這篇文章主要介紹了Android使用第三方服務(wù)器Bmob實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的思路詳解,需要的朋友可以參考下2016-09-09Android高效加載大圖、多圖解決方案 有效避免程序OOM
這篇文章主要為大家詳細(xì)介紹了Android高效加載大圖、多圖解決方案,有效避免程序OOM,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android?Flutter實(shí)現(xiàn)精靈圖的使用詳解
在游戲開發(fā)中,精靈圖會(huì)將一個(gè)人物所有動(dòng)作放置在一張圖片中,通過坐標(biāo)定位選取其中一張圖展示。本文就來教你如何使用精靈圖,感興趣的可以了解一下2022-08-08Android 模仿QQ側(cè)滑刪除ListView功能示例
這篇文章主要介紹了Android 模仿QQ側(cè)滑刪除ListView功能示例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Android MotionEvent中g(shù)etX()和getRawX()的區(qū)別實(shí)例詳解
這篇文章主要介紹了Android MotionEvent中g(shù)etX()和getRawX()的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03