java - 带有淡入淡出 ImageView 动画的启动画面

标签 java android xml animation android-studio

所以我正在尝试在动画中实现 ImageView 淡入淡出的启动画面,我想在启动启动画面 Activity 的同时启动它。在 ImageView 动画完成后,我还希望启动画面 Activity 在短暂延迟后结束(触摸事件是可选的)。

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

}
}

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

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

最佳答案

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

关于java - 带有淡入淡出 ImageView 动画的启动画面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35601042/

相关文章:

java - 在java中显示XML中的文本内容

java - Admob 广告在使用 webview 和进度条的应用程序中不可见

xml - XSLT:使用模板匹配和应用模板仅显示唯一值

c# - 您可以在 C# 中使用 XPath 将 xml 文档过滤为节点子集吗?

java - Jsp 可编辑数据网格

android - TabLayout 删除不必要的滚动

android - 如何在 recyclerview 的 viewholder 中创建动态项目行?

Java BufferedImage 在 appletviewer 中工作,但在浏览器中不起作用

java - 将值传递给构造函数

java - 无法在Android中下载图像