绘制几何图形:使用android.graphics类 (An-Beer 工作室)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
canvas.drawText(getResources().getString(R.string.str_text3),240,190,paint);
canvas.drawText(getResources().getString(R.string.str_text4),240,250,paint);
paint.setStrokeWidth(3);
/*画一个空心圆形*/
canvas.drawCircle(40,40,30,paint);
/*画一个空心正方形*/
canvas.drawRect(10,90,70,150,paint);
/*画一个空心长方形*/
canvas.drawRect(10,170,70,200,paint);
/*设置ContentView为自定义的MyVieW*/
MyViewmyView=newMyView(this);
setContentView(myView);
}
/*自定义继承View的MyView*/
privateclassMyViewextendsView
{
publicMyView(Contextcontext){
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.RectF;
importandroid.graphics.Path;
importandroid.graphics.Shader;
importandroid.graphics.LinearGradient;
path1.close();
canvas.drawPath(path1,paint);
/*设置paint的style为FILL:实心*/
paint.setStyle(Paint.Style.FILL);
/*设置paint的颜色*/
paint.setColor(Color.BLUE);
/*画一个实心圆*/
super(context);
}
/*重写onDraw()*/
@Override
protectedvoidonDraw(Canvascanvas)
{
super.onDraw(canvas);
/*设置背景为白色*/
canvas.drawColor(Color.WHITE);
Paintpaint=newPaint();
绘制几何图形:使用android.graphics类
优博网时间: 2010年12月16日18:11热度:123℃
-
-
范例说明
“如何在和机上绘制2D图形呢?”这是许多android游戏开发都是常提到的问题,在android SDK当中,并没有Java Graphics2D的函数可以使用,而是使用android.graphics底下的类来绘制2D向量图。这个package提供了许多在手机上绘制图形的类与方法,其中Canvas上,而Paint(Android.graphics.Paint)类则像是彩色铅笔,给予不同的调协,即可绘制不同颜色、不同种类效果的向量图形。
publicclassGraphicesActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
path.close();
canvas.drawPath(path,paint);
/*画一个空心梯形*/
Pathpath1=newPath();
path1.moveTo(10,410);
path1.lineTo(70,410);
path1.lineTo(55,350);
path1.lineTo(25,350);
RectFre3=newRectF(170,220,230,250);
canvas.drawOval(re3,paint);
/*画一个渐变色三角形*/
Pathpath4=newPath();
path4.moveTo(170,330);
path4.lineTo(230,330);
path4.lineTo(200,270);
/*画一个实心梯形*/
Pathpath3=newPath();
path3.moveTo(90,410);
path3.lineTo(150,410);
path3.lineTo(135,350);
path3.lineTo(105,350);
path3.close();
canvas.drawPath(path3,paint);
本范例将运用Paint对象(绘笔)的设置值不同,在Cavas(画布)上绘制空心、实习及渐变色的多种几何多形。
运行结果如果
范例程序
主程序中继承自Android.view.View的MyView类,重写MyView的onDraw()方法,一开始就会运行绘制的工作,在onDraw()中以Paint将几何图形绘制在Canvas上,以paint.setColor()改变图形颜色、以paint.setStyle()的设置来控制画出的的图形是空心还是实心。程序的最后一段,就是直接在Canvas写上文字,随着Paint对象里的属性设置,也会有不同的外观模式。
/*设置渐变色*/
ShadermShader=newLinearGradient(0,0,100,100,
newint[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},
null,Shader.TileMode.REPEAT);
paint.setShader(mShader);
canvas.drawCircle(120,40,30,paint);
/*画一个实心正方形*/
canvas.drawRect(90,90,150,150,paint);
/*画一个实心长方形*/
canvas.drawRect(90,170,150,200,paint);
/*画一个实心椭圆*/
RectFre2=newRectF(90,220,150,250);
/*画一个空心椭圆形*/
RectFre=newRectF(10,220,70,250);
canvas.drawOval(re,paint);
/*画一个空心三角形*/
Pathpath=newPath();
path.moveTo(10,330);
path.lineTo(70,330);
path.lineTo(40,270);
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);
packagecom.graphices;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.content.Context;
importandroid.graphics.Canvas;
/*去锯齿*/
paint.setAntiAlias(true);
/*设置paint的颜色*/
paint.setColor(Color.RED);
/*设置paint的style为STROKE:空心*/
paint.setStyle(Paint.Style.STROKE);
/*设置paint的外框宽度*/
/*画一个渐变色圆*/
canvas.drawCircle(200,40,30,paint);
/*画一个渐变色正方形*/
canvas.drawRect(170,90,230,150,paint);
/*画一个渐变色长方形*/
canvas.drawRect(170,170,230,200,paint);
/*画一个渐变色椭圆*/
path4.close();
canvas.drawPath(path4,paint);
/*画一个渐变色梯形*/
Pathpath5=newPath();
path5.moveTo(170,410);
path5.lineTo(230,410);
path5.lineTo(215,350);
path5.lineTo(185,350);
}
}
}Baidu Nhomakorabea
-
canvas.drawText(getResources().getString(R.string.str_text5),240,320,paint);
canvas.drawText(getResources().getString(R.string.str_text6),240,390,paint);
canvas.drawOval(re2,paint);
/*画一个实心三角形*/
Pathpath2=newPath();
path2.moveTo(90,330);
path2.lineTo(150,330);
path2.lineTo(120,270);
path2.close();
canvas.drawPath(path2,paint);
相关文档
最新文档