英文:
How to fade out one image by clicking it and bring in another image in android?
问题
我想在Android Studio中制作一个应用程序。它会在启动时显示Bart Simpson的图像。如果我点击图像,它将在2秒内淡出,然后出现Homer Simpson的图像。这是我尝试过的内容:
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:id="@+id/bartImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bart_simspon" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java代码:
package com.example.animations;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView bartImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bartImageView=findViewById(R.id.bartImageView);
bartImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bartImageView.animate().alpha(0).setDuration(2000);
bartImageView.setImageResource(R.drawable.homer_simpson);
}
});
}
}
但是,如果我在手机上运行应用程序,现实的情况是Homer Simpson的图像会在2秒后出现并淡出。
为什么会这样呢?
从程序上看,理想情况下应该是Bart Simpson的图像会淡出,然后Homer Simpson的图像会出现。
我做错了什么吗?
英文:
I wanted to make an app in android studio. It shows the image of Bart Simpson at launch. If I click the image then it will fade out in 2 seconds and image of Homer Simpson will appear. This is what I tried:
XML:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:id="@+id/bartImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bart_simspon" />
</androidx.constraintlayout.widget.ConstraintLayout>
java code:-
package com.example.animations;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView bartImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bartImageView=findViewById(R.id.bartImageView);
bartImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bartImageView.animate().alpha(0).setDuration(2000);
bartImageView.setImageResource(R.drawable.homer_simpson);
}
});
}
}
But if I run the app on my phone what is happening is that the image of Homer Simpson is appearing and fading after 2 sec.
Why is this happening?
If we go programmatically then ideally the image of Bart Simpson should fade out and Homer Simpson should appear.
Am I doing anything wrong?
答案1
得分: 0
你可以为动画设置监听器,当动画完成时更改图像资源。
bartImageView.animate().alpha(0f).setDuration(2000).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
bartImageView.setImageResource(R.drawable.homer_simpson);
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
英文:
you can set listener for animation and when finish animation change image resource
bartImageView.animate().alpha(0f).setDuration(2000).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
bartImageView.setImageResource(R.drawable.homer_simpson);
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
答案2
得分: 0
不要忘记在动画之后将 alpha 设置为 1
bartImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bartImageView.animate().alpha(0).setDuration(2000).withEndAction(new Runnable() {
@Override
public void run() {
bartImageView.setImageResource(R.drawable.homer_simpson);
bartImageView.setAlpha(1f);
}
});;
}
});
英文:
Don't forget to set your alpha to 1 after the animation
bartImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bartImageView.animate().alpha(0).setDuration(2000).withEndAction(new Runnable() {
@Override
public void run() {
bartImageView.setImageResource(R.drawable.homer_simpson);
bartImageView.setAlpha(1f);
}
});;
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论