public class MainActivity extends Activity
{
int width,height;
private GameView gameview;
private Canvas canvas;
private Bitmap map;
private Paint paint;
public Path path;
private int color,pen;
private boolean isclear=true;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //获得手机屏幕的宽度和高度
width=getWindowManager().getDefaultDisplay().getWidth();
height=getWindowManager().getDefaultDisplay().getHeight(); //设置手机为全屏模式
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); gameview=new GameView(this, width, height); setContentView(gameview);
canvas=gameview.canvas;
map=gameview.map;
paint=gameview.paint;
path=gameview.dpath;
color=gameview.color;
} //创建菜单
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} //添加菜单事件监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
try
{
Random random=new Random();
int a=random.nextInt(100);//产生100以内的数据
File f=new File(Environment.getExternalStorageDirectory().getPath()+"/bitmap");
f.mkdir();
File file=new File(f,a+".jpg");
FileOutputStream outstream=new FileOutputStream(file); map.compress(Bitmap.CompressFormat.PNG, 90, outstream);
//把bitmap对象保存一张jpg的图片 Toast.makeText(this, "保存成功!", Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
break;
case R.id.Color:
if(path!=null)
{
canvas.drawPath(path, paint);
gameview.invalidate();
}
Builder dialog=new AlertDialog.Builder(this);
dialog.setSingleChoiceItems(new String[]{"红色","绿色","黄色","粉色"}, 0, new OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1)
{
case 0:
paint.setColor(Color.RED);
break;
case 1:
paint.setColor(Color.GREEN);
break;
case 2:
paint.setColor(Color.YELLOW);
break;
case 3:
paint.setColor(Color.MAGENTA);
break;
}
pen=paint.getColor();
}
});
dialog.setPositiveButton("确定", null).create();
dialog.show();
gameview.invalidate();
break;
case R.id.Paintsize:
paint.setStrokeWidth(5);
gameview.invalidate();
break;
case R.id.background:
canvas.drawColor(Color.DKGRAY);
color=Color.CYAN;
gameview.invalidate();
break;
case R.id.clear:
path=new Path();
gameview.invalidate();
break;
}
return super.onOptionsItemSelected(item);
} } 2.绘图操作的GameView类 public class GameView extends View implements OnTouchListener {
private float mx,my;//用来存手指移动后的坐标
public Bitmap map;//对图像的一些操作
public Canvas canvas;
public Path dpath;//主要用来绘制图像的对象,他会把所有绘制的图像保存到path里面
public Paint paint;//画笔
public int color;
public GameView(Context context,int width,int height) {
super(context);
map=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);//绘制一个与手机屏幕大小的BitMap对象
canvas=new Canvas(map);
paint=new Paint();
paint.setAntiAlias(true);//消除锯齿
paint.setStyle(Style.STROKE);//设置画笔结尾时的样式为圆润
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.SQUARE);
paint.setColor(Color.MAGENTA);
paint.setStrokeWidth(3); this.setOnTouchListener(this);//添加触屏操作
} public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.WHITE);//这里具有清屏的意思
color=Color.DKGRAY;
canvas.drawBitmap(map, 0,0, paint);//从 0 0开始到map中的width,height的一块画布,也就是全屏幕的一块画布 //保存上次绘制的路径,只要他不为空就绘制出上次绘制的图形
if(dpath!=null)
{
canvas.drawPath(dpath, paint);
}
} public void touch_start(float x,float y)
{
dpath.moveTo(x, y);//设置绘制图形的起始点
}
public void touch_move(float x,float y)
{
// 从x1,y1到x2,y2画一条贝塞尔曲线,更平滑(直接用mPath.lineTo也是可以的)
dpath.lineTo(x,y);
mx = x;//把手指移动后的坐标存放到mx,my中,
my = y;
}
public void touch_up()
{
dpath.lineTo(mx, my);//设置手指从按下到移动过后之间绘制一条线存放到path中,之后通过下面的方法绘制到屏幕上
canvas.drawPath(dpath, paint);
}
public boolean onTouch(View arg0, MotionEvent event)
{
float x = event.getX()获得手指放下的坐标
float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
// 每次down下去重新new一个Path,这是一个要注意的地方,每一次手指从触摸屏幕到离开屏幕,都是一次新的path路径,所以要重新绘制一个新的Path对象
dpath = new Path();
touch_start(x, y);//设置起始点
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
//invalidate();
break;
}
return true;
} }
05-11 15:09