自定義View

流程:
  • 創(chuàng)建MyView繼承View/ViewGroup,需要三個(gè)構(gòu)造方法
  • 重寫(xiě)onDraw()方法
  • 在布局xml中引用
如果需要添加自定義view的自定義屬性:
  • 在values包下的attrs文件中添加屬性id
  • 在 MyView的構(gòu)造方法中解析這個(gè)屬性值, 在onDraw()中設(shè)置這個(gè)值
  • 在布局xml中引用

自定義View需要注意的坑

  • 支持特殊屬性,warp_content,padding & margin
    如果不在onMeasur()中設(shè)置warp_content屬性,那么設(shè)置wrap_content則會(huì)失效,至于為什么以后再補(bǔ)吧
  • 多線程直接使用post(),view本生提供了post,不需要自己創(chuàng)建handler
  • 注意內(nèi)存泄漏,動(dòng)畫(huà)需要及時(shí)停止
  • 處理好事件沖突

demo:

創(chuàng)建MyView,繼承View/ViewGroup

public class CircularCoverView extends View

在values下的attrs文件創(chuàng)建CircularCoverView的自定義屬性


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--RoundImageView-->
    <declare-styleable name="roundedimageview">
        <attr name="border_thickness" format="dimension" />
        <attr name="border_inside_color" format="color" />
        <attr name="border_outside_color" format="color"/>
        <attr name="border_radius" format="dimension"/>
    </declare-styleable>
</resources>

重寫(xiě)三個(gè)構(gòu)造方法,在構(gòu)造方法中解析自定義屬性

 public CircularCoverView(Context context) {
        this(context, null, 0);
    }

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

    public CircularCoverView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
        leftTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
        leftBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
        rightTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
        rightBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
        coverColor = typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
    }

重寫(xiě)onDraw()

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        try {
            Paint paint = new Paint();
            paint.setFilterBitmap(false);
            paint.setStyle(Paint.Style.FILL);
            //create a canvas layer to show the mix-result
            @SuppressLint("WrongConstant") int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);
            //draw sector-dst-bitmap at first.
            canvas.drawBitmap(drawSector(getWidth(), getHeight()), 0, 0, paint);
            //set Xfermode of paint.
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
            //then draw rect-src-bitmap
            canvas.drawBitmap(drawRect(getWidth(), getHeight()), 0, 0, paint);
            paint.setXfermode(null);
            //restore the canvas
            canvas.restoreToCount(sc);
        }catch (Error e){

        }
    }

在xml引用并且設(shè)置自定義屬性值

<com.jszc.mobile.videostream.ui.CircularCoverView
            android:id="@+id/head_cover"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_centerInParent="true"
            app:left_top_radius="28dp"/>

完整的代碼


public class CircularCoverView extends View {

  private int leftTopRadians = 30;        //leftTopRadians
  private int leftBottomRadians = 30;     //leftBottomRadians
  private int rightTopRadians = 30;       //rightTopRadians
  private int rightBottomRadians = 30;    //rightBottomRadians
  private int border = 0;

  private int coverColor = 0xffeaeaea;    //color of cover.

  public CircularCoverView(Context context) {
      this(context, null, 0);
  }

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

  public CircularCoverView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
      leftTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
      leftBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
      rightTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
      rightBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
      coverColor = typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
  }


  /**
   * set radians of cover.
   */
  public void setRadians(int leftTopRadians, int rightTopRadians, int leftBottomRadians, int rightBottomRadians,int border) {
      this.leftTopRadians = leftTopRadians;
      this.rightTopRadians = rightTopRadians;
      this.leftBottomRadians = leftBottomRadians;
      this.rightBottomRadians = rightBottomRadians;
      this.border = border;
  }

  /**
   * set color of cover.
   *
   * @param coverColor cover's color
   */
  public void setCoverColor(@ColorInt int coverColor) {
      this.coverColor = coverColor;
  }

  /**
   * create a sector-bitmap as the dst.
   *
   * @param w width of bitmap
   * @param h height of bitmap
   * @return bitmap
   */
  private Bitmap drawSector(int w, int h) {
      Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
      Canvas c = new Canvas(bm);
      Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
      p.setColor(0xFFFFCC44);//notice:cannot set transparent color here.otherwise cannot clip at final.

      c.drawArc(new RectF(border, border, leftTopRadians * 2+border, leftTopRadians * 2+border), 180, 90, true, p);
      c.drawArc(new RectF(border, getHeight() - leftBottomRadians * 2-border, leftBottomRadians * 2+border, getHeight()-border), 90, 90, true, p);
      c.drawArc(new RectF(getWidth() - rightTopRadians * 2-border, border, getWidth()-border, rightTopRadians * 2+border), 270, 90, true, p);
      c.drawArc(new RectF(getWidth() - rightBottomRadians * 2-border, getHeight() - rightBottomRadians * 2-border, getWidth()-border, getHeight()-border), 0, 90, true, p);
      return bm;
  }

  /**
   * create a rect-bitmap as the src.
   *
   * @param w width of bitmap
   * @param h height of bitmap
   * @return bitmap
   */
  private Bitmap drawRect(int w, int h) {
      Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
      Canvas c = new Canvas(bm);
      Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
      p.setColor(coverColor);

      c.drawRect(new RectF(border, border, leftTopRadians+border, leftTopRadians+border), p);
      c.drawRect(new RectF(border, getHeight() - leftBottomRadians-border, leftBottomRadians+border, getHeight()-border), p);
      c.drawRect(new RectF(getWidth() - rightTopRadians-border, border, getWidth()-border, rightTopRadians+border), p);
      c.drawRect(new RectF(getWidth() - rightBottomRadians-border, getHeight() - rightBottomRadians-border, getWidth()-border, getHeight()-border), p);

      c.drawRect(new RectF(0, 0, getWidth(), border), p);
      c.drawRect(new RectF(0, 0, border, getHeight()), p);
      c.drawRect(new RectF(getWidth()-border, 0, getWidth(), getHeight()), p);
      c.drawRect(new RectF(0, getHeight()-border, getWidth(), getHeight()), p);

      return bm;
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @Override
  protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      try {
          Paint paint = new Paint();
          paint.setFilterBitmap(false);
          paint.setStyle(Paint.Style.FILL);

          //create a canvas layer to show the mix-result
          @SuppressLint("WrongConstant") int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);
          //draw sector-dst-bitmap at first.
          canvas.drawBitmap(drawSector(getWidth(), getHeight()), 0, 0, paint);
          //set Xfermode of paint.
          paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
          //then draw rect-src-bitmap
          canvas.drawBitmap(drawRect(getWidth(), getHeight()), 0, 0, paint);
          paint.setXfermode(null);
          //restore the canvas
          canvas.restoreToCount(sc);
      }catch (Error e){

      }
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容