本文实例为大家分享了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();
        }
    }
}

效果显示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。