【安卓开发】Android实现画板

普通画板

添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
package Draw;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.example.myapp_b.R;

public class CanvasActivity extends AppCompatActivity {
    private ImageView iv;
    private Bitmap bitmap;
    private Canvas canvas;
    private Paint paint;
    private float startX=0f;
    private float startY=0f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas);
        iv=findViewById(R.id.iv);
        bitmap=Bitmap.createBitmap(1000,1000,Bitmap.Config.ARGB_8888);
        paint=new Paint();
        canvas=new Canvas(bitmap);
        iv.setImageBitmap(bitmap);
        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action){
                    case MotionEvent.ACTION_DOWN:
                        //按下时记录点击位置
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //移动时获取当前位置,并绘制直线
                        float endX = event.getX();
                        float endY = event.getY();
                        canvas.drawLine(startX,startY,endX,endY,paint);
                        iv.setImageBitmap(bitmap);
                        //将直线终点设置为下一次的起点
                        startX = endX;
                        startY = endY;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
    }

    public void setColor(View view) {
        paint.setColor(Color.RED);
    }

    public void setWidth(View view) {
        paint.setStrokeWidth(10);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="Draw.CanvasActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置画笔颜色"
        android:onClick="setColor"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置画笔宽度"
        android:onClick="setWidth"/>
    <ImageView
        android:background="@drawable/bg_border"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"/>
</LinearLayout>

可以选择颜色、宽度、橡皮擦,可以保存文件到本地

添加权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   

file_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="DRAW" />
</paths>
package Draw;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.myapp_b.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

public class PaintActivity extends AppCompatActivity {
    private ImageView iv;
    private Bitmap bitmap;
    private Canvas canvas;
    private Paint paint;
    private float startX=0f;
    private float startY=0f;
    private Button bt_back,bt_sure;
    private ImageView res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paint);
        iv=findViewById(R.id.iv);
        res=findViewById(R.id.res);
        bt_back=findViewById(R.id.bt_back);
        bt_sure=findViewById(R.id.paint_sure);
        bitmap=Bitmap.createBitmap(1000,1000,Bitmap.Config.ARGB_8888);
        paint=new Paint();
        canvas=new Canvas(bitmap);
        iv.setImageBitmap(bitmap);
        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action){
                    case MotionEvent.ACTION_DOWN:
                        //按下时记录点击位置
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //移动时获取当前位置,并绘制直线
                        float endX = event.getX();
                        float endY = event.getY();
                        canvas.drawLine(startX,startY,endX,endY,paint);
                        iv.setImageBitmap(bitmap);
                        //将直线终点设置为下一次的起点
                        startX = endX;
                        startY = endY;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
    }
    int id;
    public void setColor(View view) {
        //paint.setColor(Color.RED);
        id=0;
        final int [] col={Color.RED,Color.YELLOW,Color.GREEN,Color.BLUE,Color.WHITE,Color.BLACK};
        final String []s={"红色","黄色","绿色","蓝色","白色","黑色"};
        AlertDialog.Builder DanItem = new AlertDialog.Builder(PaintActivity.this);
        DanItem.setTitle("选择想要使用的颜色");
        DanItem.setSingleChoiceItems(s, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                id =which;
            }
        });
        DanItem.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which){
                ShowMessage("恭喜你选择了"+s[id].toString());
                paint.setColor(col[id]);
            }
        });
        DanItem.create().show();
    }
    private void ShowMessage(String str) {
        Toast.makeText(PaintActivity.this, str, Toast.LENGTH_LONG).show();
    }
    public void setWidth(View view) {
        id=0;
        final int [] w={5,10,15,20,25,30,35,40};
        final String []s={"5","10","15","20","25","30"};
        AlertDialog.Builder DanItem = new AlertDialog.Builder(PaintActivity.this);
        DanItem.setTitle("选择想要使用的宽度");
        DanItem.setSingleChoiceItems(s, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                id =which;
            }
        });
        DanItem.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which){
                ShowMessage("恭喜你选择了"+s[id]);
                paint.setStrokeWidth(w[id]);
            }
        });
        DanItem.create().show();
    }

    public void setErase(View view) {
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(30);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void savePng(View view) throws FileNotFoundException {
        System.out.println(saveBitmap(bitmap));
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public String saveBitmap(Bitmap bitmap) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        String paintPath = "";
        str = str + "paint.png";
        String imagePath = Environment.getExternalStorageDirectory()+"/DRAW";
        File file = new File(imagePath, str);
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            //保存绘图文件路径
            paintPath =file.getAbsolutePath();
///  /storage/emulated/0/DRAW/20211202195458paint.png
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return paintPath;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="Draw.PaintActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:id="@+id/paint_sum"
        android:layout_height="wrap_content"
        tools:ignore="InvalidId">

        <Button
            android:id="@+id/paint_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回"
            android:textSize="30dp"></Button>

        <Button
            android:id="@+id/paint_sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:paddingLeft="40dp"
            android:onClick="savePng"
            android:text="确定"
            android:textSize="30dp"></Button>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/paint_sum">

        <Button
            android:id="@+id/bnt_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="setColor"
            android:text="设置画笔颜色" />

        <Button
            android:id="@+id/bnt_width"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/bnt_color"
            android:onClick="setWidth"
            android:text="设置画笔宽度"
            tools:ignore="UnknownId" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/erase"
            android:text="橡皮擦"
            android:onClick="setErase"
            android:layout_toRightOf="@id/bnt_color"
            ></Button>
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/bnt_width" />
    </RelativeLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:id="@+id/res"
        ></ImageView>
</RelativeLayout>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆沙睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值