欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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);
? ? ? ? //由于是圓形,寬高應保持一致
? ? ? ? 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>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 解決android.support.v4.content.FileProvide找不到的問題

    解決android.support.v4.content.FileProvide找不到的問題

    這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android?ANR分析trace文件的產(chǎn)生流程詳情

    Android?ANR分析trace文件的產(chǎn)生流程詳情

    這篇文章主要介紹了Android?ANR分析trace文件的產(chǎn)生流程詳情,文章圍繞主題展開相詳細的內(nèi)容介紹,需要的朋友可以參考一下
    2022-07-07
  • Android開發(fā)實現(xiàn)根據(jù)字母快速定位側邊欄

    Android開發(fā)實現(xiàn)根據(jù)字母快速定位側邊欄

    這篇文章主要為大家詳細介紹了Android開發(fā)實現(xiàn)根據(jù)字母快速定位側邊欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android ViewPager制作新手導航頁(動態(tài)加載)

    Android ViewPager制作新手導航頁(動態(tài)加載)

    這篇文章主要為大家詳細介紹了Android ViewPager制作新手導航頁,了解什么是動態(tài)加載指示器,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android自定義輪播圖效果

    Android自定義輪播圖效果

    這篇文章主要為大家詳細介紹了Android自定義輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android View.onMeasure方法詳解及實例

    Android View.onMeasure方法詳解及實例

    這篇文章主要介紹了Android View.onMeasure方法詳解及實例的相關資料,需要的朋友可以參考下
    2017-05-05
  • android小動畫:不斷擴散的圓點

    android小動畫:不斷擴散的圓點

    這篇文章介紹了如何實現(xiàn)android小動畫:不斷擴散的圓點,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,下面的實例代碼,大家可以看看
    2021-11-11
  • Flutter進階之實現(xiàn)動畫效果(六)

    Flutter進階之實現(xiàn)動畫效果(六)

    這篇文章主要為大家詳細介紹了Flutter進階之實現(xiàn)動畫效果第六篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android特效之水波紋的實現(xiàn)

    Android特效之水波紋的實現(xiàn)

    今天我們主要講一講如何通過自定義View(以下簡稱WaveView)實現(xiàn) "咻咻咻" 式的水波紋擴散效果,感興趣的小伙伴們可以參考學習。
    2016-08-08
  • Android Canvas的drawText()與文字居中方案詳解

    Android Canvas的drawText()與文字居中方案詳解

    這篇文章主要給大家介紹了關于Android Canvas的drawText()與文字居中方案的相關資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12

最新評論