首页 > 解决方案 > 交叉淡入淡出图像视图

问题描述

嗨,我对交叉淡入淡出的图像有疑问

1)在我的项目中,我有两个图像,第一个图像具有 alpha =1 和 oncllick = myDhoni1,第二个图像具有 alpha = 0,因此当我在模拟器中运行应用程序时,第一个图像最初才可见。

现在我有一个疑问,如果我单击第一个图像它会淡出,第二个图像会淡入,如果我单击第二个图像淡出和第一个图像淡入,但在这里我只为第一个图像指定了 onClick 函数,所以如果我点击第二张图片代码不应该工作bcz我没有为第二张图片指定onClick功能,但代码工作正常,这会产生疑问和

2)我想交叉淡入淡出如何工作?

代码:

package com.example.aravi.demo_0501;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

public void myDhoni1(View dhoni){
    ImageView a = (ImageView) findViewById(R.id.imageView);
    ImageView b = (ImageView) findViewById(R.id.imageView2);
    if(b.getAlpha()==0f){
        b.animate().alpha(1f).setDuration(2500);
        a.animate().alpha(0f).setDuration(2500);
    }else{
        b.animate().alpha(0f).setDuration(2500);
        a.animate().alpha(1f).setDuration(2500);
    }
    Toast.makeText(MainActivity.this,"Image is 
 pressed",Toast.LENGTH_SHORT).show();
}
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:onClick="myDhoni1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/dhoni1" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:alpha="0"
    app:layout_constraintBottom_toBottomOf="@+id/imageView"
    app:layout_constraintEnd_toEndOf="@+id/imageView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/dhoni" />
 </android.support.constraint.ConstraintLayout>

标签: androidandroid-layoutandroid-fragments

解决方案


这里,s的alpha属性发生了Crossfade imageview,一旦你点击图像并让第二个imageview出现,第一个图像alpha变成0,这并不意味着图像已经从这个地方消失了。视图仍然存在,但完全透明。所以第一张图片上的点击事件仍然适用

您可以在您的情况下执行此解决方法:在 myDhoni1 方法中,在引用 imageviews 后,添加以下行:

ImageView a = (ImageView) findViewById(R.id.imageView);
ImageView b = (ImageView) findViewById(R.id.imageView2);

if(a.getAlpha()==0f)
     return;

我们所做的是,一旦第一张图像变得透明,我们就不会让点击监听器执行


推荐阅读