自定義View類

1、如何建立自定義的View類android

   ①、建立一個繼承android.view.View類的Java類,而且重寫構造方法(至少須要重寫一個構造方法)canvas

   ②、根據須要重寫其餘方法ide

   ③、在項目的活動中,建立並實例化自定義的View類,而後將其添加到佈局管理器中(添加到佈局管理器的方法:佈局管理器.addView())函數

2、View類經常使用的函數佈局

   ①、onDraw() 當組件將要繪制它的內容時字體

   ②、onFinishInflate() 回調方法,當應用從XML加載該組件並用它構建界面以後調用的方法優化

   ③、onMeasure() 檢測View組件及其子組件的大小動畫

   ④、onLayout() 當該組件須要分配其子組件的位置、大小時this

   ⑤、onSizeChange() 當該組件的大小被改變時spa

   ⑥、onKeyDown 當按下某個鍵盤時

   ⑦、onKeyUp 當鬆開某個鍵盤時

   ⑧、onTrackballEvent 當發生軌跡球事件時

   ⑨、onTouchEvent 當發生觸屏事件時

   ⑩、onWindowFocusChanged(boolean) 當該組件獲得、失去焦點時

   11)、onAtrrachedToWindow() 當把該組件放入到某個窗口時

   12)、onDetachedFromWindow() 當把該組件從某個窗口上分離時觸發的方法

   13)、onWindowVisibilityChanged(int): 當包含該組件的窗口的可見性發生改變時觸發的方法

3、將自定義的Viewl類添加到相對應的窗口

   方式一:

    佈局管理器  變量名 = (佈局管理器)findViewById(R.id.ID)

    類  變量名A = new 類(this)

    佈局管理器.addView(變量名A)

   方式二:

   在xml佈局中添加

4、Android自定義View的構造函數

    有兩種方式自定義構造函數

  方式一:

   每一個構造函數分別調用基類的構造函數,再調用一個公共的初始化方法作額外的初始化工做

 1 public class MyView extends ListView {
 2     public MyView(Context context) {
 3         super(context);
 4         sharedConstructor();
 5     }
 6 
 7     public MyView(Context context, AttributeSet attrs) {
 8         super(context, attrs);
 9         sharedConstructor();
10     }
11 
12     public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
13         super(context, attrs, defStyleAttr);
14         sharedConstructor();
15     }
16     
17     private void sharedConstructor() {
18         // Do some initialize work.
19     }
20 }

   方式二:

   級聯式調用,每個構造函數調用比它多一個參數的構造函數,最後一個構造函數調用基類的構造函數,最後在作一些額外的初始化工做

  

 1 public class MyView extends ListView {
 2     public MyView(Context context) {
 3         this(context, null);
 4     }
 5 
 6     public MyView(Context context, AttributeSet attrs) {
 7         this(context, attrs, 0);
 8     }
 9 
10     public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
11         super(context, attrs, defStyleAttr);
12         
13         // Other initialize work.
14     }
15 }

注:

  建議採用第一種方式。由於第二種方法在某些狀況下會出現問題,好比你自定義的View繼承自ListView或TextView時,ListView或TextView內部的構造函數有一個默認的defStyle, 第二種方法調用時defStyle會傳入0,這將覆蓋基類中默認的defStyle,進而致使一系列問題。

