在 Android 中创建屏幕保护程序

发布于 2024-12-19 08:09:54 字数 109 浏览 2 评论 0原文

是否可以为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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼藏柔 2024-12-26 08:09:54

您可以使用 BroadcastReceiver 在您的应用中接收 ACTION_SCREEN_OFF。在此 BroadcastReceiver 的 onReceive 方法中,启动一个 Activity 作为屏幕保护程序< /code>。您可以为触摸事件注册一个侦听器,当此事件发生时,完成您的应用程序。

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.

网名女生简单气质 2024-12-26 08:09:54

我给出了屏幕保护程序的摘录:评论会解释它。

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
点 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();  } 

要查看完整代码,请参阅此处:- 代码示例-屏幕保护程序-彩色点

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

春花秋月 2024-12-26 08:09:54

Android 屏幕保护程序示例代码

在提出问题之前尝试先搜索答案

Android Screen Saver Sample Code

Try to search first for answers before asking for something

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文