在Android中创建屏幕保护程序

时间:2011-12-02 12:00:28

标签: android screensaver

为android(不是锁定屏幕)编程屏幕保护程序是否合理?

我正在寻找有关屏幕保护程序编程的教程。屏幕保护程序将激活,因为用户不再执行任何操作。如果用户触摸屏幕,屏幕保护程序将消失。这可能吗?

2 个答案:

答案 0 :(得分:2)

您可以使用ACTION_SCREEN_OFF在应用中收到BroadcastReceiver。在此BroadcastReceiver的onReceive方法中,以screensaver开始活动。您可以注册一个touch events的监听者,当此事件发生时,请完成您的应用程序。

答案 1 :(得分:1)

我正在为屏幕保护程序摘录:评论将解释它。

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

要查看完整代码,请参阅此处: - Code-Example-Screen-Saver-Colorful-Dots