Android-实现简单画图画板

效果如图:



布局文件:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="600px"
        android:layout_height="900px"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="red"
            android:text="红色" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="green"
            android:text="绿色"
             />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="brush"
            android:text="刷子"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="eraser"
            android:text="橡皮擦"
             />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="save"
            android:text="保存" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="cancel"
            android:text="取消" />
    </LinearLayout>

</RelativeLayout>
</span>


MainActivity.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yulongji.android10;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

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


public class MainActivity extends Activity {

    private ImageView iv;
    //原图
    private Bitmap bitsrc;
    //拷贝图
    private Bitmap bitcopy;
    private Canvas canvas;
    private Paint paint;
    private int startX;
    private int startY;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        setBitmap();

        // 设置触摸侦听
        iv.setOnTouchListener(new View.OnTouchListener() {

            // 触摸屏幕时,触摸事件产生时,此方法调用
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    // 用户手指摸到屏幕
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX();
                        startY = (int) event.getY();
                        break;
                    // 用户手指正在滑动
                    case MotionEvent.ACTION_MOVE:
                        int x = (int) event.getX();
                        int y = (int) event.getY();
                        canvas.drawLine(startX, startY, x, y, paint);
                        // 每次绘制完毕之后,本次绘制的结束坐标变成下一次绘制的初始坐标
                        startX = x;
                        startY = y;
                        iv.setImageBitmap(bitcopy);
                        break;
                    // 用户手指离开屏幕
                    case MotionEvent.ACTION_UP:
                        break;

                }
                // true:告诉系统,这个触摸事件由我来处理
                // false:告诉系统,这个触摸事件我不处理,这时系统会把触摸事件传递给imageview的父节点
                return true;
            }
        });
    }

    public void setBitmap() {
        // 加载画画板的背景图
		bitsrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        // 创建图片副本
        // 1.在内存中创建一个与原图一模一样大小的bitmap对象,创建与原图大小一致的白纸
        bitcopy = Bitmap.createBitmap(bitsrc.getWidth(), bitsrc.getHeight(),
                bitsrc.getConfig());
        // 2.创建画笔对象
        paint = new Paint();
        // 3.创建画板对象,把白纸铺在画板上
        canvas = new Canvas(bitcopy);
        // 4.开始作画,把原图的内容绘制在白纸上
        canvas.drawBitmap(bitsrc, new Matrix(), paint);
        // 5.将绘制的图放入imageview中
        iv.setImageBitmap(bitcopy);
    }

    public void red(View view){
        paint.setColor(Color.RED);
    }
    public void green(View view){
        paint.setColor(Color.GREEN);
    }
    public void brush(View view){
        paint.setStrokeWidth(8);
    }
    public void cancel(View view){
        setBitmap();
    }
    public void eraser(View view){
        paint.setColor(Color.rgb(243,243,243));

        paint.setStrokeWidth(30);
    }

    public void save(View view){
        String path = Environment.getExternalStorageDirectory() + "/" + "2.png";
        File file = new File(path);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitcopy.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 发送sd卡就绪广播
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
        intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
        sendBroadcast(intent);
    }

}
</span>




  • 6
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yu-Knight

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

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

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

打赏作者

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

抵扣说明:

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

余额充值