1 public class MainActivity extends Activity 2 { 3 int width,height; 4 private GameView gameview; 5 private Canvas canvas; 6 private Bitmap map; 7 private Paint paint; 8 public Path path; 9 private int color,pen; 10 private boolean isclear=true; 11 @SuppressWarnings("deprecation") 12 public void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 15 //获得手机屏幕的宽度和高度 16 width=getWindowManager().getDefaultDisplay().getWidth(); 17 height=getWindowManager().getDefaultDisplay().getHeight(); 18 19 //设置手机为全屏模式 20 requestWindowFeature(Window.FEATURE_NO_TITLE); 21 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 22 23 gameview=new GameView(this, width, height); 24 25 setContentView(gameview); 26 canvas=gameview.canvas; 27 map=gameview.map; 28 paint=gameview.paint; 29 path=gameview.dpath; 30 color=gameview.color; 31 } 32 33 //创建菜单 34 public boolean onCreateOptionsMenu(Menu menu) { 35 getMenuInflater().inflate(R.menu.activity_main, menu); 36 return true; 37 } 38 39 //添加菜单事件监听 40 @Override 41 public boolean onOptionsItemSelected(MenuItem item) { 42 switch (item.getItemId()) { 43 case R.id.save: 44 try 45 { 46 Random random=new Random(); 47 int a=random.nextInt(100);//产生100以内的数据 48 File f=new File(Environment.getExternalStorageDirectory().getPath()+"/bitmap"); 49 f.mkdir(); 50 File file=new File(f,a+".jpg"); 51 FileOutputStream outstream=new FileOutputStream(file); 52 53 map.compress(Bitmap.CompressFormat.PNG, 90, outstream); 54 //把bitmap对象保存一张jpg的图片 55 56 Toast.makeText(this, "保存成功!", Toast.LENGTH_LONG).show(); 57 } 58 catch (FileNotFoundException e) 59 { 60 e.printStackTrace(); 61 } 62 break; 63 case R.id.Color: 64 if(path!=null) 65 { 66 canvas.drawPath(path, paint); 67 gameview.invalidate(); 68 } 69 Builder dialog=new AlertDialog.Builder(this); 70 dialog.setSingleChoiceItems(new String[]{"红色","绿色","黄色","粉色"}, 0, new OnClickListener() 71 { 72 public void onClick(DialogInterface arg0, int arg1) { 73 switch (arg1) 74 { 75 case 0: 76 paint.setColor(Color.RED); 77 break; 78 case 1: 79 paint.setColor(Color.GREEN); 80 break; 81 case 2: 82 paint.setColor(Color.YELLOW); 83 break; 84 case 3: 85 paint.setColor(Color.MAGENTA); 86 break; 87 } 88 pen=paint.getColor(); 89 } 90 }); 91 dialog.setPositiveButton("确定", null).create(); 92 dialog.show(); 93 gameview.invalidate(); 94 break; 95 case R.id.Paintsize: 96 paint.setStrokeWidth(5); 97 gameview.invalidate(); 98 break; 99 case R.id.background: 100 canvas.drawColor(Color.DKGRAY); 101 color=Color.CYAN; 102 gameview.invalidate(); 103 break; 104 case R.id.clear: 105 path=new Path(); 106 gameview.invalidate(); 107 break; 108 } 109 return super.onOptionsItemSelected(item); 110 } 111 112 } 113 114 115 2.绘图操作的GameView类 116 117 public class GameView extends View implements OnTouchListener { 118 private float mx,my;//用来存手指移动后的坐标 119 public Bitmap map;//对图像的一些操作 120 public Canvas canvas; 121 public Path dpath;//主要用来绘制图像的对象,他会把所有绘制的图像保存到path里面 122 public Paint paint;//画笔 123 public int color; 124 public GameView(Context context,int width,int height) { 125 super(context); 126 map=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);//绘制一个与手机屏幕大小的BitMap对象 127 canvas=new Canvas(map); 128 paint=new Paint(); 129 paint.setAntiAlias(true);//消除锯齿 130 paint.setStyle(Style.STROKE);//设置画笔结尾时的样式为圆润 131 paint.setStrokeJoin(Paint.Join.ROUND); 132 paint.setStrokeCap(Paint.Cap.SQUARE); 133 paint.setColor(Color.MAGENTA); 134 paint.setStrokeWidth(3); 135 136 this.setOnTouchListener(this);//添加触屏操作 137 } 138 139 public void draw(Canvas canvas) { 140 super.draw(canvas); 141 canvas.drawColor(Color.WHITE);//这里具有清屏的意思 142 color=Color.DKGRAY; 143 canvas.drawBitmap(map, 0,0, paint);//从 0 0开始到map中的width,height的一块画布,也就是全屏幕的一块画布 144 145 //保存上次绘制的路径,只要他不为空就绘制出上次绘制的图形 146 if(dpath!=null) 147 { 148 canvas.drawPath(dpath, paint); 149 } 150 } 151 152 public void touch_start(float x,float y) 153 { 154 dpath.moveTo(x, y);//设置绘制图形的起始点 155 } 156 public void touch_move(float x,float y) 157 { 158 // 从x1,y1到x2,y2画一条贝塞尔曲线,更平滑(直接用mPath.lineTo也是可以的) 159 dpath.lineTo(x,y); 160 mx = x;//把手指移动后的坐标存放到mx,my中, 161 my = y; 162 } 163 public void touch_up() 164 { 165 dpath.lineTo(mx, my);//设置手指从按下到移动过后之间绘制一条线存放到path中,之后通过下面的方法绘制到屏幕上 166 canvas.drawPath(dpath, paint); 167 } 168 public boolean onTouch(View arg0, MotionEvent event) 169 { 170 float x = event.getX()获得手指放下的坐标 171 float y = event.getY(); 172 switch (event.getAction()) 173 { 174 case MotionEvent.ACTION_DOWN: 175 // 每次down下去重新new一个Path,这是一个要注意的地方,每一次手指从触摸屏幕到离开屏幕,都是一次新的path路径,所以要重新绘制一个新的Path对象 176 dpath = new Path(); 177 touch_start(x, y);//设置起始点 178 invalidate(); 179 break; 180 case MotionEvent.ACTION_MOVE: 181 touch_move(x, y); 182 invalidate(); 183 break; 184 case MotionEvent.ACTION_UP: 185 touch_up(); 186 //invalidate(); 187 break; 188 } 189 return true; 190 } 191 192 }