java - 显示图像计数器
问题描述
我有一个工作应用程序,其中图像从服务器下载,存储在本地,并每隔几秒以幻灯片形式显示在图像视图中。我想计算每个图像显示了多少次。我不确定如何解决这个问题。我应该制作 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<>();
现在,每当您在以下方法中设置图像调用时:
private void updateDisplayCount(File file) {
if(countMap.containsKey(file.getName())) {
countMap.put(file.getName(), countMap.get(file.getName()) + 1);
} else {
countMap.put(file.getName(), 1);
}
}
以下代码将返回显示计数
int count = countMap.get(file.getName())
推荐阅读
- powershell - 从 PowerShell 中的另一个 ArrayList 中删除 ArrayList 中包含的项目
- spring - Spring Boot 两种身份验证方式:结合 LDAP 和基于令牌的身份验证
- javascript - AngularJS - 如果登录成功,则在页面之间导航(使用不同的 html 页面和控制器)
- c# - 如何在WPF中双击时获取富文本框中的当前偏移位置
- java - 我如何 DRY Android 改装电话?
- docker - docker run --rm -volume E:/Randomfolder 的 Docker-compose 等效项
- php - Laravel 5 24 小时循环应用
- java - 如何在java上更新txt文件
- php - 即使在括号内,如何将 x^(...) 转换为 x^{...}?纯正则表达式可能吗?
- sql - sql数据库约束