android压感测试,android压感补充

绘制的补充:

@Override

protected void onDraw(Canvas canvas) {

Rect r = canvas.getClipBounds();

PathInfo pathInfo;

for (int i = 0; i < mPathInfo.size(); i++){

pathInfo = mPathInfo.get(i);

if (Rect.intersects(r, pathInfo.rect)){

mPaint.setColor(pathInfo.color);

//press

mPaint.setStrokeWidth(pathInfo.press);

//press over

}

canvas.drawPath(pathInfo.path, mPaint);

}

mPaint.setColor(GlobalSetting.mScriptColor);

canvas.drawPath(mPath, mPaint);

}

onTouchEvent处理:

private final float PRESS_OFFSET = 2.5f;

@Override

public boolean onTouchEvent(MotionEvent event) {

if ( action == MotionEvent.ACTION_DOWN )

{

mCurX = (int)event.getX();

mCurY = (int)event.getY();

mPrevX = mCurX;

mPrevY = mCurY;

//press

float pressD = event.getPressure();

float width = getEventPress(pressD);

mPaint.setStrokeWidth(width);

mPath = new Path();

mPath.moveTo(mCurX, mCurY);

mPath.computeBounds(mRectF, true);

mRect.set(((int)mRectF.left - (int)width), ((int)mRectF.top - (int)width),

((int)mRectF.right + (int)width), ((int)mRectF.bottom + (int)width));

PathInfo pathInfoH = new PathInfo(mPath, mRect, width);

mPathInfo.add(pathInfoH);

mPrePress = width;

invalidate(mRect);

return true;

}

//moving

int N = event.getHistorySize();

Log.i(TAG,"N:"+N);

int x = 0;

int y = 0;

float pressH = 0;

int i=0;

for (i=0; i

x = (int)event.getHistoricalX(i);

y = (int)event.getHistoricalY(i);

//press

pressH =event.getHistoricalPressure(i);

float width = getEventPress(pressH);

mPath = new Path();

mPaint.setStrokeWidth(width-i);

drawPoint(mCurX, mCurY, x, y);

mPath.computeBounds(mRectF, true);

mRect.set(((int)mRectF.left - (int)width-i), ((int)mRectF.top - (int)width-i),

((int)mRectF.right + (int)width-i), ((int)mRectF.bottom + (int)width-i));

PathInfo pathInfoH = new PathInfo(mPath, mRect, width);

mPathInfo.add(pathInfoH);

mCurX = x;

mCurY = y;

StrokeMgr.instance().addStroke((short)mCurX, (short)mCurY);

mPrePress = width;

invalidate(mRect);

}

x = (int)event.getX();

y = (int)event.getY();

//press

pressH = event.getPressure();

float width = getEventPress(pressH);

if( width > mPrePress +PRESS_OFFSET ){

width = mPrePress+PRESS_OFFSET;

}else if(width < mPrePress - PRESS_OFFSET){

width = mPrePress-PRESS_OFFSET;

}

mPrePress = width;

mPath = new Path();

drawPoint(mCurX, mCurY, x, y);

mPaint.setStrokeWidth(width-i);

mPath.computeBounds(mRectF, true);

mRect.set(((int)mRectF.left - (int)width), ((int)mRectF.top - (int)width),

((int)mRectF.right + (int)width), ((int)mRectF.bottom + (int)width));

PathInfo pathInfoM = new PathInfo(mPath, mRect, width);

mPathInfo.add(pathInfoM);

//press over

mCurX = x;

mCurY = y;

StrokeMgr.instance().addStroke((short)mCurX, (short)mCurY);

if ( action == MotionEvent.ACTION_UP ){

mPath.computeBounds(mRectF, true);

float widthUp = getEventPress(event.getPressure());

if( widthUp > mPrePress +PRESS_OFFSET ){

widthUp = mPrePress+PRESS_OFFSET;

}else if(widthUp < mPrePress - PRESS_OFFSET){

widthUp = mPrePress-PRESS_OFFSET;

}

mRect.set( (int)(mRectF.left - widthUp), (int)(mRectF.top - widthUp),

(int)(mRectF.right + widthUp), (int)(mRectF.bottom + widthUp) );

mPrePress = widthUp;

PathInfo pathInfoUp = new PathInfo(mPath, mRect, widthUp);

mPathInfo.add(pathInfoUp);

// fadePoints();

mCurX = -1;

mCurY = 0;

}

invalidate(mRect);

return true;

}

如果需要填充渐变:

private int[] mFadeColor = { 0xffffffff, 0xddffffff, 0xaaffffff,

0x99ffffff, 0x77ffffff, 0x66ffffff, 0x55ffffff,

0x44ffffff, 0x39ffffff, 0x33ffffff,0x22ffffff,0x17ffffff,

0x11ffffff, 0x09ffffff, 0x05ffffff, 0x00ffffff};

private void fadePoints(){

int count = mPathInfo.size();

int start = count - 1;

int end = count - 16;

end = end < 0 ? 0 : end;

PathInfo pathInfo;

int index = 0;

for (int i = start; i >= end; i--, index++){

pathInfo = mPathInfo.get(i);

pathInfo.color = ( pathInfo.color | 0xff000000 ) & mFadeColor[index];

mRect.union(pathInfo.rect);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值