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

Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法

 更新時(shí)間:2016年09月01日 11:46:40   作者:logaxy  
圓形頭像看起來(lái)非常美觀,下文通過(guò)實(shí)例代碼給大家介紹android中使用CircleImageView實(shí)現(xiàn)圓形頭像的方法,一起看看吧

有時(shí)我們?cè)趹?yīng)用中會(huì)用到圓形頭像,下面是利用CircleImageView實(shí)現(xiàn)圓形頭像的演示,下面效果和代碼,效果如圖

實(shí)現(xiàn)起來(lái)也比較簡(jiǎn)單,先在項(xiàng)目中建一個(gè)circleimageview包用來(lái)存放CircleImageView類,待會(huì)直接把CircleImageView類復(fù)制到包里就可以使用了

然后,再建一個(gè)attrs.xml,其代碼相當(dāng)簡(jiǎn)單,定義了圓形邊框?qū)挾群皖伾?/p>

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="CircleImageView">
    <attr name="border_width" format="dimension" />
    <attr name="border_color" format="color" />
  </declare-styleable>
</resources>

然后是在自己應(yīng)用的Activity的布局文件中使用CircleImageView了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.icontest.MainActivity">
  <circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/circleImageView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:src="@mipmap/ic_launcher"
    app:border_width="2dp"
    app:border_color="#ccc"
     />
</RelativeLayout>

注意的是要寫上circleimageview.CircleImageView這個(gè)全路徑,還要指定

xmlns:app="

這個(gè)就是根據(jù)上面整個(gè)布局的

xmlns:android="

來(lái)的,把res/android改為res-auto即可,然后再指定border_width和border_color兩個(gè)屬性即可
然后再Activity中就直接可以利用id找到它了

CircleImageView icon;
icon= (CircleImageView) findViewById(R.id.circleImageView);

最后是CircleImageView類的代碼,這個(gè)網(wǎng)上隨便就能找到,不過(guò)這里還是貼出來(lái)吧

package circleimageview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.example.icontest.R;
public class CircleImageView extends ImageView {
  private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
  private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
  private static final int COLORDRAWABLE_DIMENSION = 2;
  private static final int DEFAULT_BORDER_WIDTH = 0;
  private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
  private final RectF mDrawableRect = new RectF();
  private final RectF mBorderRect = new RectF();
  private final Matrix mShaderMatrix = new Matrix();
  private final Paint mBitmapPaint = new Paint();
  private final Paint mBorderPaint = new Paint();
  private int mBorderColor = DEFAULT_BORDER_COLOR;
  private int mBorderWidth = DEFAULT_BORDER_WIDTH;
  private Bitmap mBitmap;
  private BitmapShader mBitmapShader;
  private int mBitmapWidth;
  private int mBitmapHeight;
  private float mDrawableRadius;
  private float mBorderRadius;
  private boolean mReady;
  private boolean mSetupPending;
  public CircleImageView(Context context) {
   super(context);
   init();
  }
  public CircleImageView(Context context, AttributeSet attrs) {
   this(context, attrs, 0);
  }
  public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
   mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
   mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);
   a.recycle();
   init();
  }
  private void init() {
   super.setScaleType(SCALE_TYPE);
   mReady = true;
   if (mSetupPending) {
     setup();
     mSetupPending = false;
   }
  }
  @Override
  public ScaleType getScaleType() {
   return SCALE_TYPE;
  }
  @Override
  public void setScaleType(ScaleType scaleType) {
   if (scaleType != SCALE_TYPE) {
     throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
   }
  }
  @Override
  public void setAdjustViewBounds(boolean adjustViewBounds) {
   if (adjustViewBounds) {
     throw new IllegalArgumentException("adjustViewBounds not supported.");
   }
  }
  @Override
  protected void onDraw(Canvas canvas) {
   if (getDrawable() == null) {
     return;
   }
   canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
   if (mBorderWidth != 0) {
     canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
   }
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   super.onSizeChanged(w, h, oldw, oldh);
   setup();
  }
  public int getBorderColor() {
   return mBorderColor;
  }
  public void setBorderColor(int borderColor) {
   if (borderColor == mBorderColor) {
     return;
   }
   mBorderColor = borderColor;
   mBorderPaint.setColor(mBorderColor);
   invalidate();
  }
  public int getBorderWidth() {
   return mBorderWidth;
  }
  public void setBorderWidth(int borderWidth) {
   if (borderWidth == mBorderWidth) {
     return;
   }
   mBorderWidth = borderWidth;
   setup();
  }
  @Override
  public void setImageBitmap(Bitmap bm) {
   super.setImageBitmap(bm);
   mBitmap = bm;
   setup();
  }
  @Override
  public void setImageDrawable(Drawable drawable) {
   super.setImageDrawable(drawable);
   mBitmap = getBitmapFromDrawable(drawable);
   setup();
  }
  @Override
  public void setImageResource(int resId) {
   super.setImageResource(resId);
   mBitmap = getBitmapFromDrawable(getDrawable());
   setup();
  }
  @Override
  public void setImageURI(Uri uri) {
   super.setImageURI(uri);
   mBitmap = getBitmapFromDrawable(getDrawable());
   setup();
  }
  private Bitmap getBitmapFromDrawable(Drawable drawable) {
   if (drawable == null) {
     return null;
   }
   if (drawable instanceof BitmapDrawable) {
     return ((BitmapDrawable) drawable).getBitmap();
   }
   try {
     Bitmap bitmap;
     if (drawable instanceof ColorDrawable) {
      bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
     } else {
      bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
     }
     Canvas canvas = new Canvas(bitmap);
     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
     drawable.draw(canvas);
     return bitmap;
   } catch (OutOfMemoryError e) {
     return null;
   }
  }
  private void setup() {
   if (!mReady) {
     mSetupPending = true;
     return;
   }
   if (mBitmap == null) {
     return;
   }
   mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
   mBitmapPaint.setAntiAlias(true);
   mBitmapPaint.setShader(mBitmapShader);
   mBorderPaint.setStyle(Paint.Style.STROKE);
   mBorderPaint.setAntiAlias(true);
   mBorderPaint.setColor(mBorderColor);
   mBorderPaint.setStrokeWidth(mBorderWidth);
   mBitmapHeight = mBitmap.getHeight();
   mBitmapWidth = mBitmap.getWidth();
   mBorderRect.set(0, 0, getWidth(), getHeight());
   mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);
   mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);
   mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);
   updateShaderMatrix();
   invalidate();
  }
  private void updateShaderMatrix() {
   float scale;
   float dx = 0;
   float dy = 0;
   mShaderMatrix.set(null);
   if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
     scale = mDrawableRect.height() / (float) mBitmapHeight;
     dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
   } else {
     scale = mDrawableRect.width() / (float) mBitmapWidth;
     dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
   }
   mShaderMatrix.setScale(scale, scale);
   mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);
   mBitmapShader.setLocalMatrix(mShaderMatrix);
  }
}

