Android realizes hand drawing function

  • 2021-12-04 19:36:40
  • OfStack

In this paper, we share the specific code of Android to realize hand-painted function for your reference. The specific contents are as follows

The layout file is as follows


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.administrator.main.DrawActivity">
 
 <ImageView
  android:id="@+id/iv"
  android:layout_width="1200px"
  android:layout_height="1500px"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentStart="true" />
 
 <LinearLayout
  android:id="@+id/linearLayout4"
  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:id="@+id/btn_resume"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentEnd="true"
  android:layout_marginEnd="79dp"
  android:text=" Redraw " />
 
 <Button
  android:id="@+id/btn_save"
  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, in which the color, width and other attributes of the line can be modified.


package com.example.administrator.main;
 
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
 
import java.io.File;
import java.io.FileOutputStream;
 
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) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_draw);
  radio = 10;
  iv = (ImageView) findViewById(R.id.iv);
  //  Initialization 1 Brushes with a stroke width of 5 The color is red 
  paint = new Paint();
  paint.setStrokeWidth(radio);
  paint.setColor(Color.BLACK);
  iv = (ImageView) findViewById(R.id.iv);
  btn_resume = (Button) findViewById(R.id.btn_resume);
  btn_save = (Button) findViewById(R.id.btn_save);
 
  btn_resume.setOnClickListener(click);
  btn_save.setOnClickListener(click);
  iv.setOnTouchListener(touch);
 }
 
 private View.OnTouchListener touch = new View.OnTouchListener() {
  //  Defines the coordinates at which the finger begins to touch 
  float startX;
  float startY;
 
  @Override
  public boolean onTouch(View v, MotionEvent event) {
   switch (event.getAction()) {
    //  User press action 
    case MotionEvent.ACTION_DOWN:
     //  No. 1 1 Sub-drawing initializes the memory picture, specifying a white background 
     if (baseBitmap == null) {
      baseBitmap = Bitmap.createBitmap(iv.getWidth(),
        iv.getHeight(), Bitmap.Config.ARGB_8888);
      canvas = new Canvas(baseBitmap);
      canvas.drawColor(Color.WHITE);
     }
     //  Record the coordinates of the point at which the touch begins 
     startX = event.getX();
     startY = event.getY();
     break;
    //  The action of the user's finger moving on the screen 
    case MotionEvent.ACTION_MOVE:
     //  The coordinates of the point where the moving position is recorded 
     float stopX = event.getX();
     float stopY = event.getY();
 
     Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
       radio += 0.1;
 
       try {
        Thread.sleep(1000);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
      }
     });
     t.start();
 
     paint.setStrokeWidth(radio);
     // According to the coordinates of two points, draw the connection line 
     canvas.drawLine(startX, startY, stopX, stopY, paint);
 
     //  Update the location of the starting point 
     startX = event.getX();
     startY = event.getY();
     //  Show the picture to ImageView Medium 
     iv.setImageBitmap(baseBitmap);
     break;
    case MotionEvent.ACTION_UP:
     radio = 5;
     break;
    default:
     break;
   }
   return true;
  }
 };
 private View.OnClickListener click = new View.OnClickListener() {
 
  @Override
  public void onClick(View v) {
   switch (v.getId()) {
    case R.id.btn_save:
     saveBitmap();
     break;
    case R.id.btn_resume:
     resumeCanvas();
     break;
    default:
     break;
   }
  }
 };
 
 /**
  *  Save the picture to SD On the card 
  */
 protected void saveBitmap() {
  try {
   //  Save the picture to SD On the card 
   String fileName = "/sdcard/"+System.currentTimeMillis() + ".png";
   File file = new File(fileName);
   FileOutputStream stream = new FileOutputStream(file);
   baseBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
   Toast.makeText(DrawActivity.this, " Save the picture successfully ", Toast.LENGTH_SHORT).show();
    // Android Equipment Gallery The application only scans system folders at startup time 
    //  Simulation here 1 Media loaded broadcasts, which are used to make saved pictures available in Gallery View in 
   Intent intent = new Intent();
   intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
   intent.setData(Uri.fromFile(Environment
     .getExternalStorageDirectory()));
   sendBroadcast(intent);
  } catch (Exception e) {
   Toast.makeText(DrawActivity.this, " Failed to save picture ", Toast.LENGTH_SHORT).show();
   e.printStackTrace();
  }
 }
 
 //  Manually clear the drawing on the drawing pad and recreate it 1 Sketchpad 
 protected void resumeCanvas() {
  if (baseBitmap != null) {
   baseBitmap = Bitmap.createBitmap(iv.getWidth(),
     iv.getHeight(), Bitmap.Config.ARGB_8888);
   canvas = new Canvas(baseBitmap);
   canvas.drawColor(Color.WHITE);
   iv.setImageBitmap(baseBitmap);
   Toast.makeText(DrawActivity.this, " Clear the drawing board successfully, and you can start drawing again ", Toast.LENGTH_SHORT).show();
  }
 }
}

Related articles: