android - 交叉淡入淡出图像视图
问题描述
嗨,我对交叉淡入淡出的图像有疑问
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>
解决方案
这里,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;
我们所做的是,一旦第一张图像变得透明,我们就不会让点击监听器执行
推荐阅读
- python - 我需要在 Python 中的对象中获取 {{ }} 之间的值
- ios - 使用 jpasskit 制作的 Apple 通用类型钱包通行证缺少缩略图
- css - column-count 属性,不要包裹在包含元素上
- spring - `getDeclaredMethods()` 不显示在基于 Spring 框架的类中声明的私有方法
- javascript - 如何循环遍历所有元素
- cmake - CMake进行编译定义另一个的别名
- reactjs - PrivateRoute 呈现空白页面...在反应 js 中没有显示错误
- java - 如何打印存储为对象变量的任何类型的数组?
- ethereum - 使用 Node 运行 Compound 存储库
- php - 如何在 Woocommerce 中修改小部件的 innerHTML