【问题标题】:Splash loading skipped transition animation飞溅加载跳过的过渡动画
【发布时间】:2014-04-15 11:05:15
【问题描述】:

我是 android 编程的新手,在这里我尝试使用 RunOnUIThread 创建一些启动画面,其中有 3 个不同的图像一个接一个地显示,这是代码

Splash.java

public class Splash extends Activity {

int loading_count;
int imgShowed = 1;
ImageView img1, img2;
ImageSwitcher imgSwitcher;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    img1 = (ImageView)findViewById(R.id.img01);
    img2 = (ImageView)findViewById(R.id.img02);
    imgSwitcher = (ImageSwitcher)findViewById(R.id.splash_switcher);

    LoadingThread();
}

private void LoadingThread(){
    Thread loading = new Thread(){
        @Override
        public void run(){
            try{
            //=================================//
            //   Looping for splash loading    //
            //=================================//
                while(loading_count < 170){
                    sleep(50);
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            if(loading_count >=20){
                                if(imgShowed == 1){
                                    imgSwitcher.showNext();
                                    imgShowed = 2;
                                }
                            }if(loading_count >= 70){
                                if(imgShowed == 2){
                                    img1.setImageResource(R.drawable.two);
                                    imgSwitcher.showNext();
                                    imgShowed = 3;
                                }
                            }if(loading_count >= 120){
                                if(imgShowed == 3){
                                    img2.setImageResource(R.drawable.three);
                                    imgSwitcher.showNext();
                                    imgShowed = 4;
                                }
                            }
                            loading_count += 1;
                        }
                    });
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                StartMainMenu();
            }
        }
    };
    loading.start();
}

private void StartMainMenu(){
    Intent mainmenu = new Intent(this, MainMenu.class);
    finish();
    startActivity(mainmenu);
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000" >

    <ImageSwitcher 
        android:id="@+id/splash_switcher"
        android:layout_centerInParent="true"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#00000000"
        android:inAnimation="@anim/fade_in"
        android:outAnimation="@anim/fade_out"
        >

        <ImageView 
            android:id="@+id/img01"
            android:layout_height="200dp"
            android:layout_width="200dp"
            android:scaleType="fitXY"
            android:background="#000000"
            android:contentDescription="@string/app_name"
            />

        <ImageView 
            android:id="@+id/img02"
            android:layout_height="200dp"
            android:layout_width="200dp"
            android:scaleType="fitXY"
            android:background="@drawable/one"
            android:contentDescription="@string/app_name"
            />

    </ImageSwitcher>

</RelativeLayout>

在 android 模拟器上执行它后,我在 logcat 中收到消息说它跳过了 42 帧,因为应用程序在它的主线程上做了太多的工作,并且过渡动画没有按我的预期工作.. 我在线?请指导我进行解释。

【问题讨论】:

    标签: java android transition


    【解决方案1】:

    我在尝试了所有可能的方法后发现了一些问题.. 发生跳帧是因为我没有完全停止线程(我猜?如果这个错误请纠正我)所以我会添加 return; 像这样停止线程:

    ...
    if(loading_count >= 170){
        StartMainMenu();
        return;
    }
    ...
    

    现在过渡动画可以正常工作而不会跳过任何帧。抱歉提出愚蠢的问题并发布愚蠢的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-11
      • 2019-12-17
      • 1970-01-01
      • 2020-05-09
      • 2017-09-22
      • 1970-01-01
      • 2013-11-05
      • 2012-08-01
      相关资源
      最近更新 更多