最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義圓角ImageView控件

 更新時間:2017年05月05日 14:21:04   作者:東方月初  
這篇文章主要為大家詳細(xì)介紹了Android自定義圓角ImageView的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

目前一些比較火的圖片加載庫雖然支持圓角加載,若你是接的別人作了一半的項目,剛好別人用的圖片加載庫剛好不支持圓角加載,那么這顆控件你值得擁有.(支持網(wǎng)絡(luò)圖片的加載)

1.創(chuàng)建CustomImageView 類在你的項目中(源碼如下)

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.towatt.charge.towatt.R;

/**
 * @author Mr.lynn
 * @version 1.0<br>
 * 圖片圓角實現(xiàn)
 */
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
 private Paint paint;
 private Paint paintBorder;
 private Bitmap mSrcBitmap;
 /**
  * 圓角的弧度
  */
 private float mRadius;
 private boolean mIsCircle;

 public CustomImageView(final Context context) {
  this(context, null);
 }

 public CustomImageView(Context context, AttributeSet attrs) {
  this(context, attrs, R.attr.customImageViewStyle);
 }

 public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  TypedArray ta = context.obtainStyledAttributes(attrs,
    R.styleable.CustomImageView, defStyle, 0);
  mRadius = ta.getDimension(R.styleable.CustomImageView_radius, 0);
  mIsCircle = ta.getBoolean(R.styleable.CustomImageView_circle, false);
  int srcResource = attrs.getAttributeResourceValue(
    "http://schemas.android.com/apk/res/android", "src", 0);
  if (srcResource != 0)
   mSrcBitmap = BitmapFactory.decodeResource(getResources(),
     srcResource);
  ta.recycle();
  paint = new Paint();
  paint.setAntiAlias(true);
  paintBorder = new Paint();
  paintBorder.setAntiAlias(true);
 }

 @Override
 public void onDraw(Canvas canvas) {
  int width = canvas.getWidth() - getPaddingLeft() - getPaddingRight();
  int height = canvas.getHeight() - getPaddingTop() - getPaddingBottom();
  Bitmap image = drawableToBitmap(getDrawable());
  if (mIsCircle) {
   Bitmap reSizeImage = reSizeImageC(image, width, height);
   canvas.drawBitmap(createCircleImage(reSizeImage, width, height),
     getPaddingLeft(), getPaddingTop(), null);

  } else {

   Bitmap reSizeImage = reSizeImage(image, width, height);
   canvas.drawBitmap(createRoundImage(reSizeImage, width, height),
     getPaddingLeft(), getPaddingTop(), null);
  }
 }

 /**
  * 畫圓角
  *
  * @param source
  * @param width
  * @param height
  * @return
  */
 private Bitmap createRoundImage(Bitmap source, int width, int height) {
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  Canvas canvas = new Canvas(target);
  RectF rect = new RectF(0, 0, width, height);
  canvas.drawRoundRect(rect, mRadius, mRadius, paint);
  // 核心代碼取兩個圖片的交集部分
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  canvas.drawBitmap(source, 0, 0, paint);
  return target;
 }

 /**
  * 畫圓
  *
  * @param source
  * @param width
  * @param height
  * @return
  */
 private Bitmap createCircleImage(Bitmap source, int width, int height) {

  Paint paint = new Paint();
  paint.setAntiAlias(true);
  Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  Canvas canvas = new Canvas(target);
  canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2,
    paint);
  // 核心代碼取兩個圖片的交集部分
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  canvas.drawBitmap(source, (width - source.getWidth()) / 2,
    (height - source.getHeight()) / 2, paint);
  return target;

 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int width = MeasureSpec.getSize(widthMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
  setMeasuredDimension(width, height);
 }

 /**
  * drawable轉(zhuǎn)bitmap
  *
  * @param drawable
  * @return
  */
 private Bitmap drawableToBitmap(Drawable drawable) {
  if (drawable == null) {
   if (mSrcBitmap != null) {
    return mSrcBitmap;
   } else {
    return null;
   }
  } else if (drawable instanceof BitmapDrawable) {
   return ((BitmapDrawable) drawable).getBitmap();
  }
  Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
    drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  drawable.draw(canvas);
  return bitmap;
 }

 /**
  * 重設(shè)Bitmap的寬高
  *
  * @param bitmap
  * @param newWidth
  * @param newHeight
  * @return
  */
 private Bitmap reSizeImage(Bitmap bitmap, int newWidth, int newHeight) {
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  // 計算出縮放比
  float scaleWidth = ((float) newWidth) / width;
  float scaleHeight = ((float) newHeight) / height;
  // 矩陣縮放bitmap
  Matrix matrix = new Matrix();

  matrix.postScale(scaleWidth, scaleHeight);
  return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
 }

 /**
  * 重設(shè)Bitmap的寬高
  *
  * @param bitmap
  * @param newWidth
  * @param newHeight
  * @return
  */
 private Bitmap reSizeImageC(Bitmap bitmap, int newWidth, int newHeight) {
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  int x = (newWidth - width) / 2;
  int y = (newHeight - height) / 2;
  if (x > 0 && y > 0) {
   return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true);
  }

  float scale = 1;

  if (width > height) {
   // 按照寬度進行等比縮放
   scale = ((float) newWidth) / width;

  } else {
   // 按照高度進行等比縮放
   // 計算出縮放比
   scale = ((float) newHeight) / height;
  }
  Matrix matrix = new Matrix();
  matrix.postScale(scale, scale);
  return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
 }
}


