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

Android自定義ImageView實(shí)現(xiàn)圓角功能

 更新時間:2018年12月20日 11:48:35   作者:676598624  
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)圓角功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用自定義ImageView,實(shí)現(xiàn)圓角功能,供大家參考,具體內(nèi)容如下

1.自定義屬性attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="RoundCornerImageView">
    <attr name="radius" format="dimension" />
    <attr name="left_top_radius" format="dimension" />
    <attr name="right_top_radius" format="dimension" />
    <attr name="right_bottom_radius" format="dimension" />
    <attr name="left_bottom_radius" format="dimension" />
  </declare-styleable>
</resources>

2.自定義RoundCornerImageView,繼承AppCompatImageView

public class RoundCornerImageView extends AppCompatImageView {
  private float width, height;
  private int defaultRadius = 0;
  private int radius;
  private int leftTopRadius;
  private int rightTopRadius;
  private int rightBottomRadius;
  private int leftBottomRadius;


  public RoundCornerImageView(Context context) {
    this(context, null);
    init(context, null);
  }

  public RoundCornerImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    init(context, attrs);
  }

  public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs) {
    if (Build.VERSION.SDK_INT < 18) {
      setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    // 讀取配置
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
    radius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_radius, defaultRadius);
    leftTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_top_radius, defaultRadius);
    rightTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_top_radius, defaultRadius);
    rightBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_bottom_radius, defaultRadius);
    leftBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_bottom_radius, defaultRadius);


    //如果四個角的值沒有設(shè)置,那么就使用通用的radius的值。
    if (defaultRadius == leftTopRadius) {
      leftTopRadius = radius;
    }
    if (defaultRadius == rightTopRadius) {
      rightTopRadius = radius;
    }
    if (defaultRadius == rightBottomRadius) {
      rightBottomRadius = radius;
    }
    if (defaultRadius == leftBottomRadius) {
      leftBottomRadius = radius;
    }
    array.recycle();
  }


  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    width = getWidth();
    height = getHeight();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    //這里做下判斷,只有圖片的寬高大于設(shè)置的圓角距離的時候才進(jìn)行裁剪
    int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
    int maxRight = Math.max(rightTopRadius, rightBottomRadius);
    int minWidth = maxLeft + maxRight;
    int maxTop = Math.max(leftTopRadius, rightTopRadius);
    int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
    int minHeight = maxTop + maxBottom;
    if (width >= minWidth && height > minHeight) {
      Path path = new Path();
      //四個角:右上,右下,左下,左上
      path.moveTo(leftTopRadius, 0);
      path.lineTo(width - rightTopRadius, 0);
      path.quadTo(width, 0, width, rightTopRadius);

      path.lineTo(width, height - rightBottomRadius);
      path.quadTo(width, height, width - rightBottomRadius, height);

      path.lineTo(leftBottomRadius, height);
      path.quadTo(0, height, 0, height - leftBottomRadius);

      path.lineTo(0, leftTopRadius);
      path.quadTo(0, 0, leftTopRadius, 0);

      canvas.clipPath(path);
    }
    super.onDraw(canvas);
  }

}

3.布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
  android:orientation="vertical"
  tools:context="voicedemo.iflytek.com.roundimage.MainActivity">

  <voicedemo.iflytek.com.roundimage.RoundCornerImageView
    android:id="@+id/iv_avatar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="50dp"
    android:scaleType="centerCrop"
    app:left_top_radius="20dp"
    app:right_top_radius="20dp"
    />
</LinearLayout>

4.調(diào)用

public class MainActivity extends AppCompatActivity {

  String avatarUrl = "19e9d4c0a8f1cd033ecac3692_th.jpg";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView ivAvatar = findViewById(R.id.iv_avatar);

    Glide.with(this).load(avatarUrl).into(ivAvatar);

  }
} 

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

相關(guān)文章

  • Android ListView 實(shí)例講解清晰易懂

    Android ListView 實(shí)例講解清晰易懂

    這篇文章主要通過實(shí)例介紹了Android ListView,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Matrix的set,pre,post調(diào)用順序詳解

    Matrix的set,pre,post調(diào)用順序詳解

    下面小編就為大家?guī)硪黄狹atrix的set,pre,post調(diào)用順序詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android開發(fā)之Activity管理工具類完整示例

    Android開發(fā)之Activity管理工具類完整示例

    這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實(shí)例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android通過ImageView設(shè)置手指滑動控件縮放

    Android通過ImageView設(shè)置手指滑動控件縮放

    這篇文章主要介紹了Android通過ImageView設(shè)置手指滑動控件縮放效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-12-12
  • android獲取屏幕像素思路及代碼

    android獲取屏幕像素思路及代碼

    本文為大家詳細(xì)介紹下android是怎樣獲取屏幕像素的,感興趣的各位可以參考下哈,希望對你學(xué)習(xí)android有所幫助
    2013-04-04
  • Android編程實(shí)現(xiàn)一鍵鎖屏的方法

    Android編程實(shí)現(xiàn)一鍵鎖屏的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)一鍵鎖屏的方法,結(jié)合實(shí)例詳細(xì)分析了鎖屏功能所涉及的類與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-11-11
  • Android入門之日歷選擇與時間選擇組件的使用

    Android入門之日歷選擇與時間選擇組件的使用

    這篇文章主要為大家詳細(xì)介紹了Android中TimePicker時間選擇與DatePicker日期選擇組件的使用方法,文中的示例代碼講解詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Android自定義View實(shí)現(xiàn)帶4圓角或者2圓角的效果

    Android自定義View實(shí)現(xiàn)帶4圓角或者2圓角的效果

    這篇文章主要介紹了Android自定義View實(shí)現(xiàn)帶4圓角或者2圓角的效果,本文通過實(shí)例代碼截圖給大家展示的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android實(shí)現(xiàn)仿慕課網(wǎng)下拉加載動畫

    Android實(shí)現(xiàn)仿慕課網(wǎng)下拉加載動畫

    這篇文章是我在做動畫的項(xiàng)目中整理出來的,在eoe看了篇帖子,然后仿慕課網(wǎng)做了一個下拉加載動畫。此功能實(shí)現(xiàn)方法是AnimationDrawable類進(jìn)行 Animation-list中item的循環(huán)遍歷圖片,類似于flash里的幀幀動畫,需要的朋友可以參考下
    2015-07-07
  • Android文字匹配度算法及實(shí)際應(yīng)用示例

    Android文字匹配度算法及實(shí)際應(yīng)用示例

    本文介紹了Android應(yīng)用中常用的文字匹配度算法Levenshtein Distance,并給出了實(shí)際應(yīng)用示例,通過合理選擇和應(yīng)用文字匹配度算法,可以實(shí)現(xiàn)多種功能,提升用戶體驗(yàn),增強(qiáng)應(yīng)用的實(shí)用性,需要的朋友可以參考下
    2024-05-05

最新評論