Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple drawing Board with Android Studio

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Android Studio how to achieve a simple Sketchpad", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Android Studio how to achieve a simple Sketchpad" article.

Purpose

Design a hand-drawn drawing board

Tools and environment

Using java language to develop on the Android studio platform

Function design

To achieve a drawing board, the interface has relevant selection buttons. The color of the brush can be switched according to the button, the brush can thicken the line size of the brush, the eraser can be used to erase the pattern already drawn, and the screen can be cleared to reset the artboard.

Design ideas

First design the interface, and then design the button click function. The function of the eraser can be realized by setting the brush color consistent with the background color, and the screen cleaning function can be realized by resetting the background to cover the original background.

Code

Activity_main.xml

HandWrite.java

Package com.xdw.exercise; import android.content.Context;import android.graphics.*;import android.graphics.Paint.Style;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View; public class HandWrite extends View {Paint paint = null; Bitmap originalBitmap = null; Bitmap new1_Bitmap = null; Bitmap new2_Bitmap = null; float startX = 0Magnum StartY = 0; float clickX = 0Mae ClickY = 0; boolean isMove = true; boolean isClear = false; int color=Color.BLUE Float strokeWidth=10.0f; public HandWrite (Context context, AttributeSet attrs) {super (context, attrs); originalBitmap = BitmapFactory .decodeResource (getResources (), R.drawable.iv) .copy (Bitmap.Config.ARGB_8888,true); new1_Bitmap = Bitmap.createBitmap (originalBitmap);} public void clear () {isClear = true; new2_Bitmap = Bitmap.createBitmap (originalBitmap); invalidate () } public void red () {isClear=false; color=Color.RED;} public void blue () {isClear=false; color=Color.BLUE;} public void brush () {strokeWidth=20.0f;} public void eraser () {color=Color.WHITE; strokeWidth=80.0f;} @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas) Canvas.drawBitmap (HandWriting (new1_Bitmap), 0,0PowerNull);} public Bitmap HandWriting (Bitmap o_Bitmap) {Canvas canvas = null; if (isClear) {canvas = new Canvas (new2_Bitmap);} else {canvas = new Canvas (o_Bitmap);} paint = new Paint (); paint.setStyle (Style.STROKE); paint.setAntiAlias (true) Paint.setColor (color); paint.setStrokeWidth (strokeWidth); if (isMove) {canvas.drawLine (startX, startY, clickX, clickY, paint);} startX = clickX; startY = clickY; if (isClear) {return new2_Bitmap;} return o_Bitmap } @ Override public boolean onTouchEvent (MotionEvent event) {clickX = event.getX (); clickY = event.getY (); if (event.getAction () = = MotionEvent.ACTION_DOWN) {isMove = false; invalidate (); return true } else if (event.getAction () = = MotionEvent.ACTION_MOVE) {isMove = true; invalidate (); return true;} return super.onTouchEvent (event);}}

MainActivity.java

Package com.xdw.exercise; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button; public class MainActivity extends Activity {private HandWrite handWrite = null; Button red,blue,clear,brush,eraser; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); handWrite = (HandWrite) findViewById (R.id.handwriteview) Red = (Button) findViewById (R.id.red); blue= (Button) findViewById (R.id.blue); clear = (Button) findViewById (R.id.clear); brush= (Button) findViewById (R.id.brush); eraser= (Button) findViewById (R.id.eraser); clear.setOnClickListener (new cClick ()); red.setOnClickListener (new rClick ()); blue.setOnClickListener (new bClick ()) Brush.setOnClickListener (new brClick ()); eraser.setOnClickListener (new eClick ());} class cClick implements OnClickListener {public void onClick (View v) {handWrite.clear ();}} class rClick implements OnClickListener {public void onClick (View v) {handWrite.red () } class bClick implements OnClickListener {public void onClick (View v) {handWrite.blue ();}} class brClick implements OnClickListener {public void onClick (View v) {handWrite.brush ();}} class eClick implements OnClickListener {public void onClick (View v) {handWrite.eraser ();}

The effect shows:

The above is about the content of this article on "how to achieve a simple drawing board in Android Studio". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report