SoFunction
Updated on 2025-03-11

Android implements hand-drawing function

The examples in this article share with you the specific code for Android to implement the hand-drawn function for your reference. The specific content is as follows

The layout file is as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
 xmlns:app="/apk/res-auto"
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="">
 
 <ImageView
  android:
  android:layout_width="1200px"
  android:layout_height="1500px"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentStart="true" />
 
 <LinearLayout
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_gravity="center_horizontal"
  android:orientation="horizontal">
 
 </LinearLayout>
 
 <Button
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentEnd="true"
  android:layout_marginEnd="79dp"
  android:text="Repaint" />
 
 <Button
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBottom="@+id/linearLayout4"
  android:layout_marginStart="91dp"
  android:layout_toEndOf="@+id/linearLayout4"
  android:text="save" />
</RelativeLayout>

The Activity code is as follows, where the color, width and other properties of the line can be modified.

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
 
import ;
import ;
 
public class DrawActivity extends AppCompatActivity {
 private ImageView iv;
 private Bitmap baseBitmap;
 private Button btn_resume;
 private Button btn_save;
 private Canvas canvas;
 private Paint paint;
 
 float radio;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_draw);
  radio = 10;
  iv = (ImageView) findViewById();
  // Initialize a brush with a width of 5 and a color of red  paint = new Paint();
  (radio);
  ();
  iv = (ImageView) findViewById();
  btn_resume = (Button) findViewById(.btn_resume);
  btn_save = (Button) findViewById(.btn_save);
 
  btn_resume.setOnClickListener(click);
  btn_save.setOnClickListener(click);
  (touch);
 }
 
 private  touch = new () {
  // Define the coordinates at which the finger starts to touch  float startX;
  float startY;
 
  @Override
  public boolean onTouch(View v, MotionEvent event) {
   switch (()) {
    // The user presses the action    case MotionEvent.ACTION_DOWN:
     // The memory image is initialized for the first drawing, specifying the background as white     if (baseBitmap == null) {
      baseBitmap = ((),
        (), .ARGB_8888);
      canvas = new Canvas(baseBitmap);
      ();
     }
     // Record the coordinates of the point you start touching     startX = ();
     startY = ();
     break;
    // The movement of user's fingers moving on the screen    case MotionEvent.ACTION_MOVE:
     // Record the coordinates of the point at the moving position     float stopX = ();
     float stopY = ();
 
     Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
       radio += 0.1;
 
       try {
        (1000);
       } catch (InterruptedException e) {
        ();
       }
      }
     });
     ();
 
     (radio);
     //Draw the connection according to the coordinates of the two points     (startX, startY, stopX, stopY, paint);
 
     // Update the location of the start point     startX = ();
     startY = ();
     // Display the image in ImageView     (baseBitmap);
     break;
    case MotionEvent.ACTION_UP:
     radio = 5;
     break;
    default:
     break;
   }
   return true;
  }
 };
 private  click = new () {
 
  @Override
  public void onClick(View v) {
   switch (()) {
    case .btn_save:
     saveBitmap();
     break;
    case .btn_resume:
     resumeCanvas();
     break;
    default:
     break;
   }
  }
 };
 
 /**
   * Save the picture to the SD card
   */
 protected void saveBitmap() {
  try {
   // Save the picture to the SD card   String fileName = "/sdcard/"+() + ".png";
   File file = new File(fileName);
   FileOutputStream stream = new FileOutputStream(file);
   (, 100, stream);
   (, "Save the picture successfully", Toast.LENGTH_SHORT).show();
    // Android device Gallery app will only scan system folders when it is started    // This simulates a media-loaded broadcast to enable saved images to be viewed in Gallery   Intent intent = new Intent();
   (Intent.ACTION_MEDIA_MOUNTED);
   ((Environment
     .getExternalStorageDirectory()));
   sendBroadcast(intent);
  } catch (Exception e) {
   (, "Save the image failed", Toast.LENGTH_SHORT).show();
   ();
  }
 }
 
 // Manually clear the drawing of the artboard and recreate a artboard protected void resumeCanvas() {
  if (baseBitmap != null) {
   baseBitmap = ((),
     (), .ARGB_8888);
   canvas = new Canvas(baseBitmap);
   ();
   (baseBitmap);
   (, "Cleaning the artboard successfully, you can start drawing again", Toast.LENGTH_SHORT).show();
  }
 }
}

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.