메인에서..
화면 터치 혹은 MOVE시에 graphView를 호출하는데요
좌표값을 전달하여 graphView에서 그것을 받아 onDraw()에서 drawLine()으로 그려줍니다.
타이머를 사용하여 x좌표값을 증가시켜서 마치 그래프를 그리듯이 출력이 됩니다.
근데 문제가... 처음에는 그려지는 속도가 매우 빠른데
한 3초후 부터는 현저히 느려지네요....
(터치를 했을 때 선이 그려지는데까지 걸리는 시간이 길어짐...)
속도를 개선할 수 있는 방법이 있을까요??
main 입니다.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFrameLayout = (FrameLayout) findViewById(R.id.frame);
textView = (TextView) findViewById(R.id.textView);
touchView = findViewById(R.id.touchView);
Log.i("TestLog", "ctn = " + cnt);
touchView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
String type = "LINE";
String[] verlabels = { "X", "Y", "TM", "Tm", "S", "Prs" };
String[] horlabels = { " ", " ", " ", " " };
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE) {
float xvalue = event.getX();
//float yvalue = event.getY();
mHandler = new Handler() {
public void handleMessage(Message msg) {
cnt += 3;
mHandler.sendEmptyMessageDelayed(0, 2000);
if (cnt == 455){
cnt = 0;
}
}
};
mHandler.sendEmptyMessage(0);
textView.setText("Touch coordinates : "
+ String.valueOf(event.getX()) + " x "
+ String.valueOf(event.getY()) + " x "
+ String.valueOf(event.getTouchMajor()) + " x "
+ String.valueOf(event.getTouchMinor()) + " x "
+ String.valueOf(event.getSize()) + " x "
+ String.valueOf(event.getPressure()) + " x ");
Log.d("***My Log***", "X Point" + xvalue);
graphView = new GraphView(getApplicationContext(), xvalue,
"TouchPoint", horlabels, verlabels,
/* GraphView.LINE */type, cnt);
graphView.setVisibility(View.VISIBLE);
mFrameLayout.addView(graphView);
}
return true;
}
});
/////////
graphView.java입니다.
protected void onDraw(Canvas canvas) {
float border = 20;
float horstart = border;
float height = getHeight() - 43;
float width = getWidth() - 1;
float graphheight = height - (2 * border);
float graphwidth = width - (2 * border);
paint.setAntiAlias(true);
paint.setTextAlign(Align.LEFT);
/*** 가로선 ***/
int vers = verlabels.length - 1;
for (int i = 0; i < verlabels.length; i++) {
paint.setStrokeWidth(3);
paint.setColor(Color.RED); // 가로선
float y = ((graphheight / vers) * i) + border;
canvas.drawLine(horstart, y, width, y, paint);
paint.setColor(Color.BLUE); // 좌측
paint.setTextSize(20);
canvas.drawText(verlabels[i], 0, y + 70, paint);
}
/*** 세로선 ***/
int hors = horlabels.length - 1;
for (int i = 0; i < horlabels.length; i++) {
paint.setStrokeWidth(10);
paint.setColor(Color.GREEN);
float x = ((graphwidth / hors) * i) + horstart;
canvas.drawLine(x, height - border, x, border, paint);
/*
* paint.setTextAlign(Align.CENTER); if (i==horlabels.length-1)
* paint.setTextAlign(Align.RIGHT); if (i==0)
* paint.setTextAlign(Align.LEFT); paint.setColor(Color.BLACK);
* canvas.drawText(horlabels[i], x, height - 4, paint);
*/
}
/*** 타이틀 ***/
paint.setTextAlign(Align.CENTER);
paint.setTextSize(22);
paint.setColor(Color.BLACK);
canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
/*** 그래프 선 ***/
paint.setStrokeWidth(6);
paint.setColor(Color.YELLOW);
paint.setStrokeCap(Cap.ROUND);
if (type == "LINE") { // LINE
float x1 = 25 + cnt;
float x2 = 25 + cnt;
// float y1 = xvalue/10;
float y1 = 145;
float y2 = y1 - ((xvalue) / 3.8f);
Log.i("TestLog", "Y2 = " + y2);
canvas.drawLine(x1, y1, x2, y2, paint);
if (cnt >= 476) {
//그래프 다 지우기
}
}
}




android:hardwareAccelerated="true" 를 넣으면 기기에 따라, 좀 빨라집니다.