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. lambda表达式、内置函数、进制和文件操作

    lambda表达式 定义函数(普通方式)def f1(): return 123 f2 = lambda : 123 def f3(a1,a2): return a1+a2 定义函数(lambda表达 ...

  2. NYOJ 49-开心的小明:01背包

    点击打开链接 开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是 ...

  3. NSDictionary、NSMutableDictionary的基本用法

    NSDictionary.NSMutableDictionary的基本用法 1.不可变词典NSDictionary 字典初始化 NSNumber *numObj = [NSNumber numberW ...

  4. oracle 事务测试

    此文章是根据官方改变 模拟帐户转账流程 1.JOHN帐户扣除-DAVID帐户增加-记录日志-事务提交 三个操作必须全部完成此事务才完成,否则失败 创建帐户余额表自增字段自增序列:    ; 创建支票表 ...

  5. SQL server 2016 安装步骤

    1.进入安装中心:可以参考硬件和软件要求.可以看到一些说明文档 2.选择全新安装模式继续安装 3.输入产品秘钥:这里使用演示秘钥进行 4.在协议中,点击同意,并点击下一步按钮,继续安装 5.进入全局规 ...

  6. ELK部署

    一.logstash 版本:2.4.0 要求:Java 7 + 下载:https://download.elastic.co/logstash/logstash/logstash-2.4.0.tar. ...

  7. 【Unity Shaders】学习笔记——渲染管线

    [Unity Shaders]学习笔记——Shader和渲染管线 转载请注明出处:http://www.cnblogs.com/-867259206/p/5595924.html 写作本系列文章时使用 ...

  8. Ubuntu 16.04 LTS U盘安装要点

    一.UltraISO UltraISO是一款功能强大而又方便实用的光盘映像文件制作/编辑/转换工具,它可以直接编辑ISO文件和从ISO中提取文件和目录,也可以从CD-ROM制作光盘映像或者将硬盘上的文 ...

  9. hadoop的live node为0

    1.重新格式化namenode cd ~ rm -rf name mkdir name rm -rf hadoop-2.7.2/logs/ mkdir hadoop-2.7.2/logs/ hadoo ...

  10. c#中如何做日期的三元判断(日期不为空赋值)

    <dx:ASPxDateEdit runat="server" ID="edTab4_protocoldate" Width="100%&quo ...