Android 背景淡出后重定向到新页面

发布于 2024-12-13 20:00:36 字数 1881 浏览 2 评论 0原文

我希望我的应用程序在使用线程进入另一个页面之前先有一个褪色的图片。下面是我使用的代码,它对我来说效果很好。但是,它停在线程末尾的白页中。我该怎么做才能使其在不点击任何内容的情况下进入下一个活动?或者页面变白后,我应该使用什么代码才能转到下一个活动?

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class Intro extends Activity {
    LinearLayout screen;
    Handler handler = new Handler();
    int i;
    Intent intent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.introxml);

        screen = (LinearLayout) findViewById(R.id.myintro);

        (new Thread(){
            @Override
            public void run(){
                for(i=0; i<255; i++){
                    handler.post(new Runnable(){
                        public void run(){
                            screen.setBackgroundColor(Color.argb(255, i, i, i));
                        }
                    });
                    // next will pause the thread for some time
                    try{ sleep(10); }
                    catch(Exception e){ break; }
                }
                for(i=255; i>0; i--){
                    handler.post(new Runnable(){
                        public void run(){
                            screen.setBackgroundColor(Color.argb(255, i, i, i));
                        }
                    });
                    // next will pause the thread for some time
                    try{ sleep(10); }
                    catch(Exception e){ break; }
                }
                startActivity(new Intent(Intro.this,NewKFCActivity.class));
            }
        }).start();
    }
}

I wanted my app to had a fading picture first before going into another page using threads. Below is the code i used and it worked well for me. However, it stops in the white page at the end of the thread. What will i do so that it will go to the next activity without clicking on anything? Or after the page turns white, what code should i use so that it will go now to the next activity?

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class Intro extends Activity {
    LinearLayout screen;
    Handler handler = new Handler();
    int i;
    Intent intent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.introxml);

        screen = (LinearLayout) findViewById(R.id.myintro);

        (new Thread(){
            @Override
            public void run(){
                for(i=0; i<255; i++){
                    handler.post(new Runnable(){
                        public void run(){
                            screen.setBackgroundColor(Color.argb(255, i, i, i));
                        }
                    });
                    // next will pause the thread for some time
                    try{ sleep(10); }
                    catch(Exception e){ break; }
                }
                for(i=255; i>0; i--){
                    handler.post(new Runnable(){
                        public void run(){
                            screen.setBackgroundColor(Color.argb(255, i, i, i));
                        }
                    });
                    // next will pause the thread for some time
                    try{ sleep(10); }
                    catch(Exception e){ break; }
                }
                startActivity(new Intent(Intro.this,NewKFCActivity.class));
            }
        }).start();
    }
}

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

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

发布评论

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

评论(1

别把无礼当个性 2024-12-20 20:00:36

for循环退出后。添加代码以启动新活动。

startActivity(new Intent(Intro.this,NewACtivity.class));

您需要将其放在 for 循环之外。如果将其放在 start 方法之后,它将在线程完成之前执行。您可能还需要使用 Intro.this 确定“this 变量”的范围。另请记住在清单文件中添加新活动,因为

<activity android:name=".NewActivity"/>

只需使用此

screen = (FrameLayout) findViewById(R.id.layout);
(new Thread(){
    @Override
public void run(){
    for(i=0; i<255; i++){
        handler.post(new Runnable(){
            public void run(){
                screen.setBackgroundColor(Color.argb(255, i, i, i));
            }
        });
        // next will pause the thread for some time
        try{ sleep(100); }
           catch(Exception e){ break; }
        }
        startActivity(new Intent(TabTester.this,NewKFCActivity.class));
    }
}).start();

此指针应指向 Intro 活动对象。但在线程内部, this 将引用当前线程对象(我不确定它到底指向什么),因此您需要使用“Intro.this”来确定它的范围,这意味着“使用指向 Intro 活动的 this

”当您对同一视图使用 setBackgroundColor 时,图片将被覆盖。实现此目的的一种方法是使用布局,外部布局将具有背景图片,内部布局将是应用了 setBackgroundColor 的布局。
例如:

您还需要将代码更改

screen.setBackgroundColor(Color.argb(255, i, i, i));

screen.setBackgroundColor(Color.argb(120, i, i, i));

Alpha 值设置为 255,这意味着不透明并且不会显示背景图像。

After the for loop exits. Add the code to start a new activity.

startActivity(new Intent(Intro.this,NewACtivity.class));

You need to put it outside the for loop. If you put it after start method, it will execute before the thread is completed. You may also need to scope the 'this variable' using Intro.this. Also remember to add the new activity in the manifest file as

<activity android:name=".NewActivity"/>

Just use this

screen = (FrameLayout) findViewById(R.id.layout);
(new Thread(){
    @Override
public void run(){
    for(i=0; i<255; i++){
        handler.post(new Runnable(){
            public void run(){
                screen.setBackgroundColor(Color.argb(255, i, i, i));
            }
        });
        // next will pause the thread for some time
        try{ sleep(100); }
           catch(Exception e){ break; }
        }
        startActivity(new Intent(TabTester.this,NewKFCActivity.class));
    }
}).start();

this pointer should point to the Intro activity object. But inside the thread, this will refer to the the current thread object(am not sure to what it point exactly) so you need to scope it using 'Intro.this' which mean 'use this that points to the Intro activity'

You background picture will be overwritten when you use setBackgroundColor to the same view. A way to do this will be to use to layouts, the outer layout will have the background picture and the inner layout will be the one that has setBackgroundColor applied to.
Eg:

You also need to alter the code

screen.setBackgroundColor(Color.argb(255, i, i, i));

to

screen.setBackgroundColor(Color.argb(120, i, i, i));

The alpha value is set to 255 which means opaque and will not show the background image.

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