单击按钮更改图像

我是 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" />


慕尼黑的夜晚无繁华
浏览 68回答 1
1回答

一只名叫tom的猫

每当调用 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){&nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img1.animate().alpha(0f).setDuration(2000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img2.animate().alpha(1f).setDuration(2000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 1:&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; img1.animate().alpha(1f).setDuration(2000);&nbsp; &nbsp; &nbsp; &nbsp; img2.animate().alpha(0f).setDuration(2000);&nbsp; &nbsp; &nbsp; &nbsp; flag=0;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; }}&nbsp;@Override&nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp;setContentView(R.layout.activity_main);&nbsp; &nbsp;flag = 0;&nbsp; &nbsp;ImageView img1=(ImageView) findViewById(R.id.single);&nbsp; &nbsp;ImageView img2=(ImageView) findViewById(R.id.withands);&nbsp; &nbsp;img1.animate().alpha(1f).setDuration(2000);&nbsp; &nbsp;img2.animate().alpha(0f).setDuration(2);&nbsp;}&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java