gpt4 book ai didi

java - 单击按钮更改图像

转载 作者:行者123 更新时间:2023-12-02 10:10:53 25 4
gpt4 key购买 nike

我是 Android 开发新手,这里我尝试在单击按钮时更改图像,我使用 animate() 方法淡入其中一个图像并实现结果。第一次按钮单击使图像淡入淡出,但第二次单击时图像淡入淡出我点击这个按钮,它没有给出任何结果(图像不会褪色)。

给我任何可能的解决方案。谢谢

.java 文件

package e.nani.bestimage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

public void change(View view) {
int flag=0;
ImageView img1 = (ImageView) findViewById(R.id.single);
ImageView img2 = (ImageView) findViewById(R.id.withands);
switch(flag){

case 0:img1.animate().alpha(0f).setDuration(2000);
img2.animate().alpha(1f).setDuration(2000);
flag=1;
break;
case 1: img1.animate().alpha(1f).setDuration(2000);
img2.animate().alpha(0f).setDuration(2000);
flag=0;
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img1=(ImageView) findViewById(R.id.single);
ImageView img2=(ImageView) findViewById(R.id.withands);
img1.animate().alpha(1f).setDuration(2000);
img2.animate().alpha(0f).setDuration(2);

}

}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple"
tools:context=".MainActivity">

<ImageButton
android:id="@+id/single"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="@drawable/imgthree" />

<ImageView
android:id="@+id/withands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
app:srcCompat="@drawable/imgsix" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="98dp"
android:alpha="0.7"
android:background="@android:color/background_dark"
android:onClick="change"
android:text="find out the person"
android:textColor="@android:color/holo_blue_bright" />
</android.widget.RelativeLayout>

最佳答案

每当调用change()方法时,它都会将标志设置为0。试试这个:

public class MainActivity extends AppCompatActivity {

int flag;
public void change(View view) {

ImageView img1 = (ImageView) findViewById(R.id.single);
ImageView img2 = (ImageView) findViewById(R.id.withands);
switch(flag){

case 0:
img1.animate().alpha(0f).setDuration(2000);
img2.animate().alpha(1f).setDuration(2000);
flag=1;
break;
case 1:
img1.animate().alpha(1f).setDuration(2000);
img2.animate().alpha(0f).setDuration(2000);
flag=0;
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flag = 0;
ImageView img1=(ImageView) findViewById(R.id.single);
ImageView img2=(ImageView) findViewById(R.id.withands);
img1.animate().alpha(1f).setDuration(2000);
img2.animate().alpha(0f).setDuration(2);

}

}

关于java - 单击按钮更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55009982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com