以前的文章里边画一般都是一些矩形,今天就看看怎么在android手机屏幕上绘制一些几何图形,如三角形、多边形、椭圆、圆形、正方形 等等。并且设置 空心、实心。下面我们先来看看

       在android中可以绘制出那些几何图形

[color=#008000]     方法                                                                      说明 
    drawRect                                                               绘制矩形 
    drawCircle                                                              绘制圆形 
    drawOval                                                               绘制椭圆 
    drawPath                                                               绘制任意多边形 
    drawLine                                                                绘制直线 
    drawPoin                                                                绘制点[/color]



我们先来看看效果图吧:
[align=center][/align]
[align=left]       下面我们就来看看代码是怎么做的:[/align]
[align=left][b]Java代码:[/b]
[/align][code]<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
android:textColor="#00FF00" 
/> 
<xiaohang.zhimeng.GameView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 
</LinearLayout> 
[/code]
[b]Java代码:[/b]
[code]package eoe.demo; 

import android.app.Activity; 
import android.os.Bundle; 

public class Activity01 extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
} 
} 
[/code]
GameView 

[b]Java代码:[/b]
[code]package eoe.demo; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.util.AttributeSet; 
import android.view.View; 

public class GameView extends View implements Runnable { 
// 声明Paint对象 
private Paint mPaint = null; 

private GameView2 mGameView2 = null; 

public GameView(Context context, AttributeSet attr){ 
super(context,attr); 
System.out.println(1); 
// 构建画笔对象 
mPaint = new Paint(); 

mGameView2 = new GameView2(context); 
// 开启线程 
new Thread(this).start(); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
super.onDraw(canvas); 

// 设置画布为黑色背景 
canvas.drawColor(Color.BLACK); 

// 取消锯齿 
mPaint.setAntiAlias(true); 

// 设置画笔风格为空心 
mPaint.setStyle(Paint.Style.STROKE); 

{ 
// 定义矩形对象 
Rect rect1 = new Rect(); 
// 设置矩形大小 
rect1.left = 5; 
rect1.top = 5; 
rect1.bottom = 25; 
rect1.right = 45; 

mPaint.setColor(Color.BLUE); 
// 绘制矩形 
canvas.drawRect(rect1, mPaint); 

mPaint.setColor(Color.RED); 
// 绘制矩形 
canvas.drawRect(50, 5, 90, 25, mPaint); 

mPaint.setColor(Color.YELLOW); 
// 绘制圆形 
// 40 70 分别是圆心的X 和 Y坐标 30为半径 mPaint为画笔对象 
canvas.drawCircle(40, 70, 30, mPaint); 

// 定义椭圆 
RectF rectf1 = new RectF(); 
rectf1.left = 80; 
rectf1.top = 30; 
rectf1.right = 120; 
rectf1.bottom = 70; 

mPaint.setColor(Color.LTGRAY); 
// 绘制椭圆 
canvas.drawOval(rectf1, mPaint); 

// 绘制多边形 
Path path1 = new Path(); 

/** 
* 这个多变形我也没试验它到底是怎么画 应该就是从起点 找点 一个点 一个点的连线 
*/ 
path1.moveTo(150 + 5, 80 - 50); // 此点为多边形的起点 
path1.lineTo(150 + 45, 80 - 50); 
path1.lineTo(150 + 30, 120 - 50); 
path1.lineTo(150 + 20, 120 - 50); 
// 使这些点构成封闭的多边形 
path1.close(); 

mPaint.setColor(Color.GRAY); 
// 绘制这个多边形 
canvas.drawPath(path1, mPaint); 

mPaint.setColor(Color.RED); 
// 设置画笔空心边框的宽度 
mPaint.setStrokeWidth(3); 
// 绘制直线 
// 这个绘制直线的方法 前2个参数是前点坐标 后 2个参数是 终点坐标我们可看出两个点的Y坐标都一样的 
canvas.drawLine(5, 110, 315, 110, mPaint); 
} 

// 绘制实心几何体 
// 将画笔设置为实心 
mPaint.setStyle(Paint.Style.FILL); 
{ 
// 定义矩形 
Rect rect1 = new Rect(); 
rect1.left = 5; 
rect1.top = 130 + 5; 
rect1.bottom = 130 + 25; 
rect1.right = 45; 

mPaint.setColor(Color.BLUE); 
// 绘制矩形 
canvas.drawRect(rect1, mPaint); 

mPaint.setColor(Color.RED); 
// 绘制矩形 
canvas.drawRect(50, 130 + 5, 90, 130 + 25, mPaint); 

mPaint.setColor(Color.YELLOW); 
// 绘制圆形 这里参数就不说明了 上边已经说了 
canvas.drawCircle(40, 130 + 70, 30, mPaint); 

// 定义椭圆对象 
RectF rectf1 = new RectF(); 
// 设置椭圆大小 
rectf1.left = 80; 
rectf1.top = 130+30; 
rectf1.right = 120; 
rectf1.bottom = 130 + 70; 

mPaint.setColor(Color.LTGRAY); 
// 绘制椭圆 
canvas.drawOval(rectf1, mPaint); 

// 绘制多边形 
Path path1 = new Path(); 

// 设置多边形的点 
path1.moveTo(150+5, 130+80-50); 
path1.lineTo(150+45, 130+80-50); 
path1.lineTo(150+30, 130+120-50); 
path1.lineTo(150+20, 130+120-50); 
// 使这些点构成封闭的多边形 
path1.close(); 
mPaint.setColor(Color.GRAY); 
// 绘制这个多边形 
canvas.drawPath(path1, mPaint); 

mPaint.setColor(Color.RED); 
mPaint.setStrokeWidth(3); 
// 绘制直线 
canvas.drawLine(5, 130 + 110, 315, 130 + 110, mPaint); 
} 
// 通过ShapDrawable来绘制几何图形 
mGameView2.DrawShape(canvas); 

} 

@Override 
public void run() { 
while (!Thread.currentThread().isInterrupted()) { 
try { 
Thread.sleep(100); 
} catch (InterruptedException e) { 
Thread.currentThread().interrupt(); 
} 
// 使用postInvalidate可以直接在线程中更新界面 
postInvalidate(); 
} 

} 

} 
[/code]
       在android中还可以通过ShapDrawable来绘制图像,ShapDrawable可以设置画笔的形状。通过 getPaint 方法可以得到Paint对象,可以像前面一样设置这个画笔的颜色、尺寸等属性。然而,在ShapDrawable中提供了 setBounds 方法来设置图形显示的区域,最后通过ShapeDrawable 和 Draw方法将图形显示到屏幕上。 请见下边的代码 
  
      GameView2 

[b]Java代码:[/b]
[code]package eoe.Demo; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.LinearGradient; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.Rect; 
import android.graphics.Shader; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.graphics.drawable.shapes.PathShape; 
import android.graphics.drawable.shapes.RectShape; 
import android.view.View; 

public class GameView2 extends View { 

// 声明ShapDrawable对象 
ShapeDrawable mShapeDrawable = null; 

public GameView2(Context context) { 
super(context); 
} 

public void DrawShape(Canvas canvas) { 
// 实例化ShapeDrawable对象并说明是绘制一个矩形 
mShapeDrawable = new ShapeDrawable(new RectShape()); 

// 得到画笔paint对象并设置其颜色 
mShapeDrawable.getPaint().setColor(Color.RED); 

Rect bounds = new Rect(5, 250, 55, 280); 

// 设置图像显示的区域 
mShapeDrawable.setBounds(bounds); 

// 绘制图像 
mShapeDrawable.draw(canvas); 
/* =============================== */ 
/* 实例化ShapeDrawable对象并说明是绘制一个椭圆 */ 
mShapeDrawable = new ShapeDrawable(new OvalShape()); 

// 得到画笔paint对象并设置其颜色 
mShapeDrawable.getPaint().setColor(Color.GREEN); 

// 设置图像显示的区域 
mShapeDrawable.setBounds(70, 250, 150, 280); 

// 绘制图像 
mShapeDrawable.draw(canvas); 

Path path1 = new Path(); 
// 设置多边形 
path1.moveTo(150 + 5, 80 + 80 - 50); 
path1.lineTo(150 + 45, 80 + 80 - 50); 
path1.lineTo(150 + 30, 80 + 120 - 50); 
path1.lineTo(150 + 20, 80 + 120 - 50); 
// 使这些点封闭成多边形 
path1.close(); 

// PathShape后面两个参数分别是高度和宽度 
mShapeDrawable = new ShapeDrawable(new PathShape(path1, 150, 150)); 

// 得到画笔paint对象并设置其颜色 
mShapeDrawable.getPaint().setColor(Color.BLUE); 

// 设置图像显示的区域 
mShapeDrawable.setBounds(100, 170, 200, 280); 

// 绘制图像 
mShapeDrawable.draw(canvas); 

//绘制正方形 
mShapeDrawable = new ShapeDrawable(new RectShape()); 
//得到画笔并设置颜色 
Paint xh_Paint = mShapeDrawable.getPaint(); 

/*设置渐变色 这个正方形的颜色是改变的*/ 
Shader mShader=new LinearGradient(0,0,100,100, 
new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.LTGRAY}, 
null,Shader.TileMode.REPEAT); 
xh_Paint.setShader(mShader); 

mShapeDrawable.setBounds(250, 250, 280, 280); 
mShapeDrawable.draw(canvas); 
} 
} 
[/code]