Android 为什么setImageResource需要不同的阵列来打印图像,以及其余部分如何在图像中工作?

Android 为什么setImageResource需要不同的阵列来打印图像,以及其余部分如何在图像中工作?,android,arrays,Android,Arrays,请解释上述代码当前图像索引,它是如何工作的?您有一个可绘制的数组: package com.example.imageview; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public clas

请解释上述代码当前图像索引,它是如何工作的?

您有一个可绘制的数组:

package com.example.imageview;

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 {
        private static ImageView saadaimageview;
        private static Button saadabutton;
        private static int currentimageindex;
        int[] naviimages = {
            R.drawable.hhsd,
            R.drawable.second,
            R.drawable.third
        };


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            saadaimagelistener();
        }
        public void saadaimagelistener() {
            saadaimageview = (ImageView) findViewById(R.id.ourfirstimage);
            saadabutton = (Button) findViewById(R.id.button);
            saadabutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    currentimageindex++;
                    currentimageindex = currentimageindex % naviimages.length;
                    saadaimageview.setImageResource(naviimages[currentimageindex]);
                }
            });
        }
    }
每次单击
saadButton
currentimageindex
增加1

int[] naviimages = {
    R.drawable.hhsd,
    R.drawable.second,
    R.drawable.third
};
currentImageIndex
的值取剩余的
currentImageIndex
除以可绘制的
数组的长度

currentimageindex++;
示例:

currentimageindex=1
naviimages.length=3

然后:

currentimageindex
将取1(剩余的1/3)


因此,
saadaimageview
将把
R.drawable.hhsd
作为图像资源

您有一个drawable数组:

package com.example.imageview;

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 {
        private static ImageView saadaimageview;
        private static Button saadabutton;
        private static int currentimageindex;
        int[] naviimages = {
            R.drawable.hhsd,
            R.drawable.second,
            R.drawable.third
        };


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            saadaimagelistener();
        }
        public void saadaimagelistener() {
            saadaimageview = (ImageView) findViewById(R.id.ourfirstimage);
            saadabutton = (Button) findViewById(R.id.button);
            saadabutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    currentimageindex++;
                    currentimageindex = currentimageindex % naviimages.length;
                    saadaimageview.setImageResource(naviimages[currentimageindex]);
                }
            });
        }
    }
每次单击
saadButton
currentimageindex
增加1

int[] naviimages = {
    R.drawable.hhsd,
    R.drawable.second,
    R.drawable.third
};
currentImageIndex
的值取剩余的
currentImageIndex
除以可绘制的
数组的长度

currentimageindex++;
示例:

currentimageindex=1
naviimages.length=3

然后:

currentimageindex
将取1(剩余的1/3)


因此
saadaimageview
R.drawable.hhsd
作为图像资源

但是currentimageindex将从0
currentimageindex++开始第一次单击时它将变为1,但currentimageindex将从0开始
currentimageindex++它将在第一次单击时变为1