Android?Studio實(shí)現(xiàn)簡單繪圖板
本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)簡單繪圖板的具體代碼,供大家參考,具體內(nèi)容如下
目的
設(shè)計(jì)一個(gè)手繪圖形的畫板
工具及環(huán)境
使用java語言,在Android studio平臺(tái)上進(jìn)行開發(fā)
功能設(shè)計(jì)
實(shí)現(xiàn)一個(gè)可以繪圖的畫板,界面有相關(guān)的選擇按鈕??梢愿鶕?jù)按鈕切換畫筆的顏色,刷子可以加粗畫筆的線條大小,橡皮可以用于抹除已經(jīng)繪制的圖案,清屏可實(shí)現(xiàn)清屏重置畫板
設(shè)計(jì)思路
首先設(shè)計(jì)界面,然后設(shè)計(jì)按鈕點(diǎn)擊功能。橡皮擦的功能可通過把畫筆顏色設(shè)置與背景顏色一致來實(shí)現(xiàn),清屏功能可通過背景重置覆蓋原背景實(shí)現(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="藍(lán)色" /> ? ? ? ? <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(); ? ? ? ? } ? ? } }
效果顯示:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 利用反射+try catch實(shí)現(xiàn)sdk按需引入依賴庫的方法
這篇文章主要介紹了Android 利用反射+try catch來實(shí)現(xiàn)sdk按需引入依賴庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)
這篇文章主要介紹了Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android 提交或者上傳數(shù)據(jù)時(shí)的dialog彈框動(dòng)畫效果
我們在使用支付寶支付的時(shí)候會(huì)看到類似這種彈框動(dòng)畫效果,下面通過實(shí)例代碼給大家分享android 提交或者上傳數(shù)據(jù)時(shí)的彈框動(dòng)畫效果,感興趣的的朋友參考下2017-07-07Android實(shí)現(xiàn)為ListView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
這篇文章主要介紹了Android實(shí)現(xiàn)為ListView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景,以實(shí)例形式較為詳細(xì)的分析了界面元素與功能的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02Android PC投屏功能實(shí)現(xiàn)的示例代碼
本篇文章主要介紹了Android PC投屏功能實(shí)現(xiàn)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Android實(shí)現(xiàn)Bitmap位圖旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Bitmap位圖旋轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android4.4 WebAPI實(shí)現(xiàn)拍照上傳功能
這篇文章主要介紹了Android4.4 WebAPI實(shí)現(xiàn)拍照上傳功能,本文給出4.4版本后拍照上傳的具體實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07Android 獲取drawable目錄圖片 并存入指定文件的步驟詳解
這篇文章主要介紹了Android 獲取drawable目錄圖片 并存入指定文件,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03