Step 9: Making a dots screen saver using recursive onDraw() from inner-class thereby causing auto-Thread by inner class instantiation.

package com.ani;
import android.os.*;import android.app.*;import android.content.*;import android.view.*;
import android.widget.*;import android.graphics.*;import java.util.*;

public class HomePage extends Activity
{
@Override
protected void onCreate(Bundle b)
{
super.onCreate(b);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Point res=new Point(); getWindowManager().getDefaultDisplay().getSize(res);

DotsView myView = new DotsView(this,res.x,res.y);
setContentView(myView);
}

 

class DotsView extends View
{
int i = 0;Bitmap bmp;Canvas cnv;Rect bounds;Paint p;Random rnd;int width,height;

public DotsView(Context context ,int width ,int height)
{
super(context);
this.width = width;
this.height = height;
bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
cnv = new Canvas(bmp);
bounds = new Rect(0 , 0, width,height);
p = new Paint();
rnd = new Random();

p.setColor(Color.RED);
p.setStyle(Paint.Style.FILL_AND_STROKE);
p.setDither(false);
p.setStrokeWidth(3);
p.setAntiAlias(true);
p.setColor(Color.parseColor(“#CD5C5C”));
p.setStrokeWidth(15);
p.setStrokeJoin(Paint.Join.ROUND);
p.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onDraw(Canvas c)
{//Canvas c gets recycled , so drawing on it will keep erasing previous causing animation…we dont want that.
p.setColor(Color.argb(255, rnd.nextInt(255),rnd.nextInt(255), rnd.nextInt(255)));
//cnv is outside object canvas, so drawing on it will append, no recycle, we want that.
cnv.drawPoint(rnd.nextInt(width), rnd.nextInt(height), p);
//drawing on cnv canvas, draws on bitmap bmp
//c.drawBitmap(bmp,src_rect,dest_rect,paint):-
// crop as per src_rect(null means full), and scale to dest_rect, draw using paint object-null for ‘as it is’.
c.drawBitmap(bmp, null, bounds , null);
//make onDraw() recursive but dont make blocking-loop & not indefinite condition
if(i<1000) { i++;invalidate(); }
}
}

 

 

 

 

}