活动转换无法正常使用背景色

时间:2015-08-02 16:13:27

标签: android android-intent android-activity

我的应用程序中有两个活动(A和B)。两种活动都有背景颜色。当我使用意图从活动A打开活动B时,背景颜色会变为默认的灰色。这种默认颜色的闪光使得转换看起来不平滑。我怎样才能让它顺利?

活动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);
    }


}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我找到了解决方案。在Activity A中,我在开始另一个intent之前使用了finish()。所以先前的Activity先关闭然后打开next。执行这两项活动之间存在短暂的延迟,并且它被视为默认的灰色闪光。这是因为在该短时间内不存在活动并且闪烁默认颜色。

我首先启动了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);
        }
    };
}