Android實(shí)現(xiàn)圓形圖片小工具
本文實(shí)例為大家分享了Android實(shí)現(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對(duì)象 ? ? ? ? ? ? BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); ? ? ? ? ? ? //計(jì)算縮放比例 ? ? ? ? ? ? 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- android圖片處理之讓圖片變成圓形
- Android布局自定義Shap圓形ImageView可以單獨(dú)設(shè)置背景與圖片
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android中使用Bitmap類將矩形圖片轉(zhuǎn)為圓形的方法
- Android實(shí)現(xiàn)圓形圖片的兩種方式
- Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- Android圓形頭像拍照后“無法加載此圖片”的問題解決方法(適配Android7.0)
相關(guān)文章
解決android.support.v4.content.FileProvide找不到的問題
這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android?ANR分析trace文件的產(chǎn)生流程詳情
這篇文章主要介紹了Android?ANR分析trace文件的產(chǎn)生流程詳情,文章圍繞主題展開相詳細(xì)的內(nèi)容介紹,需要的朋友可以參考一下2022-07-07Android開發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android ViewPager制作新手導(dǎo)航頁(動(dòng)態(tài)加載)
這篇文章主要為大家詳細(xì)介紹了Android ViewPager制作新手導(dǎo)航頁,了解什么是動(dòng)態(tài)加載指示器,感興趣的小伙伴們可以參考一下2016-05-05Android View.onMeasure方法詳解及實(shí)例
這篇文章主要介紹了Android View.onMeasure方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05android小動(dòng)畫:不斷擴(kuò)散的圓點(diǎn)
這篇文章介紹了如何實(shí)現(xiàn)android小動(dòng)畫:不斷擴(kuò)散的圓點(diǎn),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,下面的實(shí)例代碼,大家可以看看2021-11-11Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(六)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果第六篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android Canvas的drawText()與文字居中方案詳解
這篇文章主要給大家介紹了關(guān)于Android Canvas的drawText()與文字居中方案的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12