Java代碼
- package com.Javaeye.android.my;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.VIEw;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.graphics.Path;
- import android.graphics.Shader;
- import android.graphics.LinearGradIEnt;
//主程序中繼承自android.view.View的 MyView類,重寫 MyVIEw的onDraw()方法,
//一開始就會運行繪制的工作,在onDraw()中以Paint將幾何圖形繪制在Canvas上,
//以 paint.setColor() 改變圖形顏色、以 paint.setStyle()的設置來控制畫出的的
//圖形是空心還是實心。程序的最後一段,就是直接在Canvas寫上文字,隨著Paint對
//象裡的屬性設置,也會有不同的外觀模式。
- public class GraphicesActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /*設置ContentView為自定義的MyVIEW*/
- MyView myView=new MyVIEw(this);
- setContentView(myVIEw);
- }
-
- /* 自定義繼承View 的MyVIEw*/
- private class MyView extends VIEw {
- public MyVIEw(Context context){
- super(context) ;
- }
-
- /*重寫onDraw()*/
- @Override
- protected void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
- /*設置背景為白色*/
- canvas.drawColor(Color.WHITE);
-
- Paint paint=new Paint();
- /*去鋸齒*/
- paint.setAntiAlias(true);
- /*設置paint的顏色*/
- paint.setColor(Color.RED);
- /*設置paint的 style 為STROKE:空心*/
- paint.setStyle(Paint.Style.STROKE);
- /*設置paint的外框寬度*/
- paint.setStrokeWidth(3);
-
- /*畫一個空心圓形*/
- canvas.drawCircle(40, 40, 30, paint);
- /*畫一個空心正方形*/
- canvas.drawRect(10, 90, 70, 150, paint);
- /*畫一個空心長方形*/
- canvas.drawRect(10, 170, 70,200, paint);
- /*畫一個空心橢圓形*/
- canvas.drawOval(new RectF(10,220,70,250), paint);
- /*畫一個空心三角形*/
- Path path=new Path();
- path.moveTo(10, 330);
- path.lineTo(70,330);
- path.lineTo(40,270);
- path.close();
- canvas.drawPath(path, paint);
- /*畫一個空心梯形*/
- Path path1=new Path();
- path1.moveTo(10, 410);
- path1.lineTo(70,410);
- path1.lineTo(55,350);
- path1.lineTo(25, 350);
- path1.close();
- canvas.drawPath(path1, paint);
-
- /*設置paint的顏色*/
- paint.setColor(Color.BLUE);
- /*設置paint 的style為 FILL:實心*/
- paint.setStyle(Paint.Style.FILL);
- /*畫一個實心圓*/
- canvas.drawCircle(120,40,30, paint);
- /*畫一個實心正方形*/
- canvas.drawRect(90, 90, 150, 150, paint);
- /*畫一個實心長方形*/
- canvas.drawRect(90, 170, 150,200, paint);
- /*畫一個實心橢圓*/
- RectF re2=new RectF(90,220,150,250);
- canvas.drawOval(re2, paint);
- /*畫一個實心三角形*/
- Path path2=new Path();
- path2.moveTo(90, 330);
- path2.lineTo(150,330);
- path2.lineTo(120,270);
- path2.close();
- canvas.drawPath(path2, paint);
- /*畫一個實心梯形*/
- Path path3=new Path();
- path3.moveTo(90, 410);
- path3.lineTo(150,410);
- path3.lineTo(135,350);
- path3.lineTo(105, 350);
- path3.close();
- canvas.drawPath(path3, paint);
-
- /*設置漸變色*/
- Shader mShader=new LinearGradIEnt(0,0,100,100,
- new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},
- null,Shader.TileMode.REPEAT);
- //Shader.TileMode三種模式
- //REPEAT:沿著漸變方向循環重復
- //CLAMP:如果在預先定義的范圍外畫的話,就重復邊界的顏色
- //MIRROR:與REPEAT一樣都是循環重復,但這個會對稱重復
- paint.setShader(mShader);//用Shader中定義定義的顏色來話
-
- /*畫一個漸變色圓*/
- canvas.drawCircle(200,40,30, paint);
- /*畫一個漸變色正方形*/
- canvas.drawRect(170, 90, 230, 150, paint);
- /*畫一個漸變色長方形*/
- canvas.drawRect(170, 170, 230,200, paint);
- /*畫一個漸變色橢圓*/
- RectF re3=new RectF(170,220,230,250);
- canvas.drawOval(re3, paint);
- /*畫一個漸變色三角形*/
- Path path4=new Path();
- path4.moveTo(170,330);
- path4.lineTo(230,330);
- path4.lineTo(200,270);
- path4.close();
- canvas.drawPath(path4, paint);
- /*畫一個漸變色梯形*/
- Path path5=new Path();
- path5.moveTo(170, 410);
- path5.lineTo(230,410);
- path5.lineTo(215,350);
- path5.lineTo(185, 350);
- path5.close();
- canvas.drawPath(path5, paint);
-
- /*寫字*/
- paint.setTextSize(24);
- canvas.drawText("圓形", 240, 50, paint);
- canvas.drawText("正方形", 240, 120, paint);
- canvas.drawText("長方形", 240, 190, paint);
- canvas.drawText("橢圓形", 240, 250, paint);
- canvas.drawText("三角形", 240, 320, paint);
- canvas.drawText("梯形", 240, 390, paint);
- }
- }
- }