简体   繁体   English

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

[英]Splash screen with fade in animation of ImageView

So i am trying to implement splash screen with image view fade in animation which i want to start simultaneously with the start of the splash screen activity. 所以我试图实现启动画面,图像视图淡入动画,我想与启动画面活动同时启动。 I also want splash screen activity to end after short delay (on touch event is optional), after animation of image view is finished. 在图像视图的动画完成后,我还希望在短暂延迟(触摸事件是可选的)之后结束闪屏活动。

My SplashScreen.java: 我的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) {

}
}

Application crashes every time i try to add the block of code in comment, no matter where i put it. 每次我尝试在注释中添加代码块时,应用程序都会崩溃,无论我把它放在哪里。 Without fade in animation works everything just fine. 没有淡入淡出的动画效果一切都很好。 Is there any way how to synchronize these two in easy way? 有没有办法如何以简单的方式同步这两个? Im quite new with coding so there may be some fatal mistake in what im trying to achieve. 我是一个很新的编码,所以在我试图实现的目标中可能会有一些致命的错误。

My screeen_splash.xml 我的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>

My fade_in.xml 我的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>

you need to initialize the ImageView before trying to access their properties. 您需要在尝试访问其属性之前初始化ImageView Eg 例如

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);

Like Blackbelt said you need to initialize, 像Blackbelt说你需要初始化,

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

and if you want to the animation to start correctly after the view is created you need to do this 如果您希望在创建视图后正确启动动画,则需要执行此操作

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

img2.post(new Runnable(){
    img2.startAnimation(fadeIn);
});
Animation animation= AnimationUtils.loadAnimation(this,R.anim.fadeIn);
img1.startAnimation(animation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM