Android上的第三方开源DrawableView支持手写,类似于写字板。DrawableView支持改变画笔颜色,画笔线条粗细,画布的手势缩放和拖曳显示部分区域。并最终支持将手绘的图保存到本地。
在github上的项目主页:https://github.com/PaNaVTEC/DrawableView
先把布局文件中写一个DrawableView:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zzw.testdrawableview.MainActivity" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/subWidth"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/sub" /> <Button
android:id="@+id/addWidth"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/add" /> <Button
android:id="@+id/sava"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/sava" /> <Button
android:id="@+id/PaintColor"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_marginRight="50dp"
android:layout_toLeftOf="@+id/sava"
android:background="@drawable/rand" /> <Button
android:id="@+id/undo"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@+id/sava"
android:background="@drawable/ret" /> </RelativeLayout> <me.panavtec.drawableview.DrawableView
android:id="@+id/paintView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1" /> </LinearLayout>

JAVA代码:

 package com.zzw.testdrawableview;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import me.panavtec.drawableview.DrawableView;
import me.panavtec.drawableview.DrawableViewConfig; public class MainActivity extends Activity implements OnClickListener { private DrawableView drawableView;
private DrawableViewConfig config; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); config = new DrawableViewConfig(); drawableView = (DrawableView) findViewById(R.id.paintView); Button addWidth = (Button) findViewById(R.id.addWidth);
Button subWidth = (Button) findViewById(R.id.subWidth);
Button PaintColor = (Button) findViewById(R.id.PaintColor);
Button undo = (Button) findViewById(R.id.undo);
Button sava = (Button) findViewById(R.id.sava); addWidth.setOnClickListener(this);
subWidth.setOnClickListener(this);
PaintColor.setOnClickListener(this);
undo.setOnClickListener(this);
sava.setOnClickListener(this); // 画笔颜色
config.setStrokeColor(Color.RED); // 画布边界
config.setShowCanvasBounds(true); // 设置画笔宽度
config.setStrokeWidth(10.0f); // 缩放
config.setMinZoom(1.0f);
config.setMaxZoom(3.0f); // 画布宽和高
config.setCanvasHeight(3000);
config.setCanvasWidth(2000); drawableView.setConfig(config);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addWidth:
// 设置每次增加10.0的宽度
config.setStrokeWidth(config.getStrokeWidth() + 10);
Toast.makeText(getApplicationContext(),
"当前画笔宽度:" + config.getStrokeWidth(), 0).show();
break; case R.id.subWidth:
// 设置每次减少10.0的宽度
if (config.getStrokeWidth() > 10) {
config.setStrokeWidth(config.getStrokeWidth() - 10);
Toast.makeText(getApplicationContext(),
"当前画笔宽度:" + config.getStrokeWidth(), 0).show();
}
break; case R.id.PaintColor:
// 测试期间,随机生成一种颜色
int r1 = (int) (Math.random() * 256);
int r2 = (int) (Math.random() * 256);
int r3 = (int) (Math.random() * 256);
config.setStrokeColor(Color.argb(255, r1, r2, r3));
Toast.makeText(getApplicationContext(), "颜色生成成功", 0).show();
break; case R.id.undo:
drawableView.undo();
break; case R.id.sava:
try {
savaBitmapToSDCard();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
} // 将用户手绘的DrawableView转化为图片保存到本地系统默认的图片库中。
private void savaBitmapToSDCard() throws IOException { // 从DrawableView获得Bitmap
Bitmap bmp = drawableView.obtainBitmap(); // 获取保存的路径
File parent_path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File f = new File(parent_path.getAbsoluteFile(), "myDrawableView.png");
f.createNewFile();
Log.d("保存路径", f.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close(); Toast.makeText(getApplicationContext(),
"保存成功,保存路径" + f.getAbsolutePath(), 1).show();
} }

默认的,在未发布的debug阶段,DrawableView会在画布上添加一些log日志输出。如果不打算在画布中显示log,可以修改DrawableView的源代码去掉DrawableView默认的log日志。关键代码有两行,在CanvasDrawer的库文件源代码中:

 initLogger();

 ...

 canvasLogger.log(canvas, canvasRect, viewRect, scaleFactor);

