【问题标题】:Splash screen with fade in animation of ImageViewImageView 动画淡入淡出的闪屏
【发布时间】:2016-02-24 11:33:33
【问题描述】:

所以我正在尝试使用图像视图淡入动画来实现启动画面,我想在启动画面活动的开始同时开始。在图像视图的动画完成后,我还希望启动屏幕活动在短暂延迟后结束(触摸事件是可选的)。

我的 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) {

}
}

每当我尝试在注释中添加代码块时,应用程序都会崩溃,无论我把它放在哪里。没有淡入淡出的动画效果一切都很好。有什么方法可以轻松地同步这两者?我对编码很陌生,所以我试图实现的目标可能存在一些致命的错误。

我的 screen_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>

【问题讨论】:

  • 您可以使用 Picasso 来加载 ImageView 和淡入淡出动画。在 sleep(2000) 之后,再次使用 Picasso。

标签: java android xml animation android-studio


【解决方案1】:

您需要在尝试访问其属性之前初始化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);

【讨论】:

  • 好的,你回答得更快了:D 我会删除我的不垃圾邮件
【解决方案2】:

就像黑带说的你需要初始化,

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

【讨论】:

    【解决方案3】:
    Animation animation= AnimationUtils.loadAnimation(this,R.anim.fadeIn);
    img1.startAnimation(animation);
    

    【讨论】:

    • 感谢参与。不鼓励仅使用 Coe 的答案。请给出一些解释以便更好地理解。
    猜你喜欢
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多