goojr
goojr

Reputation: 230

Creating a screensaver in Android

Is it poosible to program a screensaver for a android (not lock screen)?

I am looking for a tutorial on programming a screen saver. The screen saver will activate as the user does nothing more. If the user touches the screen, the screensaver will disappear. Is this possible?

Upvotes: 8

Views: 9029

Answers (3)

Animesh Shrivastav
Animesh Shrivastav

Reputation: 43

I am giving an excerpt for screen-saver : Comments will explain it.

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();  } 

To see full code see here:- Code-Example-Screen-Saver-Colorful-Dots

Upvotes: 1

hasanghaforian
hasanghaforian

Reputation: 14042

You can receive ACTION_SCREEN_OFF in your App with a BroadcastReceiver.In the onReceive method of this BroadcastReceiver, start an activity as the screensaver.And you can register a listener for touch events and when this event occured,finish your App.

Upvotes: 3

Andrei Sfat
Andrei Sfat

Reputation: 8626

Android Screen Saver Sample Code

Try to search first for answers before asking for something

Upvotes: 0

Related Questions