具有淡入淡出ImageView动画的启动画面

时间:2016-02-24 11:33:33

标签: java android xml animation android-studio

所以我试图在动画中实现带有图像视图淡入淡出的启动画面,我想在启动画面活动的同时启动它。在图像视图的动画完成后,我还希望在短暂延迟(触摸事件是可选的)之后结束闪屏活动。

我的SplashScreen.java:

package hillbillys.delivery;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

public class SplashScreen extends AppCompatActivity implements Animation.AnimationListener {
protected Animation fadeIn;
protected ImageView img1;
protected ImageView img2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_splash);

    fadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in);

    /*
                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.VISIBLE);
                img1.startAnimation(fadeIn);
                img2.startAnimation(fadeIn);
     */

    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep (2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent intent = new Intent(SplashScreen.this,MainActivity.class);
                startActivity(intent);
            }
        }
    };
    timerThread.start();


}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

@Override
public void onAnimationStart(Animation animation) {


}

@Override
public void onAnimationEnd(Animation animation) {
    Toast.makeText(getBaseContext(), "Animation Stopped!", Toast.LENGTH_SHORT).show();
}

@Override
public void onAnimationRepeat(Animation animation) {

}
}

每次我尝试在评论中添加代码块时,应用程序都会崩溃,无论我把它放在哪里。没有淡入淡出的动画效果一切都很好。有没有办法如何以简单的方式同步这两个?我对编码很新,所以在我试图实现的目标中可能会有一些致命的错误。

我的screeen_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff0d9"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/logo1"
    android:layout_marginTop="250dp"
    android:visibility="gone" />

<ImageView
    android:layout_width="260dp"
    android:layout_height="41dp"
    android:id="@+id/imageView2"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/logo2"
    android:visibility="gone" />

</LinearLayout>

我的fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">

<alpha
    android:duration="1000"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0"
    />

</set>

2 个答案:

答案 0 :(得分:3)

在尝试访问其属性之前,您需要初始化ImageView。例如。

img1 = (ImageView) findViewById(R.id.imageView);
img2 = (ImageView) findViewById(R.id.imageView2);
img1.setVisibility(View.VISIBLE);
img2.setVisibility(View.VISIBLE);
img1.startAnimation(fadeIn);
img2.startAnimation(fadeIn);

答案 1 :(得分:0)

像Blackbelt说你需要初始化,

final img1 = (ImageView) findViewById(R.id.imageView);
final img2 = (ImageView) findViewById(R.id.imageView2);

如果您希望在创建视图后正确启动动画,则需要执行此操作

img1.post(new Runnable(){
    img1.startAnimation(fadeIn);
});

img2.post(new Runnable(){
    img2.startAnimation(fadeIn);
});