Android繪製進階之一:圖形的繪製

來源:互聯網
上載者:User

開始學習繪製,首先聯絡映像的基本元素的繪製,包括點線面及路徑的繪製,沒有難度,程式碼範例如下:
1. package com.mike.activity;
2. 
3. import android.R;
4. import android.app.Activity;
5. import android.graphics.Bitmap;
6. import android.graphics.Canvas;
7. import android.graphics.Color;
8. import android.graphics.Paint;
9. import android.graphics.Paint.Style;
10. import android.graphics.Path;
11. import android.graphics.RectF;
12. import android.graphics.drawable.BitmapDrawable;
13. import android.os.Bundle;
14. import android.view.ViewGroup.LayoutParams;
15. import android.widget.ImageView;
16. import android.widget.LinearLayout;
17. 
18. public class DrawDemoActivity extends Activity {
19.     /** Called when the activity is first created. */
20.     @Override
21.     public void onCreate(Bundle savedInstanceState) {
22.         super.onCreate(savedInstanceState);
23. 
24.         /*
25.          * 配置類型:1,ALPHA_8: 用於Alpha蒙版的位元影像,只為alpha通道分配8位,沒有其他顏色
26.          *           2,ARGB_4444, 
27.          *           3,ARGB_8888, 
28.          *           4,RGB565(與ARGB_8888具有相同的高品質,但佔用更少的記憶體)
29.          * 
30.          * ARGB_8888 :每個顏色佔8位,建立位元影像
31.          * param :      A:alpha通道:灰階通道,用來表明透明度資訊,共256級
32.          *           R:red   
33.          *           G:green    
34.          *           B:blue
35.          * 
36.          * 
37.          * Note: 蒙板:蒙板要結合圖層來使用。蒙板的用處很多,可以用來扣圖、建立選區等等,
38.          *             由於我好久不用PS了,所以蒙板的好多功能在這裡不能一下子講完。
39.                                                                舉個通俗的例子:蒙板就是一塊布,在某一圖層上添加蒙板後就是在那個圖層上蓋了一塊布,
40.                                                                利用那塊布可以實現多種效果,用黑色在蒙板上填充就看不見蒙板所在圖層下面的東西,
41.                                                                用白色填充就看見蒙板所在圖層下面的東西,還可以利用蒙板製作漸層。
42.                                                                總之蒙板這個功能說複雜它就複雜,說它簡單它就簡單,這個傢伙功能不小。
43.          * 
44.          */
45.         Bitmap bitmap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(),
46.                 getWindowManager().getDefaultDisplay().getHeight(),
47.                 Bitmap.Config.ARGB_8888);//配置
48.         
49.     
50.         
51.     Canvas canvas = new Canvas(bitmap); //畫布
52.     
53.     Paint paint = new Paint();
54.     paint.setColor(Color.WHITE);//可以直接設定顏色,也可通過Argb方法,,設定精確顏色
55. //  int myColor = Color.argb(alpha, red, green, blue);
56.     
57.     
58.     /*
59.      * 風格:
60.      *         STROKE 僅繪形狀的輪廓
61.      *         FILL   僅填充形狀 www.2cto.com
62.      *         FILL_AND_STROKE 填充並繪製形狀的輪廓
63.      */
64.     
65.     //1:畫一個點
66. //  paint.setStyle(Style.STROKE);不涉及封閉圖形,不寫此屬性ok~
67. //  paint.setStrokeWidth(100);
68. //  canvas.drawPoint(199, 201, paint);
69.     
70.     
71.     //2,畫一條線
72. //  paint.setStyle(Style.STROKE);不涉及封閉圖形,不寫此屬性ok~
73. //  paint.setStrokeWidth(10);
74. //  canvas.drawLine(50, 50, 100, 100, paint);
75.     
76.     //3,畫一個矩形
77. //  paint.setStyle(Style.FILL_AND_STROKE);
78. //  paint.setStrokeWidth(10);
79. //  canvas.drawRect(50, 50, 100, 100, paint);//另外一種繪製矩形的方法是:傳遞一個RectF對象
80.     
81.     //4,畫一個橢圓
82. //  paint.setStyle(Style.STROKE);
83. //  paint.setStrokeWidth(10);
84. //  RectF oval = new RectF(10, 10, 400, 200) ;//即矩形的內接圓
85. //  canvas.drawOval(oval , paint);
86.     
87.     //5,畫一個圓
88. //  paint.setStyle(Style.STROKE);
89. //  paint.setStrokeWidth(10);
90. //  canvas.drawCircle(100, 100, 50, paint);
91.     
92.     //6,路徑:通過路徑Path對象
93. //  paint.setStyle(Style.STROKE);
94. //  paint.setStrokeWidth(10);
95. //  Path path = new Path();
96. //  path.moveTo(20, 20);//起始點
97. //  path.lineTo(30, 30);
98. //  path.lineTo(40, 60);
99. //  path.lineTo(70, 100);
100. //  canvas.drawPath(path, paint);
101.     
102.     ImageView imageView = new ImageView(this);
103.     
104.     LayoutParams p = new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
105.     imageView.setLayoutParams(p);
106.     imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
107.     
108.     
109.     setContentView(imageView);
110.     
111. 
112.     }
113. }

 
摘自  小新專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.