5、Canvas 和 Paint

    Canvas表示畫布,Paint表明畫筆

  1)、Paint的屬性

       // 設置字體顏色  
        paint.setColor(Color.RED);  
        // 防鋸齒  
        paint.setAntiAlias(true);  
      
        //設置顏色過濾器,能夠在繪製顏色時實現不用顏色的變換效果   
        paint.setColorFilter(ColorFilter);   
           
        //若是該項設置爲true,則圖像在動畫進行中會濾掉對Bitmap圖像的優化操做,加      //快顯示   
        //速度,本設置項依賴於dither和xfermode的設置   
        paint.setFilterBitmap(true);   
          
        //當畫筆樣式爲STROKE或FILL_OR_STROKE時,設置筆刷的粗細度   
        paint.setStrokeWidth(10f);   
        //設置繪製路徑的效果,如點畫線等   
        paint.setPathEffect(PathEffect);   
          
        //設置圖像效果,使用Shader能夠繪製出各類漸變效果   
        paint.setShader(Shader);   
          
        //設置MaskFilter,能夠用不一樣的MaskFilter實現濾鏡的效果,如濾化,立體等   
        paint.setMaskFilter(MaskFilter);   
           
        //在圖形下面設置陰影層,產生陰影效果,radius爲陰影的角度,dx和dy爲陰影在x軸和y軸上的距離,color爲陰影的顏色   
        paint.setShadowLayer(float radius ,float dx,float dy,int color);   
           
        //設置畫筆的樣式,爲FILL,FILL_OR_STROKE,或STROKE   
        paint.setStyle(Paint.Style);   
           
        //當畫筆樣式爲STROKE或FILL_OR_STROKE時,設置筆刷的圖形樣式,圓形樣式ROUND,或方形樣式SQUARE  BUTT  
        paint.setStrokeCap(Paint.Cap);   
           
        //設置繪製時畫筆與圖形的結合方式,METER\ROUND\BEVEL  平滑效果  
        paint.setSrokeJoin(Paint.Join);   
          
        // 字體下劃線  
        paint.setUnderlineText(true);  
        // 暫時不知,有清楚的能夠告訴我,謝謝  
        paint.setLinearText(true);  
        // 字體加粗  
        paint.setFakeBoldText(true);  
        // 防抖動  
        paint.setDither(true);  
        // 透明度  
        paint.setAlpha(a);  

2)、如何獲取一個Canvas對象

    方式一:經過重寫View.onDraw方法,View中的Canvas對象會被當作參數傳遞過來,咱們操做這個Canvas,效果會直接反應在View中

    方式二:本身建立一個Canvas對象。從上面的基本要素能夠明白,一個Canvas對象必定是結合了一個Bitmap對象的。因此必定要爲一個Canvas對象設置一個Bitmap對象

       

1 //獲得一個Bitmap對象,固然也可使用別的方式獲得。可是要注意,改bitmap必定要是mutable(異變的)
2         Bitmap b = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
3         Canvas c = new Canvas(b);
4         /*先new一個Canvas對象,在調用setBitmap方法,同樣的效果
5          * Canvas c = new Canvas();
6          * c.setBitmap(b);
7          */

     方式三:調用SurfaceHolder.lockCanvas(),返回一個Canvas對象;能夠在 surfaceView 或 TextureView中使用

2.1)、Canvas能夠繪製的內容

 

       //填充
      drawARGB(int a, int r, int g, int b)
      drawColor(int color)
      drawRGB(int r, int g, int b)
      drawColor(int color, PorterDuff.Mode mode)

     //幾何圖形
     canvas.drawArc  //(扇形)

     canvas.drawCircle  //(圓)

     canvas.drawOval  //(橢圓)

     canvas.drawLine  //(線)

     canvas.drawPoint  //(點)

     canvas.drawRect  //(矩形)

     canvas.drawRoundRect  //(圓角矩形)

     canvas.drawVertices  //(頂點)

     cnavas.drawPath  //(路徑)

       //圖片
       canvas.drawBitmap  //(位圖)

       canvas.drawPicture   //(圖片)

       //文本
       canvas.drawText
//Canvas繪製經常使用圖形的方法以下:

    繪製直線:canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint);

    繪製矩形:canvas.drawRect(float left, float top, float right, float bottom, Paint paint);

    繪製圓形:canvas.drawCircle(float cx, float cy, float radius, Paint paint);

    繪製字符:canvas.drawText(String text, float x, float y, Paint paint);

    繪製圖形:canvas.drawBirmap(Bitmap bitmap, float left, float top, Paint paint);

 

