怎么在Android中通過自定義ImageView實現(xiàn)一個圓角功能-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關怎么在Android中通過自定義ImageView實現(xiàn)一個圓角功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)公司一直在為企業(yè)提供服務,多年的磨煉,使我們在創(chuàng)意設計,全網(wǎng)整合營銷推廣到技術研發(fā)擁有了開發(fā)經(jīng)驗。我們擅長傾聽企業(yè)需求,挖掘用戶對產(chǎn)品需求服務價值,為企業(yè)制作有用的創(chuàng)意設計體驗。核心團隊擁有超過10多年以上行業(yè)經(jīng)驗,涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領域,公司涉及領域有基礎互聯(lián)網(wǎng)服務移動服務器托管、APP應用開發(fā)、手機移動建站、網(wǎng)頁設計、網(wǎng)絡整合營銷。Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領導及開發(fā)。

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);


    //如果四個角的值沒有設置,那么就使用通用的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) {
    //這里做下判斷,只有圖片的寬高大于設置的圓角距離的時候才進行裁剪
    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);

  }
}

以上就是怎么在Android中通過自定義ImageView實現(xiàn)一個圓角功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁題目:怎么在Android中通過自定義ImageView實現(xiàn)一個圓角功能-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.muchs.cn/article8/dpgeop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設全網(wǎng)營銷推廣、電子商務網(wǎng)站排名、企業(yè)建站網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設