Create a simple canvas for Android multimedia development and learning
A simple canvas can be drawn, selected, and saved. Of course, this tool is commonly used in communication software, such as QQ and feiqiu.
Among them, we must listen to the touch events of the fingers. The touch events of the fingers are divided into three types: Press, lift, and move. Usually we only need to press the link, and then the entire finger Sliding Process. Then draw different straight lines during finger sliding. Of course, you can also set the color and width of the drawn line.
Public class MainActivity extends Activity {private int TouchX; private int TouchY; private Paint paint; private Canvas canvas; Bitmap bitmapCopy; ImageView lView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // 1: first create a background image of the canvas. In this case, you need to modify the image and create a copy of the image Bitmap bitmapSrc = BitmapFactory. decodeResource (getResources (), R. drawable. bg); bitmapCopy = Bitmap. createBitmap (bitmapSrc. getWidth (), bitmapSrc. getHeight (), bitmapSrc. getConfig (); paint = new Paint (); canvas = new Canvas (bitmapCopy); canvas. drawBitmap (bitmapSrc, new Matrix (), paint); lView = (ImageView) findViewById (R. id. lv); lView. setImageBitmap (bitmapCopy); // 2: You need to listen to the touch event lView of your fingers. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// there are three types of finger touch: Press, lift, and move int action = event. getAction (); switch (action) {case MotionEvent. ACTION_DOWN: // The first step is to press. When you press, you need to record the position TouchX = (int) event at the first press. getX (); TouchY = (int) event. getY (); break; case MotionEvent. ACTION_MOVE: // a moving event is generated every time you move the task. The current coordinate int x = (int) event is obtained. getX (); int y = (int) event. getY (); // draw line canvas. drawLine (TouchX, TouchY, x, y, paint); // you can specify TouchX = x, TouchY = y, and lView. setImageBitmap (bitmapCopy); break; case MotionEvent. ACTION_UP: break;} // true: Tell the system that this touch event is handled by me // false: Tell the system that I will not process this touch event, at this time, the system will pass the touch event to the parent node of the imageview return true ;}) ;}// set the paint brush color to red public void red (View v) {paint. setColor (Color. RED);} // set the paint brush color to green public void green (View v) {paint. setColor (Color. GREEN);} // set the width of the brush public void brush (View v) {paint. setStrokeWidth (7);} // save public void save (View v) {File file = new File (sdcard/tupian.png); FileOutputStream fStream = null; try {fStream = new FileOutputStream (file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // Save As png. 100 represents the highest quality bitmapCopy. compress (CompressFormat. PNG, 100, fStream );}}
The above is a simple drawing board, which can draw simple images and save them.