手指在屏幕上触动的时候,用private 私人方法fingerMove,会记录下所运动的轨迹或者方法所规定的形状,处理手指的动作,这是让手指在画布activity的界面处理手指所留下的坐标,将所有坐标的点组成的线就是所画出来的图形。
private void reDraw() {
invalidate(); } /**
* 处理手指的动作。 * @param x
* X坐标 * @param y
* Y坐标 */
private void fingerUp(float x, float y) {
this.tempX = 0; this.tempY = 0;
drawing.fingerUp(x, y, paper); this.isMoving = false; } /**
* 处理手指的动作 * @param x
* @param y */
private void fingerMove(float x, float y) {
this.tempX = x; this.tempY = y;
this.isMoving = true;
drawing.fingerMove(x, y, paper); } /**
* 处理手指的动作 * @param x * @param y */
private void fingerDown(float x, float y) {
this.isMoving = false;
drawing.fingerDown(x, y, paper); }
如果处理好一张图,需要保存,调用公用public方法saveBitmap,对画好的图进行保存,这个时候,软件会检测是否有保存的介质存在,如果不存在,就保存不了,有可能还会引起软件的崩溃;如果检测到存在保存介质,编辑图片名字进行保存就可以,如果没有名字,软件默认给一个名字,进行保存。
/**
* 检查SD卡是否可用 */
public void saveBitmap() {
String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) {
saveToSdcard(); }
else if
(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
Toast.makeText(this.context,
getResources().getString(R.string.tip_sdcard_is_read_only), Toast.LENGTH_LONG).show(); } else {
Toast.makeText(
this.context,
getResources().getString(
R.string.tip_sdcard_is_not_available), Toast.LENGTH_LONG).show(); } }
public void changeBgColor(int color) {
this.paper.drawColor(color); this.reDraw(); }
当检查到储存介质是存在的,即私人的private 方法saveToSdcard来执行,在SD卡中创建属于软件的保存文件夹,访问路径由“/”符号来分开,保存时间为系统时间,保存的图片格式为".png"格式,最后就是将图片进行储存。 /**
* 保存位图在SD卡上 */
private void saveToSdcard() {
File sdcard_path =
Environment.getExternalStorageDirectory();
String myFloder = getResources().getString( R.string.folder_name_in_sdcard); File paintpad = new File(sdcard_path + "http://www.fanwen99.cn/" + myFloder + "http://www.fanwen99.cn/"); try {
if (!paintpad.exists()) {
paintpad.mkdirs(); } }
catch (Exception e) {
e.printStackTrace(); }
String timeStamp =
(DateFormat.format("yyyy.MM.dd.hh.mm.ss",
new java.util.Date())).toString(); String suffixName = ".png"; String fullPath = "";
fullPath = sdcard_path + "http://www.fanwen99.cn/" + myFloder + "http://www.fanwen99.cn/" + timeStamp + suffixName; try {
Toast.makeText(this.context,
getResources().getString(R.string.tip_save_to) + fullPath,
Toast.LENGTH_LONG).show();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(fullPath)); }
catch (FileNotFoundException e) {
Toast.makeText(
this.context,
getResources().getString(R.string.tip_sava_failed)
+ fullPath, Toast.LENGTH_LONG).show(); e.printStackTrace(); } }
利用软件就可以画出你想要的简笔画,比如像菠萝一样的水果,哆啦A梦一样的卡通人物,还有很多很多,只要你想的到的都可以利用它画出来。创建的界面如图1所示,画出的菠萝如图2所示,画出的哆啦A梦如图所示。
图1,打开软件 图
2,用画笔画菠萝
图3,用画笔画哆啦A梦
只有画画远远不是我们要的要的功能,当然还有几乎所有软件都具备的一个功能,设置。菜单中含有形状、清除、保存、设置。进入设置,有很多关于画画
的各种功能。画笔的宽度pen_width为默认初始宽度,画笔的颜色pen_color为默认颜色,画布的颜色为空即是白色,画画的风格为初始默认状态,以下是进入设置的部分代码。图4是菜单现实设置的界面。图5是进入设置之后的界面,有画刷宽度、颜色等功能的选择。
import java.text.SimpleDateFormat;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.vhow.paintpad.R;
import org.vhow.paintpad.helper.ColorPickerDialog;
import org.vhow.paintpad.tools.Brush;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity;
/**
*当用户选择设置按钮,就跳转到这个activity。
*当用户点击的时候、实现OnPreferenceClickListener接口。
*优先调用onPreferenceClick()。
*/
public class SettingsActivity extends PreferenceActivity
implements
OnPreferenceClickListener,
ColorPickerDialog.OnColorChangedListener
{
SharedPreferences prefs = null;
Preference pen_width = null;
Preference pen_color = null;
Preference canvas_bg_color = null;
String pen_width_key;
String pen_color_key;
CheckBoxPreference pen_style = null;
Preference versionName;
Preference lastBuildTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/**
*XML对这个activity的虚拟设置
*/
addPreferencesFromResource(R.xml.settings);
pen_width_key =
getResources().getString(R.string.pen_width_key);
pen_color_key =
getResources().getString(R.string.pen_color_key);
/**
*找到要处理的对象。
*/
pen_width = (Preference)
findPreference(getResources().getString(
R.string.pen_width_key));
pen_color = (Preference)
findPreference(getString(R.string.pen_color_key));
versionName = (Preference)
findPreference(getString(R.string.setting_about_version_key)); lastBuildTime = (Preference)
findPreference(getString(R.string.setting_about_build_key));
versionName.setSummary(getVersionName());
lastBuildTime.setSummary(getLastBuiltTime());
/**
*注册监听器。包含画刷宽度,画刷颜色。
*/
pen_width.setOnPreferenceClickListener(this);
pen_color.setOnPreferenceClickListener(this);
}
private String getLastBuiltTime()
{
String lastBuiltTimeString = "Unknown";
try
{
ApplicationInfo ai =
getPackageManager().getApplicationInfo(
getPackageName(), 0);
ZipFile zf = new ZipFile(ai.sourceDir);
ZipEntry ze = zf.getEntry("classes.dex");
long time = ze.getTime();
lastBuiltTimeString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new java.util.Date(time));
}
catch (Exception e)
{
//最后编译时间是“未知”.
}
return lastBuiltTimeString;
}
private String getVersionName()
{
String versionName = "Unknown";
try
{
versionName = this.getPackageManager().getPackageInfo( getPackageName(), 0).versionName;
}
catch (NameNotFoundException e)
{
//最后的版本名是“未知”。
}
return versionName;
}
/**
* 处理该事件的优先权
*/
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals(pen_color_key))
{
String str =
getResources().getString(R.string.tip_choose_color);
new ColorPickerDialog(this, this,
Brush.getPen().getColor(), str)
.show();
}
else if (preference.getKey().equals(pen_width_key)) {
showSetBrushWidthDialog();
}
return true;
}
/**
*弹出一个对话框SeekBar以控制画笔的宽度
*/
private void showSetBrushWidthDialog()
{
SeekBarDialog seekBarDialog = new SeekBarDialog(this); seekBarDialog.setTitle(Brush.getPen().getStrokeWidth() + " 像素");
seekBarDialog.setButton(
getResources().getString(R.string.alert_dialog_ok),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
}
});
seekBarDialog.show();
}
/**
*当画刷颜色改变时,这个方法将被调用。
*/
public void colorChanged(int color)
{
Brush pen = Brush.getPen();
pen.setColor(color);
}
}
图4,打开菜单键
图5,打开菜单中的设置
当在菜单中选择形状之后,就会弹出形状的界面,在形状界面有很多形状供选择,下面可以有几个形状的源代码。当选择需要形状之后就进入到画布,画出的图形就会是选择到的形状。
直线代码如下:
import org.vhow.paintpad.tools.Brush;
import android.graphics.Canvas;
/**
*直线。
*/
public class StraightLine extends Drawing
{
@Override
public void draw(Canvas canvas)
{
canvas.drawLine(this.startX, this.startY, this.stopX, this.stopY,
Brush.getPen());
}
}
矩形代码如下:
import org.vhow.paintpad.tools.Brush;
import android.graphics.Canvas;
/**
*矩形。
*/
public class Rect extends Drawing
{
@Override
public void draw(Canvas canvas)
{
canvas.drawRect(this.startX, this.startY, this.stopX, this.stopY,
Brush.getPen());
}
}
轨迹线代码如下:
import org.vhow.paintpad.tools.Brush;
import android.graphics.Canvas;
import android.graphics.Path;
/**
*跟踪手指在屏幕上运动。
*/
public classextends Drawing
{
Path mPath = null;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
public PathLine()
{
mPath = new Path();
}
@Override
public void draw(Canvas canvas)
{
canvas.drawPath(this.mPath, Brush.getPen());
}
@Override
public void fingerDown(float x, float y, Canvas canvas) {
mPath.reset();
mPath.moveTo(x, y);
this.mX = x;
this.mY = y;
}
@Override
public void fingerMove(float x, float y, Canvas canvas) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
}
mX = x; mY = y; } this.draw(canvas); } @Override public void fingerUp(float x, float y, Canvas canvas) { mPath.lineTo(mX, mY); this.draw(canvas); mPath.reset(); }
图6,打开菜单中的形状功能
在设置中就可以进行颜色选择,颜色的设置用public公有类
ColorPickerDialog进行设置,定义颜色改变的方法colorChanged,下面是颜色设置的代码,图7是进入到颜色设置的界面。
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
importimport android.view.MotionEvent;
import android.view.View;
public class ColorPickerDialog extends Dialog
{
String title;
public interface OnColorChangedListener
{
void colorChanged(int color);
}
private OnColorChangedListener mListener;
private int mInitialColor;
private static class ColorPickerView extends View
{
private Paint mPaint;
private Paint mCenterPaint;
private final int[] mColors;
private OnColorChangedListener mListener;
ColorPickerView(Context c, OnColorChangedListener l, int color)
{
super(c);
mListener = l;