【问题标题】:Redirect into New Page after Background Fades Android在Android背景消失后重定向到新页面
【发布时间】:2011-11-07 12:17:56
【问题描述】:

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

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

【问题讨论】:

  • 当任何答案符合您的标准时..接受它以便其他用户可以了解..它可以帮助其他人找到有效的解决方案。

标签: android background fading


【解决方案1】:

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 活动对象。但是在线程内部,这将引用当前线程对象(不确定它究竟指向什么),因此您需要使用“Intro.this”来确定它的范围,这意味着“使用指向 Intro 活动的 this”

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

你还需要修改代码

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

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

alpha 值设置为 255,表示不透明,不会显示背景图像。

【讨论】:

  • 这是否意味着在 }).start(); 之前?它不起作用..它给出了一个错误“构造函数 Intent(new Thread(){}, Class) 未定义”
  • 谢谢!有效! :) 我还有一个问题.. 希望你能帮助我.. 然后我怎么能从背景图片开始呢?我运行我的程序,它从黑色到白色..我想要从图片到白色..谢谢
  • 我尝试了代码并将 TabTester 更改为 Intro.this,但它强制关闭.. 可能是因为 FrameLayout?
  • Tabtester 只是我创建的一个测试文件。将其替换为 Intro.this。错误日志说什么?
  • 我发现 xml 文件是线性布局,所以我将其更改为 framelayout.. 程序运行,但是程序从黑色背景开始,而不是从图片(XML 文件的背景)开始
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多