I want to make a view that show as if a point/circle is moving horizontally on the screen.
I want to achieve this only through Custom view.
So i wrote a piece of code in which i want to call onDraw() which will draw a circle, then it will call a handler, which will wait for 500ms and call onDraw(). So each time onDraw() is called circle will be shifted few steps towards right.
but here i am not able to syncup call between run() and onDraw().
code:
package com.example.layoutpractice;
import javax.net.ssl.HandshakeCompletedListener;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class DrawView extends View {
private static final String TAG = "DrawView";
Context ctx = null;
int MAX_WIDTH = 0;
int MAX_HEIGHT = 0;
int current_x = 0;
int current_y = 0;
Paint paint = null;
public DrawView(Context context) {
@TargetApi(13)
private void initialize() {
// get the screen size
WindowManager wm = (WindowManager) ctx
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
MAX_WIDTH = size.x;
MAX_HEIGHT = size.y;
} else {
MAX_WIDTH = display.getWidth();
MAX_HEIGHT = display.getHeight();
}
// initialize paint
paint = new Paint();
paint.setColor(Color.BLUE);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "onDraw()");
current_y = MAX_HEIGHT / 2;
while (current_x < MAX_WIDTH) {
canvas.drawColor(Color.WHITE, Mode.CLEAR);
canvas.drawCircle(current_x, current_y, 2, paint);
current_x = current_x + 10;
// new Thread(new TimeHandler()).start();
new TimeHandler().execute();
}
super.onDraw(canvas);
}
class TimeHandler extends Handler {
Runnable myRun = new Runnable() {
public void run() {
Log.d(TAG, "run");
invalidate();
}
};
public void execute() {
this.postDelayed(myRun, 500);
}
}
}
logs
02-17 01:40:28.355: D/DrawView(1162): onDraw()
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): onDraw()
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.065: D/DrawView(1162): run
02-17 01:40:29.065: D/DrawView(1162): run
02-17 01:40:29.075: D/DrawView(1162): run
02-17 01:40:29.075: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.105: D/DrawView(1162): onDraw()
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.175: D/DrawView(1162): onDraw()
Change
to
P.S. you design is wrong. onDraw is a special method, which can be called by android, not by you only. Every time onDraw is called by you via invalidate or by framework itself you schedule a runnable to run in 500ms. And you think that onDraw will be executed every 500ms (current_x = current_x + 10;)… you have a problem )
To fix your problem, do not put any logic in ondraw. OnDraw should only draw view using current state. Leave only this:
Create additional method in you custom View, which should do following:
I’m sure that you can use my code as is, but logic is clear.