Android实现手绘功能
程序员文章站
2023-11-06 17:32:28
本文实例为大家分享了android实现手绘功能的具体代码,供大家参考,具体内容如下布局文件如下&...
本文实例为大家分享了android实现手绘功能的具体代码,供大家参考,具体内容如下
布局文件如下
<?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="com.example.administrator.main.drawactivity"> <imageview android:id="@+id/iv" android:layout_width="1200px" android:layout_height="1500px" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparentstart="true" /> <linearlayout android:id="@+id/linearlayout4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_gravity="center_horizontal" android:orientation="horizontal"> </linearlayout> <button android:id="@+id/btn_resume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_marginend="79dp" android:text="重绘" /> <button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/linearlayout4" android:layout_marginstart="91dp" android:layout_toendof="@+id/linearlayout4" android:text="保存" /> </relativelayout>
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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 就是老婆出了轨