java - 如何让我的图像在 android studio 中淡出?

标签 java android xml android-studio android-animation

我对编程非常陌生,到目前为止我找不到我的问题的具体答案,所以这里是我的问题:

我正在使用 android studio 2.2.3

当我点击第一张图片时,它会变成第二张图片

public void fade(View view) {

        ImageView ja1 = (ImageView) findViewById(R.id.ja1);

        ImageView ja2 = (ImageView) findViewById(R.id.ja2);

        ja1.animate().alpha(0f).setDuration(2000);

        ja2.animate().alpha(1f).setDuration(2000);


    }

现在,当我想要制作已经可见的第二张图像以淡入淡出并且使用淡入淡出返回第一张图像时,什么也没有发生!我的应用程序甚至不会做任何事情!我只是停留在第一张图片上,这是我尝试使用的代码:

public void fade(View view) {

        ImageView ja1 = (ImageView) findViewById(R.id.ja1);

        ImageView ja2 = (ImageView) findViewById(R.id.ja2);

        ja1.animate().alpha(0f).setDuration(2000);


        ja2.animate().alpha(1f).setDuration(2000);


        ja2.animate().alpha(0f).setDuration(2000);


        ja1.animate().alpha(1f).setDuration(2000);

    }

如果有人能帮助我,我将非常感激,

谢谢你。

更新:这是我的 xml 代码,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dlx.myapplication.MainActivity"
    android:onClick="fade2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ja1"
        android:id="@+id/ja1"
        android:onClick="fade" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ja2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/ja2"
        android:alpha="0" />
</RelativeLayout>

最佳答案

这不会起作用。你知道为什么??因为第一张图像仅淡入 alpha 0,但它仍然保留在另一张图像的顶部。所以您无法单击第二张图片。尝试使用 if 语句。祝你好运。

关于java - 如何让我的图像在 android studio 中淡出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42659226/

相关文章:

ios - 选择 tableview 元素时缓慢推送

java - 如何在android中的字符串资源中使用整数资源

java - IllegalStateException 未按需要显示

java - Spring Data Neo4j 未将类字段映射到节点属性

java try catch finally - 返回

android - Android Studio 是否跟踪使用情况?

java - fragment 中的 map [IllegalArgumentException : Binary XML file line #7: Duplicate id]

java - 正则表达式在Java中捕获匹配模式之前的单词

java - android studio中的自定义facebook登录按钮

android - 打字时布局发生变化