将这两行注释掉即可,如图:

画画板--第三方开源--DrawableView的更多相关文章

  1. IOS-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角) 时间:2015-05-06 16:43:34      阅读:533      评论:0      收藏:0      [点我收藏+] ...

  2. iOS开发-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...

  3. 第三方开源库和jar包的区别

    jar包和第三方开源库的根本区别在于,开源库的功能比jar包功能更强大,通过引入库项目可以访问java文件以及该开源库项目下的资源文件,例如图片,layout等文件 jar包中只能放class文件 引 ...

  4. iOS常用第三方开源框架和优秀开发者博客等

    博客收藏iOS开发过程好的开源框架.开源项目.Xcode工具插件.Mac软件.文章等,会不断更新维护,希望对你们有帮助.如果有推荐或者建议,请到此处提交推荐或者联系我. 该文档已提交GitHub,点击 ...

  5. python基础知识8——模块1——自定义模块和第三方开源模块

    模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...

  6. Android Studio 简介及导入 jar 包和第三方开源库方[转]

    原文:http://blog.sina.com.cn/s/blog_693301190102v6au.html Android Studio 简介 几天前的晚上突然又想使用 Android Studi ...

  7. Android 实现图片画画板

    本文主要讲述了Android 实现图片画画板 设计项目布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  8. 开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发

    [原][开源框架]Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位... 时间 2015-01-05 10:08:18 我是程序猿,我为自己代言 原文  http: ...

  9. 粉笔网iPhone端使用的第三方开源库

    粉笔网iPhone端使用的第三方开源库 前言 最近有朋友问我粉笔网 iPhone 端使用了哪些第三方的开源库.我在这儿整理了一下,分享给大家. ASIHttpRequest ASIHttpReques ...

随机推荐

  1. [SQL]动态sql语句基本语法

    动态sql语句基本语法 :普通SQL语句可以用Exec执行 eg: Select * from tableName Exec('select * from tableName') Exec sp_ex ...

  2. ZOJ 3042 City Selection II 【序】【离散化】【数学】

    题意: 输入数据n,m.n代表工厂的数量,m代表城市的数量. 接下来n+m行为工厂和城市的坐标. 规定如图所示方向刮风,工厂的air会污染风向地区的air. 注意,工厂和城市的坐标表示的是从x到x+1 ...

  3. ACM—最大连续子序列(HDOJ1003)

    HDOJ链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 不了解题目的朋友可以先看一下题目,在这里就不再详细介绍了.(文章内容和解题思路不完全相同,方 ...

  4. poj 1328 Radar Installation(nyoj 287 Radar):贪心

    点击打开链接 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43490   Accep ...

  5. [ZOJ 1004] Anagrams by Stack (简单搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求 ...

  6. linux mint konsole终端模拟器 字符之间空格

    最近安装了linux mint 发现里面的终端是:konsole终端模拟器 ,问题是每次输字符随着输入字符越来越多,字符与光标之间的距离也越来越大(看上去像是自动添加了空格一样), 同时在使用vi时, ...

  7. Shell | grep with n following lines

    'foo' sample.txt ➜ dex-method-counts git:(master) ./dex-method-counts ~/Downloads/n.apk | 'hui' hui: ...

  8. css3实现小米商城鼠标移动图片上浮阴影效果

    今天在编程爱好者编码库看见一个好玩的程序,代码如下. <!DOCTYPE html> <html> <head>     <meta charset=&quo ...

  9. Linux input子系统学习总结(二)----Input事件驱动

    Input 事件驱动:  (主要文件 :drivers/input/evdev.c  .  drivers/input/input.h)基于kernel 4.0  一. 关键函数调用顺序: 1.inp ...

  10. React生命周期和虚拟DOM

    一.虚拟DOM 1.React并不直接操作DOM,React中的render方法,返回一个DOM描述,React能够将这个DOM描述与内存中的表现进行比较,然后以最快的方式更新浏览器 2.React实 ...