SoFunction
Updated on 2025-03-09

How to make Android drawing board

This article shares the specific code displayed on the Android drawing board for your reference. The specific content is as follows

layout

<RelativeLayout xmlns:andro
  xmlns:tools="/tools" android:
  android:layout_width="match_parent" android:layout_height="match_parent"
  tools:context="">

  <ImageView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bg"
    />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    >
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="red"
      android:onClick="onplay"
      />
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="green"
      android:onClick="onplay"
      />
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="brush"
      android:onClick="onplay"
      />
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="save"
      android:onClick="onplay"
      />
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Paint"
      android:onClick="onplay"
      />
  </LinearLayout>

</RelativeLayout>

Main layout

/*
 artboard canvas artboard paint gesture recognizer
 Overall idea: Because I am painting pictures, I am actually modifying the pictures to achieve the effect of drawing pictures
 1. Original picture, white paper, brush, drawing board
 2. Draw according to gesture recognition

  */
public class MainActivity extends AppCompatActivity {
private Bitmap bitmap;
  private Canvas canvas;
private ImageView iv;
  private int startx;
  private int starty;
  private Paint paint;
  private Bitmap bmSrc;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    //Load the original image    bmSrc = (getResources(), );
    //Create white paper, width, height, and parameters of the picture     bitmap = ((), (), ());
    //Create the artboard, the parameters are white paper objects    canvas = new Canvas(bitmap);
    //Create a brush    paint = new Paint();
    //Draw on paper    iv=(ImageView)findViewById();
    (bmSrc,new Matrix(), paint);
    //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  //Set a gesture adapter for the control to get the gestures made by the user on this control    (new () {


      //When the user's hand is on this control, it refers to the three scenes in which the user's hand slides, presses, and releases the control, and automatically calls back      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (()){
          case MotionEvent.ACTION_DOWN://Callback once when pressed            //Get the coordinates when the user's finger is pressed            startx = (int) ();
            starty = (int) ();
            break;
          case MotionEvent.ACTION_MOVE://Call the time when the fingers slide, call them constantly            int newx = (int) ();
            int newy = (int) ();
            //Draw lines on background            (startx,starty,newx,newy, paint);
            startx=newx;
            starty=newy;
            (bitmap);
            break;
          case MotionEvent.ACTION_UP://Callback once when released
            break;
        }
        //The distribution mechanism        //true:iv handles the touch event        //false: iv does not handle the touch event, the event is passed to the previous level        return true;
      }
    });
  }
  public void onplay(View view){
   switch (()){
     case :
       ();
       break;
     case :
       ();
       break;
     case :
       (5);
       break;
     case :
       if((iv)){
         (this, "Search taken successfully", Toast.LENGTH_SHORT).show();
       }else{
         (this, "Screenshot failed, please check whether sdcard is available", Toast.LENGTH_SHORT).show();
       }
       break;
     //Photos     case :
       (new Rect(0,0,width,height), paint);
       break;
     }

   }
  }

This is a tool for storing SD cards with drawn pictures

public class SaveViewUtil {
  
  private static final File rootDir = new File(()+);

  /**How ​​to save screenshots*/
  public static boolean saveScreen(View view){
   //Judge whether sdcard is available   if(!Environment.MEDIA_MOUNTED.equals(())){
     return false;
   }
   if(!()){
     ();
   }
   (true);
   ();
   Bitmap bitmap = ();
   try {
     (, 100, new FileOutputStream(new File(rootDir,()+".jpg")));
     return true;
   } catch (FileNotFoundException e) {
     ();
     return false;
   }finally{
     (false);
     bitmap = null;
   }
  }
}
<!-- PastSDCardWrite data permissions -->
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.