|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
可怜的程序员,还是逃不出移植的命运!典范申明 “怎样在和机上绘制2D图形呢?”这是很多android游戏开辟都是常提到的成绩,在androidSDK傍边,并没有JavaGraphics2D的函数可使用,而是利用android.graphics底下的类来绘制2D向量图。这个package供应了很多在手机上绘制图形的类与办法,个中Canvas上,而Paint(Android.graphics.Paint)类则像是黑色铅笔,赐与分歧的调协,便可绘制分歧色彩、分歧品种效果的向量图形。
本典范将使用Paint工具(绘笔)的设置值分歧,在Cavas(画布)上绘制空心、练习及突变色的多种多少多形。
运转了局假如
典范程序
主程序中承继自Android.view.View的MyView类,重写MyView的onDraw()办法,一入手下手就会运转绘制的事情,在onDraw()中以Paint将多少图形绘制在Canvas上,以paint.setColor()改动图形色彩、以paint.setStyle()的设置来把持画出的的图形是空心仍是实心。程序的最初一段,就是间接在Canvas写上笔墨,跟着Paint工具里的属性设置,也会有分歧的表面形式。
package com.graphices;
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;
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);
/*画一个空心卵形*/
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 的style为 FILL:实心*/
paint.setStyle(Paint.Style.FILL);
/*设置paint的色彩*/
paint.setColor(Color.BLUE);
/*画一个实心圆*/
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);
}
}
}
计算机发展到这个时候,很多技术日益成熟,想学好一种技术都是不容易的,当你学会用的时候你对它的很多原理可能很不了解) |
|