Please find custom view class to update view frequently..
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class CustomView extends View {
Paint paint;
int radius = 0;
public CustomView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
}
public void updateView(int radius) {
this.radius = radius;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
}
}
And call this method whenever you want to start animation :
public void startViewAnimation() {
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (i < 70) { // Please change '70' according to how long you want to go
runOnUiThread(new Runnable() {
@Override
public void run() {
int baseRadius=20; // base radius is basic radius of circle from which to start animation
customView.updateView(i+baseRadius);
i++;
}
});
} else {
i = 0;
}
}
}, 0, 500); // change '500' to milliseconds for how frequent you want to update radius
}
And Here how to call it :
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rootView);
customView = new CustomView(this);
relativeLayout.addView(customView);
startViewAnimation();
And here is defined variables :
private int i = 1;
CustomView customView;
Thanks..!!