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

Android基于Fresco實現(xiàn)圓角和圓形圖片

 更新時間:2022年04月01日 14:03:51   作者:YX_BB  
這篇文章主要為大家詳細(xì)介紹了Android基于Fresco實現(xiàn)圓角和圓形圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Fresco是FaceBook開源的Android平臺圖片加載庫,可以從網(wǎng)絡(luò),從本地文件系統(tǒng),本地資源加載圖片

Fresco本身已經(jīng)實現(xiàn)了圓角以及圓形圖片的功能。

<!--圓形圖片,一般用作頭像-->
<com.facebook.drawee.view.SimpleDraweeView
? ? android:id="@+id/iv_avatar"
? ? android:layout_width="40dp"
? ? android:layout_height="40dp"
? ? app:placeholderImage="@drawable/ic_avatar_default"
? ? app:roundAsCircle="true"/>
<!--圓角圖片,為了美觀大多數(shù)圖片都會有這樣的處理。-->
<!--當(dāng)圖片為正方形的時候,將roundedCornerRadius設(shè)置為邊長的一半,也可以形成圓形圖片的效果-->
<com.facebook.drawee.view.SimpleDraweeView
? ? android:id="@+id/iv_avatar"
? ? android:layout_width="40dp"
? ? android:layout_height="40dp"
? ? app:placeholderImage="@drawable/ic_avatar_default"
? ? app:roundedCornerRadius="5dp"/>

工作中,遇到圓形頭像的時候,UI通常會給我們這樣一張圖作為默認(rèn)圖片

理論上來講,只需要加入下列這行代碼,就可以完成這部分工作了

app:placeholderImage="@drawable/ic_avatar_default"

然而圓形圖片本身已經(jīng)是圓形的了,在有些機(jī)型上就出現(xiàn)了這個樣式。

搜索了一波,自帶的屬性都不能解決這個問題,干脆自己來定義這個圓形的實現(xiàn)吧,同時Fresco自帶的圓角效果只能保證使用統(tǒng)一的半徑,想要讓四個圓角的半徑不同,只能在java文件中設(shè)置,不夠靈活,定義圓角半徑的屬性也需要做些變更。

思路:自定義RoundImageView繼承自 SimpleDraweeVie,具備其所有的功能。
Canvas的clipPath(Path path)可以根據(jù)Path,將Canvas剪裁成我們想要的圖形。

public class RoundImageView extends SimpleDraweeView {
? ??
? ? private final static int DEFAULT_VALUE = 0;

? ? private float mWidth;
? ? private float mHeight;
? ? private Path mPath;

? ? // 圓角角度
? ? private float mCornerRadius;
? ? // 左上角圓角角度
? ? private float mLeftTopRadius;
? ? // 右上角圓角角度
? ? private float mRightTopRadius;
? ? // 右下角圓角角度
? ? private float mRightBottomRadius;
? ? // 左下角圓角角度
? ? private float mLeftBottomRadius;

? ? // 是否使用圓形圖片
? ? private boolean mAsCircle;
? ? // 圓形圖片半徑
? ? private float mRadius;
? ??
? ? public RoundImageView(Context context) {
? ? ? ? this(context, null);
? ? }

? ? public RoundImageView(Context context, AttributeSet attrs) {
? ? ? ? this(context, attrs, 0);
? ? }

? ? public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? initData();
? ? ? ? initAttrs(context, attrs);
? ? }
? ??
? ? private void initData() {
? ? ? ? mPath = new Path();
? ? }

? ? private void initAttrs(Context context, AttributeSet attrs) {
? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
? ? ? ? mCornerRadius = typedArray.getDimension(R.styleable.RoundImageView_cornerRadius, DEFAULT_VALUE);
? ? ? ? mAsCircle = typedArray.getBoolean(R.styleable.RoundImageView_asCircle, false);
? ? ? ? if (mCornerRadius <= 0) {
? ? ? ? ? ? // 說明用戶沒有設(shè)置四個圓角的有效值,此時四個圓角各自使用自己的值
? ? ? ? ? ? mLeftTopRadius = typedArray.getDimension(R.styleable.RoundImageView_leftTopRadius, DEFAULT_VALUE);
? ? ? ? ? ? mRightTopRadius = typedArray.getDimension(R.styleable.RoundImageView_rightTopRadius, DEFAULT_VALUE);
? ? ? ? ? ? mRightBottomRadius = typedArray.getDimension(R.styleable.RoundImageView_rightBottomRadius, DEFAULT_VALUE);
? ? ? ? ? ? mLeftBottomRadius = typedArray.getDimension(R.styleable.RoundImageView_leftBottomRadius, DEFAULT_VALUE);
? ? ? ? } else {
? ? ? ? ? ? // 使用了統(tǒng)一的圓角,因此使用mCornerRadius統(tǒng)一的值
? ? ? ? ? ? mLeftTopRadius = mCornerRadius;
? ? ? ? ? ? mRightTopRadius = mCornerRadius;
? ? ? ? ? ? mRightBottomRadius = mCornerRadius;
? ? ? ? ? ? mLeftBottomRadius = mCornerRadius;
? ? ? ? }
? ? ? ??
? ? ? ? typedArray.recycle();
? ? }

? ? @Override
? ? protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
? ? ? ? super.onLayout(changed, left, top, right, bottom);
? ? ? ? mWidth = getWidth();
? ? ? ? mHeight = getHeight();
? ? ? ? // 如果開啟了圓形標(biāo)記
? ? ? ? if (mAsCircle) {
? ? ? ? ? ? mRadius = Math.min(mWidth / 2, mHeight / 2);
? ? ? ? }
? ? }

? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? // 如果開啟了圓形標(biāo)記,圓形圖片的優(yōu)先級高于圓角圖片
? ? ? ? if(mAsCircle) {
? ? ? ? ? ? drawCircleImage(canvas);
? ? ? ? } else {
? ? ? ? ? ? drawCornerImage(canvas);
? ? ? ? }
? ? ? ? super.onDraw(canvas);
? ? }

? ? /**
? ? ?* 畫中間圓形
? ? ?* @param canvas
? ? ?*/
? ? private void drawCircleImage(Canvas canvas) {
? ? ? ? mPath.addCircle(mWidth / 2, mHeight / 2, mRadius, Path.Direction.CW);
? ? ? ? canvas.clipPath(mPath);
? ? }

? ? /**
? ? ?* 畫圓角
? ? ?* @param canvas
? ? ?*/
? ? private void drawCornerImage(Canvas canvas) {
? ? ? ? if (mWidth > mCornerRadius && mHeight > mCornerRadius) {
? ? ? ? ? ? // 設(shè)置四個角的x,y半徑值
? ? ? ? ? ? float[] radius = {mLeftTopRadius, mLeftTopRadius, mRightTopRadius, mRightTopRadius, mRightBottomRadius, mRightBottomRadius, mLeftBottomRadius, mLeftBottomRadius};
? ? ? ? ? ? mPath.addRoundRect(new RectF(0,0, mWidth, mHeight), radius, Path.Direction.CW);
? ? ? ? ? ? canvas.clipPath(mPath);
? ? ? ? }
? ? }
}

attr屬性如下

<!--適配android10的圖片控件-->
? ? <declare-styleable name="RoundImageView">
? ? ? ? <!--圓形圖片-->
? ? ? ? <attr name="asCircle" format="boolean"/>
? ? ? ? <!--左上角圓角半徑-->
? ? ? ? <attr name="leftTopRadius" format="dimension"/>
? ? ? ? <!--右上角圓角半徑-->
? ? ? ? <attr name="rightTopRadius" format="dimension"/>
? ? ? ? <!--右下角圓角半徑-->
? ? ? ? <attr name="rightBottomRadius" format="dimension"/>
? ? ? ? <!--左下角圓角半徑-->
? ? ? ? <attr name="leftBottomRadius" format="dimension"/>
? ? ? ? <!--四個圓角半徑,會覆蓋上邊四個圓角值-->
? ? ? ? <attr name="cornerRadius" format="dimension"/>
</declare-styleable>

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

相關(guān)文章

  • Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對象的應(yīng)用

    Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對象的應(yīng)用

    本篇文章小編為大家介紹,Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對象的應(yīng)用。需要的朋友參考下
    2013-04-04
  • Android 8.0系統(tǒng)中通知欄的適配微技巧

    Android 8.0系統(tǒng)中通知欄的適配微技巧

    這篇文章主要介紹了Android 8.0系統(tǒng)中通知欄的適配微技巧,Android 8.0系統(tǒng)最主要需要進(jìn)行適配的地方有兩處:應(yīng)用圖標(biāo)和通知欄。具體配置方法技巧大家參考下本文
    2018-04-04
  • AndroidStudio重新share代碼和上傳到svn新地址教程

    AndroidStudio重新share代碼和上傳到svn新地址教程

    這篇文章主要介紹了AndroidStudio重新share代碼和上傳到svn新地址教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Android自定義View實現(xiàn)動畫效果詳解

    Android自定義View實現(xiàn)動畫效果詳解

    這篇文章主要為大家詳細(xì)介紹了Android如何通過自定義View實現(xiàn)動畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-02-02
  • android之camera用法實例詳解

    android之camera用法實例詳解

    這篇文章主要介紹了android之camera用法,以實例形式較為詳細(xì)的分析了Android使用camera拍照的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2015-09-09
  • Android支付寶支付封裝代碼

    Android支付寶支付封裝代碼

    這篇文章主要介紹了Android支付寶支付封裝代碼,Android支付的時候肯定會使用支付寶進(jìn)行支付,封裝可以簡化操作步驟,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android studio 3.0安裝配置方法圖文教程

    Android studio 3.0安裝配置方法圖文教程

    這篇文章主要為大家詳細(xì)介紹了Android studio 3.0安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android  CardView詳解及使用方法和實例

    Android CardView詳解及使用方法和實例

    這篇文章主要介紹了Android CardView詳解及使用方法和實例的相關(guān)資料,這里附有實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下
    2016-12-12
  • Android控件系列之TextView使用介紹

    Android控件系列之TextView使用介紹

    TextView類似一般UI中的Label,TextBlock等控件,只是為了單純的顯示一行或多行文本,本文介紹了Android中文本控件TextView的用法和常用屬性的用法
    2012-11-11
  • android中Bitmap用法(顯示,保存,縮放,旋轉(zhuǎn))實例分析

    android中Bitmap用法(顯示,保存,縮放,旋轉(zhuǎn))實例分析

    這篇文章主要介紹了android中Bitmap用法,以實例形式較為詳細(xì)的分析了android中Bitmap操作圖片的顯示、保存、縮放、旋轉(zhuǎn)等相關(guān)技巧,需要的朋友可以參考下
    2015-09-09

最新評論