Android實現(xiàn)圓形圖片小工具
更新時間:2022年09月15日 08:46:18 作者:Rose?J
這篇文章主要為大家詳細介紹了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.support.v4.content.FileProvide找不到的問題
這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android?ANR分析trace文件的產(chǎn)生流程詳情
這篇文章主要介紹了Android?ANR分析trace文件的產(chǎn)生流程詳情,文章圍繞主題展開相詳細的內(nèi)容介紹,需要的朋友可以參考一下2022-07-07
Android開發(fā)實現(xiàn)根據(jù)字母快速定位側(cè)邊欄
這篇文章主要為大家詳細介紹了Android開發(fā)實現(xiàn)根據(jù)字母快速定位側(cè)邊欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Android ViewPager制作新手導(dǎo)航頁(動態(tài)加載)
這篇文章主要為大家詳細介紹了Android ViewPager制作新手導(dǎo)航頁,了解什么是動態(tài)加載指示器,感興趣的小伙伴們可以參考一下2016-05-05
Android Canvas的drawText()與文字居中方案詳解
這篇文章主要給大家介紹了關(guān)于Android Canvas的drawText()與文字居中方案的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

