在Android线程上更改图像 [英] Changing image on Android thread
问题描述
我经历了stackoverflow和所有类似的答案-但发现它们没有用.
I've gone through stackoverflow and all the similar answers - but have found them not of use.
任何人都可以指出为什么这行不通吗?它应该很简单:每6秒钟更新一次图片(每3分钟更新一次,但为了进行测试,我将其设为6秒钟).
Can anyone please point out why this is not working? It should be simple: update the image every 6 seconds (will be every 3 mins but for testing I've put it as 6 seconds).
代码的作用是-逐步浏览4个图像中的每个图像,但不更改图像.但是,当它到达最后一张图像时,它确实会更改它(??).
What the code does is - goes through each of the 4 images step by step but does not change the image. HOWEVER when it gets to the last image - it does change it(??).
问题:
- 这是怎么了?
- 为什么只更新最后一张图片?
- 如果我仅向处理程序添加"post"而不是"postDelayed"来启动代码-平板电脑只会停留在黑屏上.为什么我必须延迟启动代码才能使其正常工作?
int currentIndex = 0;
boolean start = false;
ImageView descriptionImage;
private Runnable timerRunnable;
long startTime = 0;
private Handler handler;
int[] pictureIds = new int[]{R.drawable.hang, R.drawable.dog, R.drawable.coffee, R.drawable.about};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pictureDescription = new ArrayList<String>();
handler = new Handler(Looper.getMainLooper());
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_picture_test, container, false);
descriptionImage = (ImageView) v.findViewById(R.id.iv_picture);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
start();
}
private void start() {
start = true;
startTime = System.currentTimeMillis();
loadImage();
timerRunnable = new Runnable() {
@Override
public void run() {
while (start) {
long time = System.currentTimeMillis() - startTime;
float mins = (float) time / (60 * 1000);
if (mins >= 0.1f) {
startTime = System.currentTimeMillis();
if (currentIndex < pictureIds.length) {
currentIndex++;
loadImage();
}
}
}
}
};
handler.postDelayed(timerRunnable, 1000);
}
private void loadImage() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentIndex < pictureIds.length) {
descriptionImage.setImageResource(pictureIds[currentIndex]);
} else {
start = false; // finish
}
}
});
}
谢谢!
如果我也将它发布到imageview线程而不是处理程序,则不起作用.
Doesn't work if I post it to the imageview thread either, instead of the handler.
descriptionImage.postDelayed(timerRunnable,1000);
descriptionImage.postDelayed(timerRunnable, 1000);
推荐答案
这是怎么了?
当UI线程繁忙时( while(start)
),等待条件变为真( if(mins> = 0.1f){
),它无法照顾其余的工作(例如绘图).
while the UI Thread is busy ( while (start)
) , waiting to your condition to became true ( if (mins >= 0.1f) {
), it can't take care of the rest (like drawing).
Why would it only update the last image?
因为它只能在 if(currentIndex< pictureIds.length){
不为真, start
为假,并且UI线程最终可以绘制时才绘制>
because it can draw only when if (currentIndex < pictureIds.length) {
is not true, start
becomes false, and the UI thread can finally draw
如果我仅通过处理程序的"post"(而不是一个"post")来启动代码"postDelayed"-平板电脑仅停留在黑屏上.我为什么会必须延迟启动代码才能使其正常工作?
If I start the code with just a "post" to the handler instead of a "postDelayed" - the tablet just stays with a black screen. Why do I have to start the code with a delay to make it work?
请参阅第1点.
如果您想每3秒更改一次图像,则可以继续使用Handler#postDelayed.例如
if you want to change your image every 3 seconds, you could keep using Handler#postDelayed. E.g.
start = true;
startTime = System.currentTimeMillis();
loadImage();
timerRunnable = new Runnable() {
@Override
public void run() {
currentIndex = currentIndex++ % pictureIds.length;
loadImage();
handler.postDelayed(this, 3000);
}
};
handler.postDelayed(timerRunnable, 3000);
这篇关于在Android线程上更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!