Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:如何通过点击按钮循环浏览不同的图像?_Java_Android_Android Studio - Fatal编程技术网

Java 安卓:如何通过点击按钮循环浏览不同的图像?

Java 安卓:如何通过点击按钮循环浏览不同的图像?,java,android,android-studio,Java,Android,Android Studio,在我的android程序中,我想点击一个按钮,在不同的交通灯图像之间循环。每当应用程序加载时,它会以一个红灯的图像开始,当我单击它时,我希望它将绿灯变为绿灯,然后再次单击变为黄灯。这就是我的Java文件中的内容 package com.example.trafficsimulator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; imp

在我的android程序中,我想点击一个按钮,在不同的交通灯图像之间循环。每当应用程序加载时,它会以一个红灯的图像开始,当我单击它时,我希望它将绿灯变为绿灯,然后再次单击变为黄灯。这就是我的Java文件中的内容

package com.example.trafficsimulator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    public void stopButton(View view){
        final Button button = findViewById(R.id.button);
        ImageView image = findViewById(R.id.redLightImage);
        button.setBackgroundColor(getResources().getColor(R.color.yellowlight));

        button.setText("Go");
        image.setImageResource(R.drawable.yellowlight);


    }
和XML文件

   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/redLightImage"
        android:layout_width="217dp"
        android:layout_height="372dp"
        android:layout_marginStart="97dp"
        android:layout_marginTop="61dp"
        android:layout_marginEnd="97dp"
        android:layout_marginBottom="298dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/redlight" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="156dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="167dp"
        android:layout_marginBottom="173dp"
        android:background="#BA1C1C"
        android:onClick="stopButton"
        android:text="@string/stop"
        android:textColor="@android:color/white"
        android:textColorHint="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/redLightImage"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

您好(我无法理解您的问题,但我上传了解决方案,如果这不起作用,请告诉我,我会尝试给出更好的答案)您可以在这里做一件事

最初,将所有图像(红色、黄色、绿色)添加到应用程序xml文件中,所有图像都位于同一位置,然后从属性(红色图像部分除外)中将其中两个图像的alpha设置为“0”,然后每次单击按钮,将当前图像的alpha设置为零,将第二个图像的alpha设置为零。。。诸如此类 所以代码是这样的

boolean isred =true; //for red image
boolean isyellow = false; //for yellow image
boolean isgreen = false; // for green image
//button onclick listener
public void ChangeImage(View view){

  if(isred == true){
        RedImageView.animate().alpha(0).setduration(500); //500 milli-seconds it will 
        //give an animation effect
       YellowImageView.animate().alpha(1).setduration(500);
       isyellow = true;
       isred = false;
   }
if(isyellow == true){

    YellowImageView.animate().alpha(0).setduration(500);
    GreenImageView.animate().alpha(1).setduration(500);
    isyellow = false;
    isgreen = true;
 }
if(isgreen == true){
     GreenImageView.animate().alpha(0).setduration(500);
     isgreen = false;
     // now if you want to continue the same thing then you must add the below code
     RedImageView.animate().alpha(1).setduration(500);
     isred = true;
}
   
}

你可以像这样实现你的目标

boolean isred =true; //for red image
boolean isyellow = false; //for yellow image
boolean isgreen = false; // for green image
//button onclick listener
public void ChangeImage(View view){

  if(isred == true){
        RedImageView.animate().alpha(0).setduration(500); //500 milli-seconds it will 
        //give an animation effect
       YellowImageView.animate().alpha(1).setduration(500);
       isyellow = true;
       isred = false;
   }
if(isyellow == true){

    YellowImageView.animate().alpha(0).setduration(500);
    GreenImageView.animate().alpha(1).setduration(500);
    isyellow = false;
    isgreen = true;
 }
if(isgreen == true){
     GreenImageView.animate().alpha(0).setduration(500);
     isgreen = false;
     // now if you want to continue the same thing then you must add the below code
     RedImageView.animate().alpha(1).setduration(500);
     isred = true;
}
   
}
主要活动

    Button buttonChangeLight;
    ImageView imageLight;
    int counter = 0;
    
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonChangeLight = findViewById(R.id.button);
        imageLight = findViewById(R.id.redLightImage);


        //to change lights
        changeLight();
    }

    //change you light
    public void changeLight(){
        buttonChangeLight.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(counter == 2){
           counter = 0;
           image.setImageResource(R.drawable.redLight);
        }else if(counter == 1){
           counter++;
           image.setImageResource(R.drawable.yellowLight);
        }else if(counter == 2){
           counter++;
           image.setImageResource(R.drawable.greenLight);
        }
      }
    });
  }