Android實現(xiàn)圓形圖片小工具
本文實例為大家分享了Android實現(xiàn)圓形圖片小工具的具體代碼,供大家參考,具體內(nèi)容如下
1.CircleImageView類代碼
public class CircleImageView extends androidx.appcompat.widget.AppCompatImageView {
? ? //畫筆
? ? private Paint mPaint;
? ? //圓形圖片的半徑
? ? private int mRadius;
? ? //圖片的宿放比例
? ? private float mScale;
? ? public CircleImageView(Context context) {
? ? ? ? super(context);
? ? }
? ? public CircleImageView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
? ? public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? //由于是圓形,寬高應(yīng)保持一致
? ? ? ? int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
? ? ? ? mRadius = size / 2;
? ? ? ? setMeasuredDimension(size, size);
? ? }
? ? @SuppressLint("DrawAllocation")
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? mPaint = new Paint();
? ? ? ? Drawable drawable = getDrawable();
? ? ? ? if (null != drawable) {
? ? ? ? ? ? Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
? ? ? ? ? ? //初始化BitmapShader,傳入bitmap對象
? ? ? ? ? ? BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
? ? ? ? ? ? //計算縮放比例
? ? ? ? ? ? mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());
? ? ? ? ? ? Matrix matrix = new Matrix();
? ? ? ? ? ? matrix.setScale(mScale, mScale);
? ? ? ? ? ? bitmapShader.setLocalMatrix(matrix);
? ? ? ? ? ? mPaint.setShader(bitmapShader);
? ? ? ? ? ? //畫圓形,指定好坐標(biāo),半徑,畫筆
? ? ? ? ? ? canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
? ? ? ? } else {
? ? ? ? ? ? super.onDraw(canvas);
? ? ? ? }
? ? }
}
2.布局文件中使用
代碼
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" ? ? tools:context=".MainActivity"> ? ?<com.hnucm18jr.myapplication.CircleImageView ? ? ? ?android:layout_width="100dp" ? ? ? ?android:layout_height="100dp" ? ? ? ?android:src="@drawable/a1" ? ? ? ?app:layout_constraintLeft_toLeftOf="parent" ? ? ? ?app:layout_constraintRight_toRightOf="parent" ? ? ? ?app:layout_constraintTop_toTopOf="parent" ? ? ? ?android:layout_marginTop="200dp"/> </androidx.constraintlayout.widget.ConstraintLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android裁剪圖片為圓形圖片的實現(xiàn)原理與代碼
- android圖片處理之讓圖片變成圓形
- Android布局自定義Shap圓形ImageView可以單獨設(shè)置背景與圖片
- Android中Glide加載圓形圖片和圓角圖片實例代碼
- Android中使用Bitmap類將矩形圖片轉(zhuǎn)為圓形的方法
- Android實現(xiàn)圓形圖片的兩種方式
- Android將Glide動態(tài)加載不同大小的圖片切圓角與圓形的方法
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0)
相關(guān)文章
Android 關(guān)于“NetworkOnMainThreadException”問題的原因分析及解決辦法
這篇文章主要介紹了Android 關(guān)于“NetworkOnMainThreadException”的相關(guān)知識,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
解決Android Studio 格式化快捷鍵和QQ 鎖鍵盤快捷鍵沖突問題
每次打開qq使用android studio格式化的快捷鍵Ctrl + Alt +L時,總是出現(xiàn)qq鎖鍵盤提示,怎么回事呢?下面小編給大家?guī)砹薬ndroid studio格式化的快捷鍵和qq快捷鍵之間的沖突的處理方法,需要的朋友參考下吧2017-12-12
Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實例
這篇文章主要介紹了Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實現(xiàn)方法,結(jié)合完整實例形式較為詳細的分析了Android針對登錄數(shù)據(jù)的保存及回顯操作技巧,需要的朋友可以參考下2015-12-12
Android開發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)的數(shù)據(jù),感興趣可以來學(xué)習(xí)一下2022-08-08
Android開發(fā)中ProgressDialog簡單用法示例
這篇文章主要介紹了Android開發(fā)中ProgressDialog簡單用法,結(jié)合實例形式分析了Android使用ProgressDialog的進度條顯示與關(guān)閉、更新等事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android 關(guān)于ExpandableListView刷新問題的解決方法
下面小編就為大家?guī)硪黄狝ndroid 關(guān)于ExpandableListView刷新問題的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Android中使用socket使底層和framework通信的實現(xiàn)方法
native和framework的通信是通過jni,但是這一般只是framework調(diào)用native,native如果有消息要怎樣通知上層 呢?android中GSP模塊提供一種解決思路,但是實現(xiàn)有些復(fù)雜,這里介紹一種使用socket通信的方法可以使native和framework自由通信,感興趣的朋友一起看看吧2016-11-11
Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式
這篇文章主要介紹了Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

