Android实现简单画图画板

 更新时间:2021年01月05日 08:43:19   作者:yu-Knight  
这篇文章主要为大家详细介绍了Android实现简单画图画板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现简单画图画板的具体代码,供大家参考,具体内容如下

效果如图:

布局文件:

<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>

MainActivity.java

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);
 }
 
}

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

相关文章

  • Android 如何获取设备唯一标识

    Android 如何获取设备唯一标识

    这篇文章主要介绍了Android 如何获取设备唯一标识,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-03-03
  • Android自定义View实现shape图形绘制

    Android自定义View实现shape图形绘制

    这篇文章主要为大家详细介绍了Android使用自定义View实现shape图形绘制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • 解决EditText不显示光标的三种方法(总结)

    解决EditText不显示光标的三种方法(总结)

    下面小编就为大家带来一篇解决EditText不显示光标的三种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Kotlin协程低级api startCoroutine与ContinuationInterceptor

    Kotlin协程低级api startCoroutine与ContinuationInterceptor

    这篇文章主要为大家介绍了Kotlin协程低级api startCoroutine与ContinuationInterceptor示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android中TelephonyManager类的用法案例详解

    Android中TelephonyManager类的用法案例详解

    这篇文章主要介绍了Android中TelephonyManager类的用法,以获取Android手机硬件信息为例详细分析了TelephonyManager类的使用技巧,需要的朋友可以参考下
    2015-09-09
  • Android ListView之setEmptyView正确使用方法

    Android ListView之setEmptyView正确使用方法

    这篇文章主要介绍了Android ListView之setEmptyView正确使用方法的相关资料,希望通过本文能帮助到大家使用该方法,需要的朋友可以参考下
    2017-09-09
  • Android中使用自定义ViewGroup的总结

    Android中使用自定义ViewGroup的总结

    本篇文章主要介绍了Android中使用自定义ViewGroup的总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • 实例讲解建立Android项目

    实例讲解建立Android项目

    在本篇内容中我们给大家整理了关于建立Android项目的步骤和相关知识点,有兴趣的读者们学习下。
    2019-03-03
  • Android圆角设置方法看着一篇文章就够了

    Android圆角设置方法看着一篇文章就够了

    我们在实际工作中,android经常有需要实现圆角的场景,下面这篇文章主要给大家介绍了关于Android圆角设置方法的相关资料,文中通过实例代码介绍的非常详细需要的朋友可以参考下
    2023-02-02
  • 浅谈谈Android 图片选择器

    浅谈谈Android 图片选择器

    近段时间有项目要求写一个类似于微信发送图片时,用来选择照片的一个图片浏览器。相信有很多网友也有这样的需求,这里分享给大家
    2015-12-12

最新评论