首页 技术 正文
技术 2022年11月23日
0 收藏 775 点赞 4,348 浏览 2490 个字

先上图:

关键是在检測到手指移动的时候用mPath.quadTo的方法,android sdk解释是:

Add a quadratic bezier from the last point, approaching control point (x1,y1), and ending at (x2,y2). If no moveTo() call has been made for this contour, the first point is automatically set to (0,0).

中文是用贝塞尔曲线链接了(x1,y1),(x2,y2)这两个点,假设没有moveTo()这种方法调用的话,第一个点默觉得(0,0)

android绘图是用Canvas的API,如画一个实心的矩形,能够用在重写一个View的onDraw():

<span style="white-space:pre"></span>Rect rect = new Rect(100,100,500,500);
mPaint.setStrokeWidth(5); //设置画笔的粗细
mPaint.setColor(Color.RED); //设置画笔的颜色
mPaint.setStyle(Style.FILL); //填充整个图形
mPaint.setAntiAlias(true); //抗锯齿效果
canvas.drawRect(rect, mPaint);

对于检測手指的移动,我们能够用onTouchEvent来实现:

private float mX, mY;
private float mOppositeX, mOppositeY;
private static final float TOUCH_TOLERANCE = 4; //当手指移动超过4时我们才去set Path@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
if(isMirrorDraw) {
touch_opposite_start(x, y);
}
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
if(isMirrorDraw) {
touch_opposite_move(x, y);
}
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
if(isMirrorDraw) {
touch_opposite_up();
}
invalidate();
break;
}
return true;
}private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}private void touch_move(float x, float y) {
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;
}
}private void touch_up() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}private void touch_opposite_up() {
mOppositePath.lineTo(mOppositeX, mY);
mCanvas.drawPath(mOppositePath, mOppoPaint);
mOppositePath.reset();
}private void touch_opposite_move(float x, float y) {
float oppositeX = OppositeDrawActivity.screenWidth - x; //<span style="font-family: Arial, Helvetica, sans-serif;">OppositeDrawActivity.screenWidth是屏幕宽度</span>
float dx = Math.abs(oppositeX - mOppositeX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mOppositePath.quadTo(mOppositeX, mY, (mOppositeX + oppositeX) / 2, (y + mY) / 2);
mOppositeX = oppositeX;
mY = y;
}
}private void touch_opposite_start(float x, float y) {
mOppositePath.reset();
float oppositeX = OppositeDrawActivity.screenWidth - x;
mOppositePath.moveTo(oppositeX, y);
mOppositeX = oppositeX;}

然后重写onDraw():

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
if(isMirrorDraw) {
canvas.drawPath(mOppositePath, mOppoPaint);
}
}

代码能够在http://download.csdn.net/detail/baidu_nod/7572549下载

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,933
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,454
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,267
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,085
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,717
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,754