首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

作图几何图形

2012-08-22 
绘制几何图形/* * 绘制几何图形 */import 略public class Ex07_02Activity extends Activity {/** Called

绘制几何图形
/*
 * 绘制几何图形
 */
import 略;

public class Ex07_02Activity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);MyView myView = new MyView(this);setContentView(myView);}private class MyView extends View {public MyView(Context context) {super(context);}// 重写onDraw()方法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);// 画一个空心的椭圆RectF re = new RectF(10, 220, 70, 250);canvas.drawOval(re, 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);paint.setShader(mShader);// 画一个渐变的圆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(getResources().getString(R.string.str_text1), 240,50, paint);canvas.drawText(getResources().getString(R.string.str_text2), 240,120, paint);canvas.drawText(getResources().getString(R.string.str_text3), 240,190, paint);canvas.drawText(getResources().getString(R.string.str_text4), 240,250, paint);canvas.drawText(getResources().getString(R.string.str_text5), 240,320, paint);canvas.drawText(getResources().getString(R.string.str_text6), 240,390, paint);}}}

下面我们来看看程序运行后的结果:
作图几何图形

热点排行