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

Android自定義View多種效果解析

 更新時(shí)間:2018年05月16日 09:53:26   作者:huangyi-blog  
這篇文章主要為大家詳細(xì)介紹了Android自定義View多種效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近項(xiàng)目里涉及到自定義View的東西還是挺多的,所以打算在自定義View上多花點(diǎn)時(shí)間,也順便分享給大家。

先總結(jié)下自定義View的步驟:
1、自定義View的屬性
2、在View的構(gòu)造方法中獲得我們自定義的屬性
[3、重寫onMeasure]
4、重寫onDraw

1.首先在我們的res/values/目錄下建立一個(gè)attrs.xml文件,然后在里面聲明我們我們需要的自定義屬性

我們定義了矩形的顏色,矩形的高度,矩形的寬度3個(gè)屬性,format是指該屬性的取值類型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
不太明白的可以google一下
然后在布局中聲明我們的自定義View

需要引入xmls:app="http://schemas.android.com/apk/res-auto"這樣就會(huì)自動(dòng)查找我們的自定義屬性,也可以采用另外一種引入方式,xmlns:app="http://schemas.android.com/apk/res/com.example.administrator.demoone.customeview.CustomeRectView"
我們的命名空間,后面的包路徑指的是項(xiàng)目的package

2.在View的構(gòu)造方法里獲取我們的自定義屬性

public class CustomeRectView extends View {
 private Paint mPiant;//定義畫筆對(duì)象
 private int rectColor;//矩形的顏色
 private int rectHeight;//矩形的高度
 private int rectWidth;//矩形的寬度
 public CustomeRectView(Context context) {
  this(context, null);
 }
 public CustomeRectView(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs, 0);
 }
 public CustomeRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  /**
   * 獲取我們自定義的屬性
   */
  TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomeRectView, defStyleAttr, 0);
  int arrayCount = array.getIndexCount();
  for (int i = 0; i < arrayCount; i++) {
   int index = array.getIndex(i);
   switch (index) {
    case R.styleable.CustomeRectView_rectColor:
     //getColor(int index,int defaultValue)
     rectColor=array.getColor(R.styleable.CustomeRectView_rectColor, Color.BLACK);
     break;
    case R.styleable.CustomeRectView_rectHeight:
     /**
      * 獲取dimension值得時(shí)候會(huì)有3個(gè)不同的方法getDimension()、getDimensionPixelSize()和getDimenPixelOffset()
      * 結(jié)果值都是將資源文件中定義的dip值乘以屏幕密度,即rectHeight*屏幕密度,只是getDimension()返回的是float,
      * 其余兩個(gè)返回的是int, 其中g(shù)etDimensionPixelSize()返回的是實(shí)際數(shù)值的四舍五入,
      * 而getDimensionPixelOffset返回的是實(shí)際數(shù)值去掉后面的小數(shù)點(diǎn);
      */
     rectHeight=array.getDimensionPixelOffset(R.styleable.CustomeRectView_rectHeight,200);
     break;
    case R.styleable.CustomeRectView_rectWidth:
     rectWidth=array.getDimensionPixelOffset(R.styleable.CustomeRectView_rectWidth,200);
     break;
   }
  }
  array.recycle();
 }
}

我們重寫了3個(gè)構(gòu)造方法,默認(rèn)的布局文件調(diào)用的是兩個(gè)參數(shù)的構(gòu)造方法,所以記得讓所有的構(gòu)造調(diào)用我們的三個(gè)參數(shù)的構(gòu)造,我們?cè)谌齻€(gè)參數(shù)的構(gòu)造中獲得自定義屬性。

3.重寫OnDraw,調(diào)用系統(tǒng)的onMeasure

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 mPiant=new Paint();
 mPiant.setColor(rectColor);//設(shè)置畫筆的顏色
 mPiant.setStyle(Paint.Style.FILL);//設(shè)置畫筆的樣式
 mPiant.setAntiAlias(true);//去除鋸齒
 /**
  * Draw the specified Rect using the specified paint. The rectangle will
  * be filled or framed based on the Style in the paint.
  *
  * @param left The left side of the rectangle to be drawn
  * @param top The top side of the rectangle to be drawn
  * @param right The right side of the rectangle to be drawn
  * @param bottom The bottom side of the rectangle to be drawn
  * @param paint The paint used to draw the rect
  */
 canvas.drawRect(0,0,getWidth(),getHeight(),mPiant);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

此時(shí)的效果是:

但是此時(shí)如果我們把布局文件的寬和高寫成wrap_content,去掉我們自定義的寬高屬性

運(yùn)行效果如下

系統(tǒng)幫我們測(cè)量的高度和寬度都是MATCH_PARNET,當(dāng)我們?cè)O(shè)置明確的寬度和高度時(shí),系統(tǒng)幫我們測(cè)量的結(jié)果就是我們?cè)O(shè)置的結(jié)果,當(dāng)我們?cè)O(shè)置為WRAP_CONTENT,或者M(jìn)ATCH_PARENT系統(tǒng)幫我們測(cè)量的結(jié)果就是MATCH_PARENT的長(zhǎng)度。

所以,當(dāng)設(shè)置了WRAP_CONTENT時(shí),我們需要自己進(jìn)行測(cè)量,即重寫onMesure方法”:
重寫之前先了解MeasureSpec的specMode,一共三種類型:
EXACTLY:一般是設(shè)置了明確的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一個(gè)最大值內(nèi),一般為WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int widthMode=MeasureSpec.getMode(widthMeasureSpec);
  int widthSize=MeasureSpec.getSize(widthMeasureSpec);
  int heightMode=MeasureSpec.getMode(heightMeasureSpec);
  int heightSize=MeasureSpec.getSize(heightMeasureSpec);
  int width;
  int height;

  //如果設(shè)置了明確的大小
  if(widthMode==MeasureSpec.EXACTLY){
   width=widthSize;
  }else{
   //設(shè)置寬
   width=dpToPx(getContext(),200);
  }

  if(heightMode==MeasureSpec.EXACTLY){
  height=heightSize;
  }else{
  //設(shè)置高
   height=dpToPx(getContext(),200);
  }

  setMeasuredDimension(width,height);
 }
 
 /**
 * 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素)
 */
 public int dpToPx(Context context, float dpValue){
  final float scale=context.getResources().getDisplayMetrics().density;
  return (int)(dpValue*scale+0.5f);
 }

此時(shí)的效果如下,就和我們?cè)诓季治募镌O(shè)置的一樣了

好了,后續(xù)還會(huì)陸續(xù)更新。

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

相關(guān)文章

最新評(píng)論