Android studio implements the drawing board function

Time:2022-8-8
Table of contents
  • Simple overview
  • Implementation process

Simple overview

In daily life, we often have some whimsy, or a picture, or a few symbols. At this time, the camera or typing function cannot be used, and I want to take a note and I can't find a pen by my side. So I wondered if I could make a drawing board on the mobile phone.

renderings

在这里插入图片描述

Implementation process

Project layout is simple

在这里插入图片描述

Let's look at the code: first declare the brush, artboard, and coordinates


public class MainActivity extends AppCompatActivity{

 Paint paint;
 Canvas canvas;
 ImageView imageview;
 Bitmap bitmap,newbitmap;
 TextView tv_stroke;
 int startX, startY, endX, endY;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my_paint_tools);
  LinearLayout ll_layout = findViewById(R.id.ll_layout);
  RadioGroup rg_color = findViewById(R.id.rg_color);

Traverse the radio buttons. When the radio button is selected, get the radio button color and set the brush color to the text color of the current button. Finally, pay attention to setting the brush width, so that the brush width cannot be adjusted back when the eraser is clicked later.


for (int i = 0;i<rg_color.getChildCount();i++){
   RadioButton rb = (RadioButton) rg_color.getChildAt(i);
   rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     if (buttonView.isChecked()){
      paint.setColor(buttonView.getTextColors().getDefaultColor()); 
      paint.setStrokeWidth(5);
     }
    }
   });
  }

First create a blank image and a grey canvas, place the image on top of the canvas

Register the touch listener event to get the coordinates when the mouse is pressed and the coordinates after the mouse is moved. Draw a line between start and end and update canvas image


 imageview.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
     case MotionEvent.ACTION_DOWN:
      Log.i("MyPaintToolsActivity","ACTION_DOWN");

      
      startX = (int) (event.getX()/1.4);
      startY = (int) (event.getY()/1.4);
      break;
     case MotionEvent.ACTION_MOVE:
      Log.i("MyPaintToolsActivity","ACTION_MOVE");

      
      endX = (int) (event.getX()/1.4);
      endY = (int) (event.getY()/1.4);

      canvas.drawLine(startX,startY,endX,endY,paint);
      startX = (int) (event.getX()/1.4);
      startY = (int) (event.getY()/1.4);
      imageview.setImageBitmap(bitmap);
      break;
     case MotionEvent.ACTION_UP:
      Log.i("MyPaintToolsActivity","ACTION_UP");
      break;
    }
    imageview.invalidate();
    return true;
   }
  });

If you clear the screen, just one line of code, the rest is to regenerate a canvas


 Button btn_clear = findViewById(R.id.btn_clear);
  btn_clear.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    canvas.drawColor(0,PorterDuff.Mode.CLEAR);
    bitmap = Bitmap.createBitmap(888,1200,Bitmap.Config.ARGB_8888); 
    canvas = new Canvas(bitmap); 
    canvas.drawColor(Color.argb(100,0,0,0)); 
    paint = new Paint();
    paint.setStrokeWidth(5);
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawBitmap(bitmap,new Matrix(),paint); 
    imageview.setImageBitmap(bitmap);   

   }
  });

Well, the canvas will be wiped here…that is to say white…

Finally look at the page layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
 android:orientation="vertical"
 android:id="@+id/ll_layout">
<!-- tools:context=".MyPaintToolsActivity">-->

 <ImageView

  android:id="@+id/imageview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1" />
 <RadioGroup
  android:background="#747373"
  android:layout_width="match_parent"
  android:orientation="horizontal"
  android:id="@+id/rg_color"
  android:layout_height="wrap_content">

  <RadioButton
   android:id="@+id/rb_red"
   android:layout_width="wrap_content"
   android:layout_height="43dp"
   android:layout_weight="1"
   android:text=&quot;red&quot;
   android:textColor="#FF0000"
   android:textSize="18sp" />

  <RadioButton
   android:id="@+id/rb_green"
   android:layout_width="wrap_content"
   android:layout_height="30dp"
   android:layout_weight="1"
   android:text=&quot;black&quot;
   android:textColor="#000000"
   android:textSize="18sp" />

  <RadioButton
   android:id="@+id/rb_blue"
   android:layout_width="wrap_content"
   android:layout_height="30dp"
   android:layout_weight="1"
   android:text=&quot;white&quot;
   android:textColor="#FFFFFF"
   android:textSize="18sp" />

 </RadioGroup>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="horizontal">
  <Button
   android:id="@+id/btn_clear"
   android:layout_width="wrap_content"
   android:layout_weight="1"
   android:layout_height="wrap_content"
   android:background="#000000"
   android:textColor="#FFFFFF"
   android:textSize="18sp"
   android:text=&quot;clear&quot;/&gt;
  <Button
   android:id="@+id/btn_eraser"
   android:layout_width="wrap_content"
   android:layout_weight="1"
   android:layout_height="wrap_content"
   android:textColor="#FFFFFF"
   android:textSize="18sp"
   android:background="#000000"
   android:text=&quot;erase&quot;/&gt;

 </LinearLayout>

</LinearLayout>

So far, this article about the realization of the sketchpad function in Android studio is introduced here. For more related Android studio sketchpad functions, please search for the previous articles of developpaer or continue to browse the related articles below. I hope you will support developpaer more in the future!