Android 通过Canvas画出绿色android机器人

android机器人很简单,就是几个圆角距形和几个圆形的组合,难点在于机器人头顶上的两根天线,涉及到旋转画布,需要通过不断地调整参数来定位到所需的位置。
先看效果图:

Java代码:


package com.pocketdigi;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

public class Main extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		RobotView cv = new RobotView(this);
		setContentView(cv);
	}

}
class RobotView extends View {
	public RobotView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		//背景为白色
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		//抗锯齿
		paint.setColor(Color.GREEN);
		//画笔颜色为绿色

		canvas.rotate(45.0f);
		//顺时针旋转画板45度
		canvas.drawRoundRect(new RectF(200, 0, 270, 10), 10, 10, paint);
		//左天线
		canvas.restore();
		//恢复canvas到原始状态
		canvas.rotate(-45.0f);
		canvas.drawRoundRect(new RectF(20, 285, 90, 295), 10, 10, paint);
		//右天线
		canvas.restore();
		canvas.drawCircle(200, 350, 100, paint);
		//画圆,下半圆直接被下面的身体覆盖
		canvas.drawRoundRect(new RectF(100, 350, 300, 530), 10, 10, paint);
		//画身体
		paint.setColor(Color.WHITE);
		//画笔改白色
		canvas.drawCircle(160, 300, 10, paint);
		//左眼
		canvas.drawCircle(240, 300, 10, paint);
		//右眼
		canvas.drawRect(new Rect(100,350,300,360), paint);
		//画白色距形,分开头跟身体
		paint.setColor(Color.GREEN);
		//换回绿色
		canvas.drawRoundRect(new RectF(60, 360, 90, 500), 10, 10, paint);
		//左手
		canvas.drawRoundRect(new RectF(310, 360, 340, 500), 10, 10, paint);
		//右手
		canvas.drawRoundRect(new RectF(130, 520, 160, 620), 10, 10, paint);
		//左脚
		canvas.drawRoundRect(new RectF(240, 520, 270, 620), 10, 10, paint);
		//右脚
		
	}

}

© 2011, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记