Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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_Arrays_Imageview - Fatal编程技术网

Java 显示图像的计数器

Java 显示图像的计数器,java,android,arrays,imageview,Java,Android,Arrays,Imageview,我有一个可以工作的应用程序,图像从服务器下载,本地存储,每隔几秒钟以幻灯片的形式显示在图像视图中。我想计算每个图像显示的次数。我不知道如何解决这个问题。我应该创建6个计数器(每个图像一个)还是创建一个数组来存储图像名称和计数?但是我如何更新它呢 任何帮助或代码片段都将不胜感激 protected void onPostExecute(String s) { super.onPostExecute(s); imageView = findViewById(R.id

我有一个可以工作的应用程序,图像从服务器下载,本地存储,每隔几秒钟以幻灯片的形式显示在图像视图中。我想计算每个图像显示的次数。我不知道如何解决这个问题。我应该创建6个计数器(每个图像一个)还是创建一个数组来存储图像名称和计数?但是我如何更新它呢

任何帮助或代码片段都将不胜感激

 protected void onPostExecute(String s) {
        super.onPostExecute(s);
        imageView = findViewById(R.id.imgholder);
        Timer mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                // As timer is not a Main/UI thread need to do all UI task on runOnUiThread
                DashboardActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // increase your position so new image will show
                        position++;
                        // check whether position increased to length then set it to 0
                        // so it will show images in circuler
                        if (position >= 6)
                            position = 0;
                        File file = new File(Environment.getExternalStorageDirectory(), user.fullName + "-" + position + ".jpg");
                        imgUri = Uri.fromFile(file);
                        Log.v("test", "Now Showing Image: " + imgUri.toString());

                        // Set Image
                        imageView.setImageURI(imgUri);
                        counter++;
                        Log.v("test","Image: "+ position +" has appeared: " +counter);
                    }
                });
            }
        }, 0, DELAY_TIMER);

创建一个HashMap来存储计数

private HashMap<String, Integer> countMap = new HashMap<>();
以下代码将返回显示计数

int count = countMap.get(file.getName())

创建一个长度为6的整数数组
int[]counters=newint[]{0,0,0,0,0}
,然后在
之后如果(位置>=6)位置=0在代码中,将计数器作为
计数器[position]++递增。因此,您可以始终从阵列中跟踪特定位置的图像计数
计数器

来自服务器的图像的数量是否固定?号码会变吗?或者正在从服务器刷新图像?图像编号是固定的。将下载6幅图像。图像可能会从服务器更改,但它们将始终为6。每个图像是否具有唯一的id?您应该使用类似的方法来区分每个图像images@Antonio是,每个图像都有唯一的ID。图像名为0-5。谢谢。我会测试一下,然后再给你回复。这个答案也是正确的。这是一种更整洁的方式。但就我的用法而言,另一个答案更简单。如果图像数量可变,请使用Hashmap。如果图像已修复,则可以使用提供的更简单的解决方案below@SaeedJoul如果你觉得有用,请投票。谢谢,谢谢。我会测试一下,然后再给你回复。
int count = countMap.get(file.getName())