/*
* Copyright (C) 2007 The Android Open Source Project
* Copyright (C) 2010-2014 Freescale Semiconductor, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cinread.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
//import com.cinread.note.utils.Log;
//created by pengjf 16-6-18
public class MyView extends View {
private final static String TAG = "MyView";
/**
* 位图,所有的轨迹都是先通过mCanvas绘制到这上面的
*/
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
private Path mPath;
boolean mStopDrawing = false; //update command
boolean mStartDrawing = true; //first point
private static final int UPDATE_MSG = 1;
private static final int UPDATE_DELAY = 20;
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
/**
* 初始化,View宽高等于屏幕的宽高
*
* @param context
* @param attrs
* @param defStyle
*/
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* 初始化画笔 mCanvas , 路径 mPath , 位图 mBitmap
*/
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(0xFF000000);
mPaint.setStrokeWidth(5);
mPath = new Path();
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
Log.d("fmi", "===== onSizeChanged ==== widthPixels = " + displayMetrics.widthPixels + " , heightPixels = " +
displayMetrics.heightPixels);
// 创建屏幕大小的位图,在上面绘制路径
Bitmap newBitmap = Bitmap.createBitmap(displayMetrics.widthPixels, displayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (mBitmap != null) {
newCanvas.drawBitmap(mBitmap, 0, 0, null);
}
/**
* mCanvas 一直在mBitmap上面绘制,所以最终要现实到界面需要在onDraw 方法中绘制mBitmap才能显示出来!!
*/
mBitmap = newBitmap;
mCanvas = newCanvas;
}
/**
* 将mBitmap绘制到当前View
* 所有的手势轨迹已经通过mCanvas绘制在mBitmap上,但是最终要在界面上显示
* 需要通过View自身的 canvas 绘制才能显示!!
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, null);
}
/**
* 将触摸路径通过mCanvas绘制到mBitmap上,刷新的频率由 mhandler递归发送消息的间隔决定
* 默认 UPDATE_DELAY = 20 即 刷新50次/秒
*/
public synchronized void update() {
if (mCanvas != null) {
mCanvas.drawPath(mPath, mPaint);
invalidate(); // 将 mBitmap 绘制到View上
}
}
/**
* 开始绘制
*/
void startUpdating() {
mStopDrawing = false;
mHandler.removeMessages(UPDATE_MSG);
mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_MSG), UPDATE_DELAY);
}
/**
* 停止绘制
*/
void stopUpdating() {
resetState();
mHandler.removeMessages(UPDATE_MSG);
}
/**
* 将标记变量恢复到初始状态
*/
public void resetState() {
// 清空已绘制的路径
mPath.reset();
// 恢复开始绘制
mStartDrawing = true;
mStopDrawing = false;
}
private synchronized void drawPoint(int x, int y) {
if (mCanvas != null) {
if (mStartDrawing == true) {
mStartDrawing = false;
mPath.reset();
mPath.moveTo(x, y);
}
mPath.lineTo(x, y);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startUpdating();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
mStopDrawing = true; // 停止记录和绘制
break;
}
int N = event.getHistorySize();
for (int i = 0; i < N; i++) {
drawPoint((int) event.getHistoricalX(i), (int) event.getHistoricalY(i));
}
drawPoint((int) event.getX(), (int) event.getY());
return true;
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_MSG: {
update();
if (mStopDrawing == true) {
Log.d(TAG, ("end update 2"));
stopUpdating();
} else {
// 50次/秒 发送消息刷新View
mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_MSG), UPDATE_DELAY);
}
break;
}
default:
super.handleMessage(msg);
}
}
};
/**
* View创建时自动生成对应尺寸的 位图 mBitmap
*
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//Log.d("fmi", "===== onSizeChanged ==== w = " + w + " , h = " + h);
//
//
//
//DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
//Log.d("fmi", "===== onSizeChanged ==== widthPixels = " + displayMetrics.widthPixels + " , heightPixels = " +
// displayMetrics.heightPixels);
//
//Bitmap newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
//Canvas newCanvas = new Canvas();
//newCanvas.setBitmap(newBitmap);
//if (mBitmap != null) {
// newCanvas.drawBitmap(mBitmap, 0, 0, null);
//}
///**
// * mCanvas 一直在mBitmap上面绘制,所以最终要现实到界面需要在onDraw 方法中绘制mBitmap才能显示出来!!
// */
//mBitmap = newBitmap;
//mCanvas = newCanvas;
//mCanvas.drawColor(0x00FF0000);
}
}
* Copyright (C) 2007 The Android Open Source Project
* Copyright (C) 2010-2014 Freescale Semiconductor, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cinread.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
//import com.cinread.note.utils.Log;
//created by pengjf 16-6-18
public class MyView extends View {
private final static String TAG = "MyView";
/**
* 位图,所有的轨迹都是先通过mCanvas绘制到这上面的
*/
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
private Path mPath;
boolean mStopDrawing = false; //update command
boolean mStartDrawing = true; //first point
private static final int UPDATE_MSG = 1;
private static final int UPDATE_DELAY = 20;
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
/**
* 初始化,View宽高等于屏幕的宽高
*
* @param context
* @param attrs
* @param defStyle
*/
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* 初始化画笔 mCanvas , 路径 mPath , 位图 mBitmap
*/
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(0xFF000000);
mPaint.setStrokeWidth(5);
mPath = new Path();
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
Log.d("fmi", "===== onSizeChanged ==== widthPixels = " + displayMetrics.widthPixels + " , heightPixels = " +
displayMetrics.heightPixels);
// 创建屏幕大小的位图,在上面绘制路径
Bitmap newBitmap = Bitmap.createBitmap(displayMetrics.widthPixels, displayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (mBitmap != null) {
newCanvas.drawBitmap(mBitmap, 0, 0, null);
}
/**
* mCanvas 一直在mBitmap上面绘制,所以最终要现实到界面需要在onDraw 方法中绘制mBitmap才能显示出来!!
*/
mBitmap = newBitmap;
mCanvas = newCanvas;
}
/**
* 将mBitmap绘制到当前View
* 所有的手势轨迹已经通过mCanvas绘制在mBitmap上,但是最终要在界面上显示
* 需要通过View自身的 canvas 绘制才能显示!!
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, null);
}
/**
* 将触摸路径通过mCanvas绘制到mBitmap上,刷新的频率由 mhandler递归发送消息的间隔决定
* 默认 UPDATE_DELAY = 20 即 刷新50次/秒
*/
public synchronized void update() {
if (mCanvas != null) {
mCanvas.drawPath(mPath, mPaint);
invalidate(); // 将 mBitmap 绘制到View上
}
}
/**
* 开始绘制
*/
void startUpdating() {
mStopDrawing = false;
mHandler.removeMessages(UPDATE_MSG);
mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_MSG), UPDATE_DELAY);
}
/**
* 停止绘制
*/
void stopUpdating() {
resetState();
mHandler.removeMessages(UPDATE_MSG);
}
/**
* 将标记变量恢复到初始状态
*/
public void resetState() {
// 清空已绘制的路径
mPath.reset();
// 恢复开始绘制
mStartDrawing = true;
mStopDrawing = false;
}
private synchronized void drawPoint(int x, int y) {
if (mCanvas != null) {
if (mStartDrawing == true) {
mStartDrawing = false;
mPath.reset();
mPath.moveTo(x, y);
}
mPath.lineTo(x, y);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startUpdating();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
mStopDrawing = true; // 停止记录和绘制
break;
}
int N = event.getHistorySize();
for (int i = 0; i < N; i++) {
drawPoint((int) event.getHistoricalX(i), (int) event.getHistoricalY(i));
}
drawPoint((int) event.getX(), (int) event.getY());
return true;
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_MSG: {
update();
if (mStopDrawing == true) {
Log.d(TAG, ("end update 2"));
stopUpdating();
} else {
// 50次/秒 发送消息刷新View
mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_MSG), UPDATE_DELAY);
}
break;
}
default:
super.handleMessage(msg);
}
}
};
/**
* View创建时自动生成对应尺寸的 位图 mBitmap
*
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//Log.d("fmi", "===== onSizeChanged ==== w = " + w + " , h = " + h);
//
//
//
//DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
//Log.d("fmi", "===== onSizeChanged ==== widthPixels = " + displayMetrics.widthPixels + " , heightPixels = " +
// displayMetrics.heightPixels);
//
//Bitmap newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
//Canvas newCanvas = new Canvas();
//newCanvas.setBitmap(newBitmap);
//if (mBitmap != null) {
// newCanvas.drawBitmap(mBitmap, 0, 0, null);
//}
///**
// * mCanvas 一直在mBitmap上面绘制,所以最终要现实到界面需要在onDraw 方法中绘制mBitmap才能显示出来!!
// */
//mBitmap = newBitmap;
//mCanvas = newCanvas;
//mCanvas.drawColor(0x00FF0000);
}
}