Canvas类就是表示一块画布,你可以在上面画你想画的东西。当然,你还可以设置画布的属性,如画布的颜色/尺寸等。
Paint 画笔 和Canvas搭配使用,用于指定绘制的颜色, 线条的粗细, 过渡, 渐变等效果.
1 package com.example.day17_04simplepainter; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import android.app.Activity; 6 import android.graphics.Bitmap; 7 import android.graphics.Bitmap.CompressFormat; 8 import android.graphics.Bitmap.Config; 9 import android.graphics.Canvas; 10 import android.graphics.Color; 11 import android.graphics.Paint; 12 import android.os.Bundle; 13 import android.view.MotionEvent; 14 import android.view.View; 15 import android.view.View.OnTouchListener; 16 import android.widget.ImageView; 17 18 public class MainActivity extends Activity implements OnTouchListener { 19 20 private ImageView iv_painter; 21 private Bitmap bitmap; 22 private Canvas canvas; 23 private Paint paint; 24 int startX =0; 25 int startY=0; 26 int stopX=0; 27 int stopY =0; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 iv_painter = (ImageView) findViewById(R.id.iv_painter); 34 iv_painter.setOnTouchListener(this); 35 } 36 37 @Override 38 public boolean onTouch(View v, MotionEvent event) { 39 40 switch (event.getAction()) { 41 case MotionEvent.ACTION_DOWN: 42 43 if (bitmap==null) { 44 bitmap = Bitmap.createBitmap(iv_painter.getWidth(), iv_painter.getHeight(), Config.ARGB_8888); 45 canvas = new Canvas(bitmap); 46 canvas.drawColor(Color.WHITE); 47 paint = new Paint(); 48 paint.setStrokeWidth(10); 49 paint.setColor(Color.GREEN); 50 } 51 startX = (int) event.getX(); 52 startY = (int) event.getY(); 53 System.out.println("MainActivity.onTouch()ACTION_DOWN"); 54 break; 55 56 case MotionEvent.ACTION_MOVE: 57 System.out.println("MainActivity.onTouch()ACTION_MOVE"); 58 59 stopX = (int) event.getX(); 60 stopY = (int) event.getY(); 61 canvas.drawLine(startX, startY, stopX, stopY, paint); 62 iv_painter.setImageBitmap(bitmap); 63 64 startX=stopX; 65 startY=stopY; 66 67 break; 68 69 case MotionEvent.ACTION_UP: 70 break; 71 default: 72 break; 73 } 74 return true; 75 } 76 77 public void save(View v){ 78 FileOutputStream fos; 79 try { 80 fos = new FileOutputStream(getFilesDir()+"/mypaint.jpg"); 81 bitmap.compress(CompressFormat.JPEG, 100, fos); 82 } catch (FileNotFoundException e) { 83 // TODO Auto-generated catch block 84 e.printStackTrace(); 85 } 86 } 87 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.day17_04simplepainter.MainActivity" > 10 11 <ImageView 12 android:id="@+id/iv_painter" 13 android:layout_width="300px" 14 android:layout_height="400px" /> 15 16 <Button 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="save" 20 android:onClick="save" 21 android:layout_alignParentBottom="true"/> 22 </RelativeLayout>