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

1 に答える 1

1

for ループが終了した後。コードを追加して、新しいアクティビティを開始します。

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

for ループの外に置く必要があります。start メソッドの後に置くと、スレッドが完了する前に実行されます。Intro.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));

アルファ値は 255 に設定されています。これは不透明を意味し、背景画像を表示しません。

于 2011-11-07T12:26:48.257 に答える