【问题标题】:Display a random image from array in ImageView after 5000 milliseconds5000 毫秒后在 ImageView 中显示来自数组的随机图像
【发布时间】:2016-02-18 09:25:45
【问题描述】:

我有一个字符串数组,在按下按钮后和 5000 毫秒后在 TextView 中显示随机文本结果。我想要实现的是拥有一组图像而不是文本,因此希望在 5000 毫秒后在ImageView 中显示随机图像。

public class MainActivity extends AppCompatActivity {
    private ImageView thumbPrint;
    private TextView result;
    private AnimationDrawable thumbAnimation;
    private String[] moodResults;
    private Runnable mRunnable;

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

        moodResults = new String[]{
            "result a",
            "result b",
            "result c",
            "result d",
            "result e",
            "result f"
        };

        thumbPrint = (ImageView)findViewById(R.id.thumbPrint);
        thumbPrint.setBackgroundResource(R.drawable.thumb_animation);
        thumbAnimation = (AnimationDrawable)thumbPrint.getBackground();

        result = (TextView)findViewById(R.id.resultText);

        thumbPrint.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                thumbAnimation.start();
                showResult();
                return true;
            }
        });
    }

    public void showResult(){
        mRunnable = new Runnable() {
            @Override
            public void run() {
                int rand = (int)(Math.random()* moodResults.length);
                result.setText(moodResults[rand]);
                //stop animation
                thumbAnimation.stop();
            }
        };

        Handler mHandler = new Handler();
        mHandler.postDelayed(mRunnable, 5000);
    }
}

【问题讨论】:

  • 图片会保存在drawable文件夹中

标签: java random imageview


【解决方案1】:

这样实现:

这些变量在 MainActivity 中声明。它们可以在onCreate()中单独初始化

 final int[] imageIds= {
            R.drawable.image1, R.drawable.image2, ...}; // This is your array with resource id of each image
 Random r = new Random();
 Handler mHandler = new Handler();

这是showResult()的重构代码:

public void showResult(){
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    int rand = (int)(Math.random()* moodResults.length);
                    result.setText(moodResults[rand]);
                    int randomInt = r.nextInt(imageIds.length);
                    thumbprint.setBackgroundResource(imageIds[randomInt]); //thumbprint is your Imageview
                    //stop animation
                    thumbAnimation.stop();
                    mHandler.postDelayed(mRunnable, 5000);//This causes hanlder to called again in 5 seconds
                }
            };

            mHandler.postDelayed(mRunnable, 5000); //Here handler is called the first time; the code in mRunnable will execute after 5 seconds 
        }

【讨论】:

  • 我可以将它链接到按钮按下的 onClick 方法吗?或者只是替换现有的文本数组
  • 如果您不需要 TextView 并且您只是在试验,则将其与字符串数组一起删除。如果您想将随机图像及其描述显示为文本,请将 String 和 int 数组一起初始化,并将其他所有内容放在您希望更改图像的位置。
  • 似乎在 (imageIds.length) 方法下给我一个错误预期调用
  • 我做了一些改变。改用 imageIds.length。
  • 尝试了新代码,应用程序运行但图像一直存在,我希望在 5000 毫秒和动画后显示随机图像
【解决方案2】:

我会创建一个带有图像路径的字符串数组,然后选择一个随机路径并显示该图像。

【讨论】:

  • 试过了,但似乎无法让它工作,我用一组图像代替字符串文本数组,但对我不起作用!让它在 5000 毫秒后和动画后显示随机图像是我发现的困难
猜你喜欢
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
相关资源
最近更新 更多