2.在values目錄下創(chuàng)建attrs.xml(若是已存在該文件)直接復(fù)制如下代碼既可

<resources>
 <declare-styleable name="Theme">
  <attr name="customImageViewStyle" format="reference" />
 </declare-styleable>
 <!-- 自定義圓角ImageView -->
 <declare-styleable name="CustomImageView">
  <attr name="circle" format="boolean" />
  <attr name="radius" format="dimension" />
 </declare-styleable>
</resources>


3.正確的使用方式(在布局文件中)

注意此路徑是你控件所在包

 <com.xxx.xxx.view.CustomImageView
  android:layout_marginTop="5dp"
  android:layout_width="77dp"
  android:layout_height="77dp"
  lynn:radius="@dimen/size_10dp"
  android:src="@drawable/position_icon"
  android:id="@+id/iv_build_icon" />

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

相關(guān)文章

  • 快速處理ListView為空的情況

    快速處理ListView為空的情況

    下面小編就為大家?guī)硪黄焖偬幚鞮istView為空的情況。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 詳解Android ViewCompat的作用

    詳解Android ViewCompat的作用

    這篇文章主要介紹了詳解Android ViewCompat的作用的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Android NDK開發(fā)(C語言基本數(shù)據(jù)類型)

    Android NDK開發(fā)(C語言基本數(shù)據(jù)類型)

    這篇文章主要介紹了Android NDK開發(fā)中,C語言基本數(shù)據(jù)類型,主要以C語言包含的數(shù)據(jù)類型及基本類型展開相關(guān)資料,需要的朋友可以參考一下
    2021-12-12
  • flutter實現(xiàn)頭部tabTop滾動欄

    flutter實現(xiàn)頭部tabTop滾動欄

    這篇文章主要為大家詳細(xì)介紹了flutter實現(xiàn)頭部tabTop滾動欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android基礎(chǔ)總結(jié)篇之三:Activity的task相關(guān)介紹

    Android基礎(chǔ)總結(jié)篇之三:Activity的task相關(guān)介紹

    這篇文章主要介紹了Android基礎(chǔ)總結(jié)篇之三:Activity的task相關(guān)介紹,具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • Android自定義view貝塞爾曲線

    Android自定義view貝塞爾曲線

    這篇文章主要為大家詳細(xì)介紹了Android自定義view貝塞爾曲線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java4Android開發(fā)教程(二)hello world!

    Java4Android開發(fā)教程(二)hello world!

    一般的開發(fā)教程都是介紹完安裝配置開發(fā)環(huán)境,緊接著來一篇hello world,算是國際慣例吧,我們當(dāng)然也不能免俗,哈哈,各位看官請看好了!
    2014-10-10
  • Android?補間動畫及組合AnimationSet常用方法詳解

    Android?補間動畫及組合AnimationSet常用方法詳解

    這篇文章主要為大家介紹了Android?補間動畫及組合AnimationSet常用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Kotlin Fragment使用方法詳解

    Kotlin Fragment使用方法詳解

    Fragment是Android3.0后引入的一個新的API,他出現(xiàn)的初衷是為了適應(yīng)大屏幕的平板電腦, 當(dāng)然現(xiàn)在他仍然是平板APP UI設(shè)計的寵兒,而且我們普通手機開發(fā)也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段
    2023-01-01
  • Android videoview搶占焦點的處理方法

    Android videoview搶占焦點的處理方法

    這篇文章主要為大家詳細(xì)介紹了Android videoview搶占焦點的處理方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評論

长垣县| 福海县| 佛山市| 宜君县| 浦江县| 海宁市| 隆安县| 广汉市| 微山县| 双辽市| 宁晋县| 孟津县| 象州县| 忻州市| 南昌县| 谢通门县| 德清县| 那曲县| 怀来县| 普定县| 西宁市| 海兴县| 娱乐| 军事| 八宿县| 赞皇县| 牙克石市| 汉川市| 惠东县| 稷山县| 大方县| 石首市| 逊克县| 藁城市| 浠水县| 库伦旗| 邓州市| 库尔勒市| 宁城县| 琼结县| 鄱阳县|