【发布时间】: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文件夹中