如何在安卓中通过点击图像使其渐隐并引入另一张图像?

huangapple go评论67阅读模式
英文:

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:-

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/bartImageView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:src=&quot;@drawable/bart_simspon&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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


            }


        });

huangapple
  • 本文由 发表于 2020年9月16日 16:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63915757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定