Android implementation of drawing board case

Time:2019-6-30

This article gives you an example to share the specific code for Android to implement the drawing board, for your reference, the specific content is as follows.

Preparing a layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.it.MainActivity">
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <Button
    Android: text = "modify color"
    android:onClick="color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <Button
    Android: text = "thickness + 1"
    android:onClick="size"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <Button
    Android: text = "save pictures"
    android:onClick="save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

<ImageView
  android:id="@+id/iv_bg"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</LinearLayout>

Core Code

package com.example.it;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

private final String TAG = getClass().getSimpleName();
private ImageView iv_show;
private Bitmap copyBm;
private float x;
private float y;
private Paint paint;
private int paintsize = 1;
private FileOutputStream fos;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // Initialization control
  iv_show = (ImageView) findViewById(R.id.iv_bg);
  // Create a blank bitmap
  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
  copyBm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
  // Create Canvas
  final Canvas canvas = new Canvas(copyBm);
  // Draw pictures using canvas
  paint = new Paint();
  canvas.drawBitmap(bitmap,new Matrix(), paint);
  iv_show.setImageBitmap(copyBm);

  // Set up listening events for brushes to move on the screen
  iv_show.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

      // Get the type of the current event
      int action = event.getAction();
      switch (action){
        case MotionEvent.ACTION_DOWN:
          // If the finger is pressed, record the position of the finger.
          x = event.getX();
          y = event.getY();
          Log.e(TAG,x+"******"+y);
          break;
        case MotionEvent.ACTION_MOVE:
          // If the finger is moving
          float x1 = event.getX();
          float y1 = event.getY();
          canvas.drawLine(x,y,x1,y1, paint);
          x = x1;
          y = y1;
          Log.e(TAG,x1+"**********"+y1);
          break;
        case MotionEvent.ACTION_UP:
          break;
      }
      iv_show.setImageBitmap(copyBm);
      return true;
    }
  });
}

// Modify the color
public void color(View view) {
  paint.setColor(Color.RED);
}

// Modify line thickness
public void size(View view) {
  paintsize+=1;
  paint.setStrokeWidth(paintsize);
}

// Save pictures
public void save(View view) {

  // Set the location of the image to be saved
  File file = new File(Environment.getExternalStorageDirectory(), SystemClock.currentThreadTimeMillis()+".png");
  try {
    fos = new FileOutputStream(file);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }

  // Setting the Quality of Parameter Preservation
  copyBm.compress(Bitmap.CompressFormat.PNG,100,fos);
  // Define an intention to send a broadcast to the system to scan a file at a specified location
  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  intent.setData(Uri.fromFile(file));
  sendBroadcast(intent);
}
}

(3) Authority information

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.