相關(guān)文章

  • Android獲取app應(yīng)用程序大小的方法

    Android獲取app應(yīng)用程序大小的方法

    本文通過(guò)一段代碼給大家介紹android獲取app應(yīng)用程序大小的方法,由于android對(duì)這種方法進(jìn)行了封裝,我們沒有權(quán)限去調(diào)用這個(gè)方法,只能通過(guò)aidl,然后用java的反射機(jī)制去調(diào)用系統(tǒng)級(jí)方法,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • 淺談Android Studio3.6 更新功能

    淺談Android Studio3.6 更新功能

    這篇文章主要介紹了Android Studio3.6 更新功能的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Android中顯示GIF動(dòng)畫的實(shí)現(xiàn)代碼

    Android中顯示GIF動(dòng)畫的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android中顯示GIF動(dòng)畫的實(shí)現(xiàn)代碼,較為詳細(xì)的分析了Android調(diào)用GIF動(dòng)畫所涉及的頁(yè)面布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • android SQLite數(shù)據(jù)庫(kù)總結(jié)

    android SQLite數(shù)據(jù)庫(kù)總結(jié)

    本文主要介紹了android SQLite數(shù)據(jù)庫(kù)的相關(guān)知識(shí)。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Android編程實(shí)現(xiàn)畫板功能的方法總結(jié)【附源碼下載】

    Android編程實(shí)現(xiàn)畫板功能的方法總結(jié)【附源碼下載】

    這篇文章主要介紹了Android編程實(shí)現(xiàn)畫板功能的方法,結(jié)合實(shí)例形式總結(jié)分析了Android基于自定義View與Canvas類實(shí)現(xiàn)畫板功能的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-02-02
  • Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)

    Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)

    這篇文章主要介紹了Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Android仿QQ個(gè)人標(biāo)簽添加與刪除功能

    Android仿QQ個(gè)人標(biāo)簽添加與刪除功能

    這篇文章主要為大家詳細(xì)介紹了Android仿QQ個(gè)人標(biāo)簽添加與刪除功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android使用ShareSDK實(shí)現(xiàn)應(yīng)用分享的功能

    Android使用ShareSDK實(shí)現(xiàn)應(yīng)用分享的功能

    這篇文章主要為大家詳細(xì)介紹了Android使用ShareSDK實(shí)現(xiàn)應(yīng)用分享的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)

    Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)

    這篇文章主要介紹了Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Android 錄音與播放功能的簡(jiǎn)單實(shí)例

    Android 錄音與播放功能的簡(jiǎn)單實(shí)例

    這篇文章主要介紹了 Android 錄音與播放功能的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06

最新評(píng)論