开发者

How shift graph in Android's Canvas

I draw a graph with canvas in android, but I want to shift it to left or right side. I don't want to scroll it, I just want to shift it in one page by which you can see the graph is shifting lively. I would be appreciated if any开发者_StackOverflow body help me.


I am looking for a an answer to this question as well. I have a solution but it is to slow and uses a lot of CPU time. I am drawing two graphs with a width of 200 pixels and have the values stored in an array. I just shift the values in the array and moves them into a path. I would be nice to have a solution where the canvas gets shifted by 1 pixel to the left and the 200th column gets drawn with the new value. With both paths are empty it takes about 2ms (Droid Incredible) to draw the paths. When the 2 paths are filled up it can take more than 40ms. This would give still 25fps but I would like to run other threads at the same time and don't want to waste CPU time. Here is the source.

public class OSCI extends View

{ Integer[] codebuffer = new Integer[200]; Integer[] codebuffer1 = new Integer[200]; private static final String TAG = "MyActivity"; long start; int code=0;

// private static final String TAG = "MyActivity"; public OSCI(Context context) { super(context); Arrays.fill(codebuffer, 1); Arrays.fill(codebuffer1, 1); } public OSCI(Context context, AttributeSet attrs) { super(context, attrs);; Arrays.fill(codebuffer, 1); Arrays.fill(codebuffer1, 1); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(2); paint.setColor(Color.YELLOW); Path path = new Path(); path.moveTo(0, 70); Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG); paint1.setStyle(Paint.Style.STROKE); paint1.setStrokeWidth(2); paint1.setColor(Color.GREEN); Path path1 = new Path(); path1.moveTo(0, 70); int max = Collections.max(Arrays.asList(codebuffer)); if(max==0)max=1; for (int a = 0; a < codebuffer.length; a++) { if (codebuffer[a] < 0)code=0; code = 70 * codebuffer[a] / max; path.lineTo(a * 2 + 2, 70 - code); path1.lineTo(a * 2+ 2, 70 - (codebuffer1[a])); } start=(SystemClock.elapsedRealtime()); canvas.drawPath(path1, paint); canvas.drawPath(path, paint1); Log.v(TAG, " "+(SystemClock.elapsedRealtime()-start)); } public void newValue(int value, int valuew) { System.arraycopy(codebuffer, 1, codebuffer, 0, 199); codebuffer[199] = value; System.arraycopy(codebuffer1, 1, codebuffer1, 0, 199); codebuffer1[199] = valuew; invalidate(); }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