欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现手绘功能教程

程序员文章站 2022-06-30 13:33:23
布局文件如下。 activity代码如下,其中线的颜色,宽度等属性都可以修改。 package com.example.administrator.mai...
布局文件如下。



 

 

 

activity代码如下,其中线的颜色,宽度等属性都可以修改。

package com.example.administrator.main;

import android.content.intent;
import android.graphics.bitmap;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.net.uri;
import android.os.environment;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.motionevent;
import android.view.view;
import android.widget.button;
import android.widget.imageview;
import android.widget.toast;

import java.io.file;
import java.io.fileoutputstream;

public class drawactivity extends appcompatactivity {
 private imageview iv;
 private bitmap basebitmap;
 private button btn_resume;
 private button btn_save;
 private canvas canvas;
 private paint paint;

 float radio;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_draw);
  radio = 10;
  iv = (imageview) findviewbyid(r.id.iv);
  // 初始化一个画笔,笔触宽度为5,颜色为红色
  paint = new paint();
  paint.setstrokewidth(radio);
  paint.setcolor(color.black);
  iv = (imageview) findviewbyid(r.id.iv);
  btn_resume = (button) findviewbyid(r.id.btn_resume);
  btn_save = (button) findviewbyid(r.id.btn_save);

  btn_resume.setonclicklistener(click);
  btn_save.setonclicklistener(click);
  iv.setontouchlistener(touch);
 }

 private view.ontouchlistener touch = new view.ontouchlistener() {
  // 定义手指开始触摸的坐标
  float startx;
  float starty;

  @override
  public boolean ontouch(view v, motionevent event) {
switch (event.getaction()) {
 // 用户按下动作
 case motionevent.action_down:
  // 第一次绘图初始化内存图片,指定背景为白色
  if (basebitmap == null) {
basebitmap = bitmap.createbitmap(iv.getwidth(),
  iv.getheight(), bitmap.config.argb_8888);
canvas = new canvas(basebitmap);
canvas.drawcolor(color.white);
  }
  // 记录开始触摸的点的坐标
  startx = event.getx();
  starty = event.gety();
  break;
 // 用户手指在屏幕上移动的动作
 case motionevent.action_move:
  // 记录移动位置的点的坐标
  float stopx = event.getx();
  float stopy = event.gety();

  thread t = new thread(new runnable() {
@override
public void run() {
 radio += 0.1;

 try {
  thread.sleep(1000);
 } catch (interruptedexception e) {
  e.printstacktrace();
 }
}
  });
  t.start();

  paint.setstrokewidth(radio);
  //根据两点坐标,绘制连线
  canvas.drawline(startx, starty, stopx, stopy, paint);

  // 更新开始点的位置
  startx = event.getx();
  starty = event.gety();
  // 把图片展示到imageview中
  iv.setimagebitmap(basebitmap);
  break;
 case motionevent.action_up:
  radio = 5;
  break;
 default:
  break;
}
return true;
  }
 };
 private view.onclicklistener click = new view.onclicklistener() {

  @override
  public void onclick(view v) {
switch (v.getid()) {
 case r.id.btn_save:
  savebitmap();
  break;
 case r.id.btn_resume:
  resumecanvas();
  break;
 default:
  break;
}
  }
 };

 /**
  * 保存图片到sd卡上
  */
 protected void savebitmap() {
  try {
// 保存图片到sd卡上
string filename = "/sdcard/"+system.currenttimemillis() + ".png";
file file = new file(filename);
fileoutputstream stream = new fileoutputstream(file);
basebitmap.compress(bitmap.compressformat.png, 100, stream);
toast.maketext(drawactivity.this, "保存图片成功", toast.length_short).show();
 // android设备gallery应用只会在启动的时候扫描系统文件夹
 // 这里模拟一个媒体装载的广播,用于使保存的图片可以在gallery中查看
intent intent = new intent();
intent.setaction(intent.action_media_mounted);
intent.setdata(uri.fromfile(environment
  .getexternalstoragedirectory()));
sendbroadcast(intent);
  } catch (exception e) {
toast.maketext(drawactivity.this, "保存图片失败", toast.length_short).show();
e.printstacktrace();
  }
 }

 // 手动清除画板的绘图,重新创建一个画板
 protected void resumecanvas() {
  if (basebitmap != null) {
basebitmap = bitmap.createbitmap(iv.getwidth(),
  iv.getheight(), bitmap.config.argb_8888);
canvas = new canvas(basebitmap);
canvas.drawcolor(color.white);
iv.setimagebitmap(basebitmap);
toast.maketext(drawactivity.this, "清除画板成功,可以重新开始绘图", toast.length_short).show();
  }
 }
}