Android根据TouchEvent来绘图onDraw问题,重绘滞后严重!! 谈谈我的理解啊,你说每次有触摸消息你就new个path,加到list里,然后draw里面无论怎样都会draw所有的path?你这样设计结果是必然的啊,list里面元素多了,速度就慢了。首先要定位问题,看看每次draw到底花了多少时间,花在了哪里。其次,考虑下你这种方式,每个path都会重新画,无论以前画过没有,这个方式可不可以优化? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不好意思,前几天又弄其它的,没及时回! 我也是在想能怎么优化path重新绘制问题。你说每次draw到底花多少时间什么意思,是每次list里所有画完时间吗,我可以肯定这个延迟时间就是在draw这里的。附上touch时,处理代码:public boolean onTouchEvent(MotionEvent event) { final int action = event.getAction(); final int pact = action & MotionEvent.ACTION_MASK; int index=(action & MotionEvent.ACTION_POINTER_ID_MASK)>>>MotionEvent.ACTION_POINTER_ID_SHIFT; float x = event.getX(); float y = event.getY(); float curpress = 0;//event.getPressure(); int pen_width = 1; if(pact == MotionEvent.ACTION_DOWN) { m_FrameNum ++; mEarseFlag = false; mCurX = x; mCurY = y; mPrevX = mCurX; mPrevY = mCurY; curpress = event.getPressure(); mOldpress = curpress; Point_Buf.pt_x = x; Point_Buf.pt_y = y; Point_Buf.pt_p = curpress; pen_width = getPenWidth(curpress); mPath = new Path(); mPath.moveTo(mCurX, mCurY); mPath.lineTo(x+1, y+1); //mPath.addCircle(x, y, 0.001f, Path.Direction.CCW); mPath.computeBounds(mRectF, true); mRect.set(((int)mRectF.left - pen_width-REFRESH_OFFSET), ((int)mRectF.top - pen_width-REFRESH_OFFSET), ((int)mRectF.right + pen_width+REFRESH_OFFSET), ((int)mRectF.bottom + pen_width+REFRESH_OFFSET)); PathInfo pathInfoH1 = new PathInfo(mPath, mRect, pen_width, mCurColor); mPathInfo.add(pathInfoH1); mPrePenWidth = pen_width; invalidate(mRect); return true; } curpress = event.getPressure(); m_FrameNum ++; mOldpress = curpress; mPrePenWidth = pen_width; drawPoint(mCurX, mCurY, Point_Buf.pt_p, x, y, curpress); mCurX = x; mCurY = y; Point_Buf.pt_x = x; Point_Buf.pt_y = y; Point_Buf.pt_p = curpress; if(pact == MotionEvent.ACTION_UP) { curpress = event.getPressure(); pen_width = getPenWidth(curpress); mOldpress = 0; mPath.computeBounds(mRectF, true); mRect.set( (int)(mRectF.left -pen_width-REFRESH_OFFSET), (int)(mRectF.top - pen_width-REFRESH_OFFSET), (int)(mRectF.right + pen_width+REFRESH_OFFSET), (int)(mRectF.bottom + pen_width+REFRESH_OFFSET)); mPrePenWidth = pen_width; PathInfo pathInfoUp = new PathInfo(mPath, mRect, pen_width, mCurColor); mPathInfo.add(pathInfoUp); Point_Buf.pt_x = 0; Point_Buf.pt_y = 0; Point_Buf.pt_p = 0; mCurX = -1; mCurY = 0; m_FrameNum = 0; } invalidate(mRect); return true; }private void drawPoint(float x1, float y1, float p1, float x2, float y2, float p2){ int pen_width = 1; float tmp_p = p1; float add_p = (p2 - p1)/7; Point2D[] cp = new Point2D[4]; for(int i=0; i<4; i++) { cp[i] = new Point2D(); } cp[0].x = mPrevX; cp[0].y = mPrevY; cp[1].x = x1; cp[1].y = y1; cp[2].x = x1; cp[2].y = y1; cp[3].x = x2; cp[3].y = y2; ComputeBezier( cp, INSERT_VALUE_NUM ); for(int i=0; i<(INSERT_VALUE_NUM-1); i++) { tmp_p = tmp_p + add_p; pen_width = getPenWidth(tmp_p); mPath = new Path(); mPath.moveTo(mPrevX, mPrevY); mPath.quadTo(BezierPoint[i].x, BezierPoint[i].y, (BezierPoint[i].x +BezierPoint[i+1].x)/2, (BezierPoint[i].y + BezierPoint[i+1].y)/2); mPath.computeBounds(mRectF, true); mRect.set(((int)mRectF.left - pen_width-REFRESH_OFFSET), ((int)mRectF.top - pen_width-REFRESH_OFFSET), ((int)mRectF.right + pen_width+REFRESH_OFFSET), ((int)mRectF .bottom + pen_width+REFRESH_OFFSET)); PathInfo pathInfoM1 = new PathInfo(mPath, mRect, pen_width, mCurColor); mPathInfo.add(pathInfoM1); mPrevX = (BezierPoint[i].x + BezierPoint[i+1].x)/2; mPrevY = (BezierPoint[i].y + BezierPoint[i+1].y)/2; } } GridView中Focus的问题 LayoutParams的参数意义是什么 opengl 问题,大牛请进 android 文件上传 下载 android中对json数据的解析,并在listview中现实,下面是数据 android 微博显示效果 调用百度地图报的错 关于调用google map的问题!!!! 急,刚学安卓开发,为什么按一下activity的按钮程序就自动停止了,求高人解答~~~ Vitamio错误,不能加载视频! 能不能读取签名信息 starActivityforResult()函数的调用与响应流程问题
你说每次draw到底花多少时间什么意思,是每次list里所有画完时间吗,我可以肯定这个延迟时间就是在draw这里的。附上touch时,处理代码:public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
final int pact = action & MotionEvent.ACTION_MASK;
int index=(action & MotionEvent.ACTION_POINTER_ID_MASK)>>>MotionEvent.ACTION_POINTER_ID_SHIFT;
float x = event.getX();
float y = event.getY();
float curpress = 0;//event.getPressure();
int pen_width = 1;
if(pact == MotionEvent.ACTION_DOWN)
{ m_FrameNum ++;
mEarseFlag = false;
mCurX = x;
mCurY = y;
mPrevX = mCurX;
mPrevY = mCurY;
curpress = event.getPressure();
mOldpress = curpress;
Point_Buf.pt_x = x;
Point_Buf.pt_y = y;
Point_Buf.pt_p = curpress;
pen_width = getPenWidth(curpress);
mPath = new Path();
mPath.moveTo(mCurX, mCurY);
mPath.lineTo(x+1, y+1);
//mPath.addCircle(x, y, 0.001f, Path.Direction.CCW);
mPath.computeBounds(mRectF, true);
mRect.set(((int)mRectF.left - pen_width-REFRESH_OFFSET), ((int)mRectF.top - pen_width-REFRESH_OFFSET),
((int)mRectF.right + pen_width+REFRESH_OFFSET), ((int)mRectF.bottom + pen_width+REFRESH_OFFSET));
PathInfo pathInfoH1 = new PathInfo(mPath, mRect, pen_width, mCurColor); mPathInfo.add(pathInfoH1);
mPrePenWidth = pen_width;
invalidate(mRect);
return true; }
curpress = event.getPressure();
m_FrameNum ++;
mOldpress = curpress;
mPrePenWidth = pen_width;
drawPoint(mCurX, mCurY, Point_Buf.pt_p, x, y, curpress);
mCurX = x;
mCurY = y;
Point_Buf.pt_x = x; Point_Buf.pt_y = y;
Point_Buf.pt_p = curpress;
if(pact == MotionEvent.ACTION_UP)
{ curpress = event.getPressure(); pen_width = getPenWidth(curpress);
mOldpress = 0;
mPath.computeBounds(mRectF, true);
mRect.set( (int)(mRectF.left -pen_width-REFRESH_OFFSET), (int)(mRectF.top - pen_width-REFRESH_OFFSET),
(int)(mRectF.right + pen_width+REFRESH_OFFSET), (int)(mRectF.bottom + pen_width+REFRESH_OFFSET));
mPrePenWidth = pen_width;
PathInfo pathInfoUp = new PathInfo(mPath, mRect, pen_width, mCurColor); mPathInfo.add(pathInfoUp);
Point_Buf.pt_x = 0;
Point_Buf.pt_y = 0;
Point_Buf.pt_p = 0;
mCurX = -1;
mCurY = 0;
m_FrameNum = 0; }
invalidate(mRect);
return true; }
private void drawPoint(float x1, float y1, float p1, float x2, float y2, float p2)
{ int pen_width = 1;
float tmp_p = p1;
float add_p = (p2 - p1)/7;
Point2D[] cp = new Point2D[4];
for(int i=0; i<4; i++)
{ cp[i] = new Point2D(); }
cp[0].x = mPrevX;
cp[0].y = mPrevY;
cp[1].x = x1;
cp[1].y = y1;
cp[2].x = x1;
cp[2].y = y1;
cp[3].x = x2;
cp[3].y = y2;
ComputeBezier( cp, INSERT_VALUE_NUM ); for(int i=0; i<(INSERT_VALUE_NUM-1); i++) { tmp_p = tmp_p + add_p;
pen_width = getPenWidth(tmp_p);
mPath = new Path();
mPath.moveTo(mPrevX, mPrevY);
mPath.quadTo(BezierPoint[i].x, BezierPoint[i].y, (BezierPoint[i].x +BezierPoint[i+1].x)/2, (BezierPoint[i].y + BezierPoint[i+1].y)/2);
mPath.computeBounds(mRectF, true);
mRect.set(((int)mRectF.left - pen_width-REFRESH_OFFSET), ((int)mRectF.top
- pen_width-REFRESH_OFFSET),
((int)mRectF.right + pen_width+REFRESH_OFFSET), ((int)mRectF .bottom + pen_width+REFRESH_OFFSET));
PathInfo pathInfoM1 = new PathInfo(mPath, mRect, pen_width, mCurColor); mPathInfo.add(pathInfoM1);
mPrevX = (BezierPoint[i].x + BezierPoint[i+1].x)/2;
mPrevY = (BezierPoint[i].y + BezierPoint[i+1].y)/2; } }