Java 更改按钮上的图像单击
我是android开发的新手,在这里我尝试通过点击按钮来改变图像,我使用动画方法来淡出其中一幅图像并获得效果。第一次点击按钮是淡出图像,但第二次点击这个按钮时,它不会给出任何结果图像不会淡出 给我任何可能的解决办法。谢谢 .java文件Java 更改按钮上的图像单击,java,android,Java,Android,我是android开发的新手,在这里我尝试通过点击按钮来改变图像,我使用动画方法来淡出其中一幅图像并获得效果。第一次点击按钮是淡出图像,但第二次点击这个按钮时,它不会给出任何结果图像不会淡出 给我任何可能的解决办法。谢谢 .java文件 package e.nani.bestimage; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; imp
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);
}
}