活动过渡与背景颜色不能正常工作



我有两个活动(A和B)在我的应用程序。两个活动都有一个背景颜色。当我打开活动B从活动A使用一个意图,一会儿背景颜色改变为默认的灰色。这种默认颜色的闪烁使得过渡看起来不太流畅。我怎样才能使它光滑呢?

活动A XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#116493" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

活动B XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#116493">
</LinearLayout>
活动A.java

package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
    TextView tt1;
    private Handler customHandler = new Handler(Looper.getMainLooper());
    long timeInMilliseconds = 0L,timeToGo=0L,startTime=0L;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tt1=(TextView) findViewById(R.id.textView1);
        startTime=System.currentTimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);
    }
    public Runnable updateTimerThread=new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            long timeNow = System.currentTimeMillis();
            timeToGo = 30 - (timeNow - startTime) / 1000;
            tt1=(TextView) findViewById(R.id.textView1);
            tt1.setText(timeToGo+"");
            if(timeToGo<0L){
                Intent intent=new Intent(MainActivity.this,Game.class);
                finish();
                startActivity(intent);
            }
            else
                customHandler.postDelayed(this, 0);
        }
    };
}

活动B .java

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Game extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
    }

}

你在UI线程上执行了太多的计算,这就是为什么它会滞后。特别是findViewById是一个非常繁重的操作。试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tt1=(TextView) findViewById(R.id.textView1);
    new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) {
            tt1.setText(millisUntilFinished / 1000 + "");
        }
        public void onFinish() {
            // tt1.setText("done!");
            Intent intent=new Intent(MainActivity.this,Game.class);
            finish();
            startActivity(intent);
        }
     }.start();
 }

我找到了解决方案。在活动A中,我在开始另一个意图之前使用了finish()。因此,前一个活动首先关闭,下一个活动打开。这两个活动的执行之间有一个短暂的延迟,它被视为默认的灰色闪光。这是因为在很短的时间内没有任何活动,默认的颜色是闪烁的。

我通过首先启动intent,然后使用finish完成之前的活动来解决问题。

活动A的代码为

package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
    TextView tt1;
    private Handler customHandler = new Handler(Looper.getMainLooper());
    long timeInMilliseconds = 0L,timeToGo=0L,startTime=0L;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tt1=(TextView) findViewById(R.id.textView1);
        startTime=System.currentTimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);
    }
    public Runnable updateTimerThread=new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            long timeNow = System.currentTimeMillis();
            timeToGo = 30 - (timeNow - startTime) / 1000;
            tt1=(TextView) findViewById(R.id.textView1);
            tt1.setText(timeToGo+"");
            if(timeToGo<0L){
                Intent intent=new Intent(MainActivity.this,Game.class);
                startActivity(intent); //Exchanged the order of statement
                finish();              //After starting intent
            }
            else
                customHandler.postDelayed(this, 0);
        }
    };
}

最新更新