網站首頁 編程語言 正文
本文實例為大家分享了Android Studio實現簡單繪圖板的具體代碼,供大家參考,具體內容如下
目的
設計一個手繪圖形的畫板
工具及環境
使用java語言,在Android studio平臺上進行開發
功能設計
實現一個可以繪圖的畫板,界面有相關的選擇按鈕。可以根據按鈕切換畫筆的顏色,刷子可以加粗畫筆的線條大小,橡皮可以用于抹除已經繪制的圖案,清屏可實現清屏重置畫板
設計思路
首先設計界面,然后設計按鈕點擊功能。橡皮擦的功能可通過把畫筆顏色設置與背景顏色一致來實現,清屏功能可通過背景重置覆蓋原背景實現
代碼
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();
? ? ? ? }
? ? }
}
效果顯示:
原文鏈接:https://blog.csdn.net/MofukYXM/article/details/89044636
相關推薦
- 2022-07-02 ASP.NET配置文件中自定義節點_實用技巧
- 2022-06-29 利用python實現你說我猜游戲的完整實例_python
- 2022-08-03 Python類的基本寫法與注釋風格介紹_python
- 2022-03-26 Unity實現坦克模型_C#教程
- 2021-12-13 Linux系統創建TCP連接流程介紹_Linux
- 2021-12-18 死鎖的處理基本策略和常用方法
- 2023-03-05 Redis緩存工具封裝實現_Redis
- 2022-11-17 有意思的數據結構默克樹?Merkle?tree應用介紹_其它綜合
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支