Android實(shí)現(xiàn)圓形圖片效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圓形圖片效果的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)建RoundPicture.java文件
在src/main/java/XX包下新建RoundPicture.java


寫(xiě)入RoundPicture.java文件
復(fù)制下方代碼,并引入類即可
public class RoundPicture extends androidx.appcompat.widget.AppCompatImageView {
private Paint paint;
public RoundPicture(Context context) {
this(context, null);
}
public RoundPicture(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundPicture(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
}
// 繪制圓形圖片
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (null != drawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap b = getCircleBitmap(bitmap, 14);
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);
} else {
super.onDraw(canvas);
}
}
// 獲取圓形圖片方法
private Bitmap getCircleBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
int x = bitmap.getWidth();
canvas.drawCircle(x / 2, x / 2, x / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
調(diào)用RoundPicture創(chuàng)建圓形圖片
只需在.xml文件中插入圖片處,將控件名改為
< XX.RoundPicture 并引入圖片即可
<com.example.jh_android.RoundPicture
android:id="@+id/iv"
android:layout_height="200dp"
android:layout_width="200dp"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:src="@drawable/head"
/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android實(shí)現(xiàn)圓形圖片的兩種方式
- 詳解Android中Glide與CircleImageView加載圓形圖片的問(wèn)題
- 分享一個(gè)Android設(shè)置圓形圖片的特別方法
- Android編程繪制圓形圖片的方法
- Android自定義View實(shí)現(xiàn)旋轉(zhuǎn)的圓形圖片
- android繪制圓形圖片的兩種方式示例
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
相關(guān)文章
解析Android獲取系統(tǒng)cpu信息,內(nèi)存,版本,電量等信息的方法詳解
本篇文章對(duì)用Android獲取系統(tǒng)cpu信息,內(nèi)存,版本,電量等信息的方法進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
Android實(shí)現(xiàn)自動(dòng)填寫(xiě)獲取驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)填寫(xiě)獲取驗(yàn)證碼功能,感興趣的小伙伴們可以參考一下2016-03-03
Android中應(yīng)用多進(jìn)程的整理總結(jié)
Android平臺(tái)支持多進(jìn)程通信,也支持應(yīng)用內(nèi)實(shí)現(xiàn)多進(jìn)程,下面這篇文章主要給大家介紹了關(guān)于Android中應(yīng)用多進(jìn)程的相關(guān)資料,文中介紹的很詳細(xì),相信對(duì)大家具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-01-01
關(guān)于ADB的Android Debug Bridge(安卓調(diào)試橋)那些事
這篇文章主要介紹了關(guān)于ADB的Android Debug Bridge(安卓調(diào)試橋)那些事,需要的朋友可以參考下2019-10-10
Jetpack Compose實(shí)現(xiàn)列表和動(dòng)畫(huà)效果詳解
這篇文章主要為大家詳細(xì)講講Jetpack Compose實(shí)現(xiàn)列表和動(dòng)畫(huà)效果的方法步驟,文中的代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06
Android自定義滾動(dòng)選擇器實(shí)例代碼
本篇文章主要介紹了Android自定義滾動(dòng)選擇器實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01

