Android編程繪制圓形圖片的方法
本文實(shí)例講述了Android編程繪制圓形圖片的方法。分享給大家供大家參考,具體如下:
效果圖如下:
第一步:新建RoundView自定義控件繼承View
package com.rong.activity; import com.rong.test.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.widget.ImageView; /** * 利用Xfermode制作一個圓形圖片 * * @author 徐榮 * */ public class RoundView extends ImageView { /** * 畫筆 */ private Paint mPaint; /** * 原型圖 */ private Bitmap src; /** * 遮罩層 */ private Bitmap mask; public RoundView(Context context, AttributeSet attrs) { super(context, attrs); // 初始化畫筆 mPaint = new Paint(); mPaint.setAntiAlias(true); // 拿到原型圖 src = BitmapFactory.decodeResource(getResources(), R.drawable.ml); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 圖片的遮罩,為什么要在這里面初始化遮罩層呢?因?yàn)樵谶@個方法里Width()和Height()才被初始化了 mask = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Config.ARGB_4444); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 設(shè)置畫布的顏色為透明 canvas.drawColor(Color.TRANSPARENT); // 劃出你要顯示的圓 Canvas cc = new Canvas(mask); cc.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, getMeasuredHeight() / 2, mPaint); // 這個方法相當(dāng)于PS新建圖層,下面你要做的事就在這個圖層上操作 canvas.saveLayer(0, 0, getMeasuredWidth(), getMeasuredHeight(), null, Canvas.ALL_SAVE_FLAG); // 先繪制遮罩層 canvas.drawBitmap(mask, 0, 0, mPaint); // 設(shè)置混合模式 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // 再繪制src源圖 canvas.drawBitmap(src, 0, 0, mPaint); // 還原混合模式 mPaint.setXfermode(null); // 還原畫布,相當(dāng)于Ps的合并圖層 canvas.restore(); } }
第二步 新建布局文件Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" > <com.rong.activity.RoundView android:layout_width="160dp" android:layout_height="160dp" android:layout_centerInParent="true" /> </RelativeLayout>
運(yùn)行!
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android實(shí)現(xiàn)圓形圖片的兩種方式
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- 分享一個Android設(shè)置圓形圖片的特別方法
- Android自定義View實(shí)現(xiàn)旋轉(zhuǎn)的圓形圖片
- android繪制圓形圖片的兩種方式示例
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
- Android實(shí)現(xiàn)圓形圖片效果
相關(guān)文章
Android自定義流式布局實(shí)現(xiàn)淘寶搜索記錄
這篇文章主要為大家詳細(xì)介紹了Android自定義流式布局實(shí)現(xiàn)淘寶搜索記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10android使用ViewPager實(shí)現(xiàn)圖片自動切換
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)圖片自動切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02unity5.6 導(dǎo)出gradle工程 Android Studio 導(dǎo)入問題及處理方法
這篇文章主要介紹了unity5.6 導(dǎo)出gradle工程 Android Studio 導(dǎo)入問題及處理方法,需要的朋友可以參考下2017-12-12Android 項(xiàng)目實(shí)戰(zhàn)之頭像選擇功能
這篇文章主要介紹了Android 項(xiàng)目實(shí)戰(zhàn)頭像選擇功能實(shí)戰(zhàn),需要的朋友可以參考下2018-01-01Android自定義控件深入學(xué)習(xí) Android生成隨機(jī)驗(yàn)證碼
這篇文章主要再次為大家介紹了Android自定義控件,以及針對自定義view學(xué)習(xí),實(shí)戰(zhàn)演練了Android生成隨機(jī)驗(yàn)證碼的詳細(xì)過程,感興趣的小伙伴們可以參考一下2016-01-01Android ExpandableRecyclerView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android ExpandableRecyclerView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android開發(fā)實(shí)現(xiàn)TextView超鏈接5種方式源碼實(shí)例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)TextView超鏈接5種方式源碼實(shí)例,需要的朋友可以參考下2020-03-03Android實(shí)現(xiàn)倒計(jì)時30分鐘功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)倒計(jì)時30分鐘功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Android 系統(tǒng)相機(jī)拍照后相片無法在相冊中顯示解決辦法
這篇文章主要介紹了Android 系統(tǒng)相機(jī)拍照后相片無法在相冊中顯示解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12Android官方下拉刷新控件SwipeRefreshLayout使用詳解
這篇文章主要為大家詳細(xì)介紹了Android官方下拉刷新控件SwipeRefreshLayout使用方法,實(shí)例展示如何使用下拉刷新控件,感興趣的小伙伴們可以參考一下2016-07-07