Several issues to consider when developing a drawing board for Android are as follows:
1 How to draw the screen sketchpad and brush
2 The user's finger touches the screen drawing board to monitor events, and several corresponding state processing problems
3 Save the picture to the SD card, and automatically load the picture just now when the system album is opened
Solution
1 The related methods of BitmapFactor and Bitmap are used to parse a background image, make a copy, and then draw the drawing board and brush on Canvas.
2 Use the system's OnTouchLister to monitor the user's touch screen drawing board events, and the corresponding states for different processing
3 Every time the system receives the SD card ready broadcast, it will traverse all the files and folders of the SD card, and save all the multimedia files traversed into an index in the MediaStore database. size. Every time the gallery is opened, it does not traverse the sd card to obtain the picture, but obtains the picture information from the MediaStore database through the content provider, and then reads the picture. When the system is turned on or click the load sd card button, the system will send the sd card ready broadcast, we can also manually send the ready broadcast to load the pictures we saved in the sd card.
code show as below
1 package com.scywxx.paint; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 import android.net.Uri; 9 import android.os.Bundle; 10 import android.os.Environment; 11 import android.app.Activity; 12 import android.content.Intent; 13 import android.graphics.Bitmap; 14 import android.graphics.Bitmap.CompressFormat; 15 import android.graphics.BitmapFactory; 16 import android.graphics.Canvas; 17 import android.graphics.Color; 18 import android.graphics.Matrix; 19 import android.graphics.Paint; 20 import android.view.Menu; 21 import android.view.MotionEvent; 22 import android.view.View; 23 import android.view.View.OnTouchListener; 24 import android.widget.ImageView; 25 26 public class MainActivity extends Activity { 27 private ImageView iv; 28 private Bitmap bmCopy; 29 private Paint paint; 30 private Canvas canvas; 31 private int startX,startY; 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.activity_main); 36 //1 加载画画板的背景图 37 Bitmap bmSrc =BitmapFactory.decodeResource(getResources(), R.drawable.bg); 38 // 2 Copy the source bitmap object 39 bmCopy = Bitmap.createBitmap(bmSrc.getWidth(),bmSrc.getHeight(),bmSrc.getConfig( )); 40 // 3 Create a brush object 41 paint = new Paint(); 42 canvas = new Canvas(bmCopy); 43 // 4 Start drawing 44 canvas.drawBitmap(bmSrc, new Matrix(), paint); 45 / / 5 Get the resource id of the ImageView object on the interface 46 iv = (ImageView) findViewById(R.id.iv); 47 iv.setImageBitmap(bmCopy); 48 // 6 Set touch listener for the background of the drawing board 49 iv.setOnTouchListener( new OnTouchListener() { 50 // When touching the screen, when a touch event occurs, this method is called 51 @Override 52 public boolean onTouch(View v, MotionEvent event) { 53 int action = event.getAction(); // Get the action to get the event 54 switch (action){ 55 // The user's finger touches the screen 56 case MotionEvent.ACTION_DOWN: 57 // Record start x and start y 58 startX = (int ) event.getX(); 59 startY = ( int ) event.getY(); 60 break ; 61 // User's finger is swiping 62 case MotionEvent.ACTION_MOVE: 63 int x=( int ) event.getX(); 64 int y=( int ) event.getY(); 65 // two points form a line 66 canvas.drawLine(startX, startY, x, y, paint); 67 // the next starting point 68 startX= x ; 69 startY= y; 70 iv.setImageBitmap(bmCopy); 71 break ; 72 // User's finger leaves the screen 73 case MotionEvent.ACTION_UP: 74 break ; 75 76 77 } 78 // true: tell the system that this touch event is handled by me 79 // false : Tell the system that I will not handle this touch event, then the system will pass the touch event to the parent node of the imageview 80 return true ; 81 } 82 }); 83 84 85 86 87 88 } 89 // Set the brush color to red 90 public void red(View v){ 91 paint.setColor(Color.RED); 92 } 93 // Set the brush color to red 94 public void green(View v){ 95 paint.setColor(Color.GREEN); 96 } 97 // Set the thickness of the brush 98 public void change(View v){ 99 paint.setStrokeWidth(7 ); 100 } 101 // Save the picture to the sd card 102 public void save(View v){ 103 File file =new File("sdcard/huahua.png" ); 104 FileOutputStream fos= null ; 105 try { 106 fos = new FileOutputStream(file); 107 } catch (FileNotFoundException e) { 108 e.printStackTrace(); 109 } 110 // Compress the picture and save it 111 bmCopy.compress(CompressFormat.PNG, 100 , fos); 112 // Every time the system receives the SD card ready broadcast, it will traverse all the files and folders of the sd card, and traverse all the multimedia Files are stored in an index in the MediaStore database, this index contains the file name, path, size of the multimedia file 113 //Each time the gallery is opened, it does not traverse the sd card to obtain the picture, but obtains the picture information from the MediaStore database through the content provider, and then reads the picture 114 // When the system is turned on or the button to load the sd card is clicked, the system Will send sd card ready broadcast, we can also send ready broadcast manually 115 Intent intent = new Intent(); 116 intent.setAction(Intent.ACTION_MEDIA_MOUNTED); 117 intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); 118 sendBroadcast(intent); 119 } 120 121 }
The interface layout is as follows
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=".MainActivity" > 10 <ImageView 11 android:id="@+id/iv" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 /> 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:orientation="horizontal" 19 android:layout_alignParentBottom="true" 20 > 21 <Button 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="红色" 25 android:onClick="red"/> 26 <Button 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="绿色" 30 android:onClick="green"/> 31 <Button 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="粗细" 35 android:onClick="change"/> 36 <Button 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:text="保存" 40 android:onClick="save"/> 41 </LinearLayout> 42 43 </RelativeLayout>
The effect is as follows