網站首頁 編程語言 正文
本文實例為大家分享了Android實現圓形圖片小工具的具體代碼,供大家參考,具體內容如下
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);
? ? ? ? //由于是圓形,寬高應保持一致
? ? ? ? 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);
? ? ? ? ? ? //畫圓形,指定好坐標,半徑,畫筆
? ? ? ? ? ? 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>
原文鏈接:https://blog.csdn.net/qq_46526828/article/details/108928654
相關推薦
- 2022-10-26 Python如何用NumPy讀取和保存點云數據_python
- 2023-03-13 pandas按某列降序的實現_python
- 2022-07-20 react中事件處理與柯里化的實現_React
- 2023-05-26 keras.layers.Conv2D()函數參數用法及說明_python
- 2022-07-14 python中h5py開源庫的使用樣例詳解_python
- 2023-05-20 Python優化算法之遺傳算法案例代碼_python
- 2022-06-14 如何利用Python擬合函數曲線詳解_python
- 2021-11-17 Linux配置免密登錄單機和全分布詳細教程_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支