我正在Android上开发应用程序。
我对片段有问题,可以在下面找到代码。
其想法是让图像视图无限循环地显示图片列表。为了实现这一点,我创建了一个新的线程,以免阻塞UI线程。使用while(0 <5)语句创建一个无限循环。然后,我运行if ... else语句来检查我们要确定要转到的下一张图片。
使用处理程序来处理切换图片之间的10秒延迟。最后,另一个可运行对象负责向UI线程的发布。
这似乎是完成工作的一种非常复杂的方法,有人使用了简单的代码吗?
最重要的是,在我的代码中的某个地方,有一个错误。我找不到它,有人吗?
这是我的代码。
public class SecAct_Foto_Fragment extends Fragment {
int counter = 0;
View rootView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);
return rootView;
}
Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
while (0 < 5) {
//so far it loops only once
//you start with run_rocks and but_left
final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);
final ImageView three_but = (ImageView) rootView.findViewById(R.id.knoppen);
//create a runnable for the picture view
pic_view.post(new Runnable() {
@Override
public void run() {
//every 10 seconds, switch picture and button fragment
if (counter == 0) {
final Handler handler0 = new Handler();
handler0.postDelayed(new Runnable() {
@Override
public void run() {
pic_view.post(new Runnable() {
@Override
public void run() {
pic_view.setImageResource(R.drawable.run_mount);
}
});
counter = 1;
}
}, 10000L);
} else if (counter == 1) {
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
pic_view.post(new Runnable() {
@Override
public void run() {
pic_view.setImageResource(R.drawable.run_away);
}
});
counter = 2;
}
}, 10000L);
} else {
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
pic_view.post(new Runnable() {
@Override
public void run() {
pic_view.setImageResource(R.drawable.run_rocks);
}
});
counter = 0;
}
}, 10000L);
}
}
});
myThread.start();
}
}
});
}
答案 0 :(得分:0)
private class AsyncQueryRun extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
for (...){
////do what you want
runOnUiThread(new Runnable() {
@Override
public void run() {
///do what you want to be handled by UI thread
}});
SystemClock.sleep(60); ////wait as long as you want in mili sec.
}
}
@Override
protected void onPostExecute(Object o) {
}
}
答案 1 :(得分:0)
代替上面的代码,只需使用ViewFlipper或ImageSwitcher。
您可以将视图添加到ViewFlipper并可以使用setFlipInterval()设置延迟
请通过此链接 https://developer.android.com/reference/android/widget/ViewFlipper
答案 2 :(得分:0)
您可以通过以下方式使用Handler:
final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);
private int animationCounter = 1;
private Handler imageSwitcherHandler;
imageSwitcherHandler = new Handler(Looper.getMainLooper());
imageSwitcherHandler.post(new Runnable() {
@Override
public void run() {
switch (animationCounter++) {
case 1:
pic_view.setImageResource(R.drawable.run_mount);
break;
case 2:
pic_view.setImageResource(R.drawable.run_mount2);
break;
case 3:
pic_view.setImageResource(R.drawable.run_mount3);
break;
}
animationCounter %= 4;
if(animationCounter == 0 ) animationCounter = 1;
imageSwitcherHandler.postDelayed(this, 3000);
}
});
答案 3 :(得分:0)
我决定尝试@NehaK的解决方案,并使用ImageSwitcher View。
以XML添加了以下代码。
<ImageSwitcher
android:id="@+id/foto_groot_imageswitch"
android:layout_width="match_parent"
android:layout_height="220dp"
app:srcCompat="@drawable/run_rocks"
/>
然后在我的片段中使用它。
public class SecAct_Foto_Fragment extends Fragment {
int counter = 0;
View rootView;
private ImageSwitcher pic_image_switch;
private Handler pic_image_switch_handler;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);
/*Animation anim_in = AnimationUtils.loadAnimation(getActivity(), R.anim.enter_from_left);
pic_image_switch.setInAnimation(anim_in);*/
//pic_image_switch = new ImageSwitcher(getActivity());
pic_image_switch = (ImageSwitcher) rootView.findViewById(R.id.foto_groot_imageswitch);
pic_image_switch.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
return imageView;
}
});
pic_image_switch_handler = new Handler(Looper.getMainLooper());
pic_image_switch_handler.post(new Runnable() {
@Override
public void run() {
switch (counter) {
case 0:
pic_image_switch.setImageResource(R.drawable.run_mount);
break;
case 1:
pic_image_switch.setImageResource(R.drawable.run_away);
break;
case 2:
pic_image_switch.setImageResource(R.drawable.run_rocks);
break;
}
counter += 1;
if (counter == 3) {
counter = 0;
}
pic_image_switch.postDelayed(this, 1000);
}
});
return rootView;
}
}