2.1)、Canvas的保存和回滾

    Canvas還提供了保存和回滾屬性的方法(save和restore),好比你能夠先保存目前畫紙的位置(save),而後旋轉90度,向下移動100像素後畫一些圖形,畫完後調用restore方法返回到剛纔保存的位置

 

 1 /**
 2      * 保存當前的matrix和clip到私有的棧中(Skia內部實現)。任何matrix變換和clip操做都會在調用restore的時候還原。
 3      * @return 返回值能夠傳入到restoreToCount()方法,以返回到某個save狀態以前。
 4      */
 5     public native int save();
 6     
 7  
 8  
 9     /**
10      * 傳入一個標誌,來表示當restore 的時候,哪些參數須要還原。該參數定義在Canvas中,參照下面。
11      * save()方法默認的是還原matrix和clip,可是可使用這個方法指定哪些須要還原。而且只有指定matrix和clip纔有效,其他的幾個參數是
12      * 用於saveLayer()和saveLayerAlpha()方法 的。
13      */
14     public native int save(int saveFlags);
15  
16  
17     /**
18      * 回到上一個save調用以前的狀態,若是restore調用的次數大於save方法,會出錯。
19      */
20     public native void restore();
21  
22      /**
23      * 返回棧中保存的狀態,值等譯 save()調用次數-restore()調用次數
24      */
25     public native int getSaveCount();
26  
27  
28     
29  
30     /**
31      * 回到任何一個save()方法調用以前的狀態
32      */
33     public native void restoreToCount(int saveCount);
34  
35  
36  
37 /**saveFlags的參數*/
38  public static final int MATRIX_SAVE_FLAG = 0x01;//須要還原Matrix
39     public static final int CLIP_SAVE_FLAG = 0x02;//須要還原Clip
40 /**下面三個參數在saveLayer的時候使用,具體做用,沒有搞明白*/
41    public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 0x04;
42   public static final int FULL_COLOR_LAYER_SAVE_FLAG = 0x08;
43   public static final int CLIP_TO_LAYER_SAVE_FLAG = 0x10;
44     public static final int ALL_SAVE_FLAG = 0x1F; //還原全部
45  
46 /*關於saveLayer的具體flags還不大明白它的含義,具體怎麼使用在下面例子中*/
47  public int saveLayer(RectF bounds, Paint paint, int saveFlags)
48 public int saveLayer(float left, float top, float right, float bottom,
49                          Paint paint, int saveFlags) 
50  public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
51 public int saveLayerAlpha(float left, float top, float right, float bottom,
52                               int alpha, int saveFlags)

2.3)、Canvas的轉換

  Canvas還提供了一系列位置轉換的方法:rorate、scale、translate、skew(扭曲)等

 

 1 @Override
 2         protected void onDraw(Canvas canvas) {
 3             canvas.translate(100, 100);
 4             canvas.drawColor(Color.RED);//能夠看到,整個屏幕依然填充爲紅色
 5             
 6             canvas.drawRect(new Rect(-100, -100, 0, 0), new Paint());//縮放了
 7             canvas.scale(0.5f, 0.5f);
 8             canvas.drawRect(new Rect(0, 0, 100, 100), new Paint());
 9             
10             canvas.translate(200, 0);
11             canvas.rotate(30);
12             canvas.drawRect(new Rect(0, 0, 100, 100), new Paint());//旋轉了
13             
14             canvas.translate(200, 0);
15             canvas.skew(.5f, .5f);//扭曲了
16             canvas.drawRect(new Rect(0, 0, 100, 100), new Paint());
17             // canvas.setMatrix(matrix);//Matrix的使用在後面在是。
18         }

3)、Color類

  ①、Android系統中顏色的經常使用表示方法有如下3種:

  (1)int color = Color.BLUE;

  (2)int color = Color.argb(150,200,0,100);

  (3)在xml文件中定義顏色;

  ②、在實際應用當中,咱們經常使用的顏色有如下一些,其顏色常量及其表示的顏色以下所示:

  Color.BLACK      黑色                                       Color.GREEN                  綠色

  Color.BLUE        藍色                                       Color.LTGRAY                淺灰色

  Color.CYAN       青綠色                                     Color.MAGENTA              紅紫色

  Color.DKGRAY    灰黑色                                    Color.RED                      紅色

  Color.YELLOW    黃色                                       Color.TRANSPARENT       透明

  Color.GRAY        灰色                                       Color.WHITE                  白色

4、Bitmap  圖像

相關文章
相關標籤/搜索