【问题标题】:How to set ImageView Looping while i only have limited images ? (Android)当我只有有限的图像时如何设置 ImageView 循环? (安卓)
【发布时间】:2016-01-27 22:03:51
【问题描述】:

我有数组drawable,数组drawable只有4个图像,我可能超过4个ImageView,如何通过循环在ImageView上设置图像,当循环达到4个以上时,我希望它返回第一张图片,所以它会是这样的:

  • 带有第一个可绘制对象的 ImageView 1
  • 带有第二个可绘制对象的 ImageView 2
  • 带有第三个可绘制对象的 ImageView 3
  • 带有第四个可绘制对象的 ImageView 4
  • 带有第一个可绘制对象的 ImageView 5
  • 带有第二个可绘制对象的 ImageView 6

等等..

如何做到这一点? 任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 检查图像阵列位置并将其重置为 4.. 在您的循环中
  • 提示:将%/Arrays 一起使用
  • This 可能会给你一些提示
  • 所以,它是“图像 [i % images.length]”?

标签: android arrays loops imageview


【解决方案1】:

你可以使用 % 操作符。它返回除法的余数。 例如,如果您有 6 个视图和 4 个图像,您可以这样做:

ImageView[] viewsArray = new ImageView[6];
//init your array with findViewById()

for(int i = 0; i < viewsArray.length; ++i) {
   viewsArray[i].setImageDrawable(imagesArray[i % 4]); // if your index is 5 % operator will return 1.  
}    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多