您好,登錄后才能下訂單哦!
這篇“Android Studio怎么實現(xiàn)簡單繪圖板”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android Studio怎么實現(xiàn)簡單繪圖板”文章吧。
目的
設(shè)計一個手繪圖形的畫板
工具及環(huán)境
使用java語言,在Android studio平臺上進行開發(fā)
功能設(shè)計
實現(xiàn)一個可以繪圖的畫板,界面有相關(guān)的選擇按鈕。可以根據(jù)按鈕切換畫筆的顏色,刷子可以加粗畫筆的線條大小,橡皮可以用于抹除已經(jīng)繪制的圖案,清屏可實現(xiàn)清屏重置畫板
設(shè)計思路
首先設(shè)計界面,然后設(shè)計按鈕點擊功能。橡皮擦的功能可通過把畫筆顏色設(shè)置與背景顏色一致來實現(xiàn),清屏功能可通過背景重置覆蓋原背景實現(xiàn)
代碼
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.xdw.exercise.HandWrite android:id="@+id/handwriteview" android:layout_width="fill_parent" android:layout_height="600dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:layout_weight="1" android:text="紅色" /> <Button android:id="@+id/blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:layout_weight="1" android:text="藍色" /> <Button android:id="@+id/brush" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:layout_weight="1" android:text="刷子" /> <Button android:id="@+id/eraser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:layout_weight="1" android:text="橡皮" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:layout_weight="1" android:text="清屏" /> </LinearLayout> </LinearLayout>
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 = 0,startY = 0; float clickX = 0,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, 0,null); } 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(); } } }
效果顯示:
以上就是關(guān)于“Android Studio怎么實現(xiàn)簡單繪圖板”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。