显示和删除图像几秒钟
Show and remove an image for some seconds
我需要显示一个图像 1 秒,在该时间结束时使其不可见 1 秒,然后在时间结束时再次显示,因为我遍历了一个列表,它不再显示给我下面的照片。
但是我的代码只显示了 1 秒的图像,但下面的图像没有显示它们。
这是我的代码:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNextImage() {
// loads the image at position currentPosition
final Bits item = L.get(currentPosition);
imageBit.setImageBitmap(BitmapFactory.decodeFile(item.getbImage()));
handler.postDelayed(new Runnable() {
@Override
public void run() {
imageBit.setImageBitmap(BitmapFactory.decodeFile(item.getbImage()));
}
},1000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
nameBit.setText(item.getbText());
imageBit.setVisibility(View.GONE);
}
},1000);
currentPosition++; // updates the current position
if (L.size() > currentPosition) { // more images to show?
// loads the next image after some delay
handler.postDelayed(new Runnable() {
@Override
public void run() {
showNextImage();
}
}, 1000); // in millis, 1000 for one second delay
}
}
您可以使用 Handler
来完成。拿个flag就可以了isShowing
。以下是大概的思路,大家可以根据自己的需要进行修改。
Runnable runnable = new Runnable() {
@Override
public void run() {
showHideImage();
}
};
private void showHideImage() {
if(isShowing) {
isShowing = false;
showImage(); //Do whatever you want to
} else {
isShowing = true;
hideImage();
}
yourImageView.postDelayed(runnable, ONE_SECOND);
}
你只需要在开始时调用showHideImage()
一次,其余的将自行处理。
它是这样工作的:
声明...
private int currentPosition = 0;
public ArrayList<Bitmap> ItemsBitmap;
public Bitmap bitmap;
public Bits items;
初始化:
ItemsBitmap = new ArrayList<>();
for(int i = 0; i < L.size(); i++){
Bits item = L.get(i);
ItemsBitmap.add(BitmapFactory.decodeFile(item.getbImage()));
}
for (int x = 0; x < L.size(); x++){
Bits item = L.get(x);
nameBit.setText(item.getbText());
}
showImages();
方法:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showImages() {
new CountDownTimer(2000, 1000) {
boolean continuar = true; // Si quieres que se detenga, vuelve continuar false en algun momento
int number = 0;
public void onTick(long millisUntilFinished) {
if(number % 2 != 0) {
if(number == 101)
number = -1; // esto hace que la bandera no crezca mucho
imageBit.setVisibility(View.VISIBLE);
nameBit.setVisibility(View.GONE);
}
else {
bitmap = ItemsBitmap.get(currentPosition);
imageBit.setImageBitmap(bitmap);
items = L.get(currentPosition);
nameBit.setText(items.getbText());
currentPosition++;
if (ItemsBitmap.size() == currentPosition) {
currentPosition = 0; // Esto hace que se vuelva a repetir la lista de Bitmaps
}
imageBit.setVisibility(View.GONE);
if (L.size() == currentPosition){
currentPosition = 0;
}
nameBit.setVisibility(View.VISIBLE);
}
number++;
}
public void onFinish() {
if (continuar) {
this.start();
}
}
}.start();
}
感谢您的帮助!!
我需要显示一个图像 1 秒,在该时间结束时使其不可见 1 秒,然后在时间结束时再次显示,因为我遍历了一个列表,它不再显示给我下面的照片。
但是我的代码只显示了 1 秒的图像,但下面的图像没有显示它们。
这是我的代码:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNextImage() {
// loads the image at position currentPosition
final Bits item = L.get(currentPosition);
imageBit.setImageBitmap(BitmapFactory.decodeFile(item.getbImage()));
handler.postDelayed(new Runnable() {
@Override
public void run() {
imageBit.setImageBitmap(BitmapFactory.decodeFile(item.getbImage()));
}
},1000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
nameBit.setText(item.getbText());
imageBit.setVisibility(View.GONE);
}
},1000);
currentPosition++; // updates the current position
if (L.size() > currentPosition) { // more images to show?
// loads the next image after some delay
handler.postDelayed(new Runnable() {
@Override
public void run() {
showNextImage();
}
}, 1000); // in millis, 1000 for one second delay
}
}
您可以使用 Handler
来完成。拿个flag就可以了isShowing
。以下是大概的思路,大家可以根据自己的需要进行修改。
Runnable runnable = new Runnable() {
@Override
public void run() {
showHideImage();
}
};
private void showHideImage() {
if(isShowing) {
isShowing = false;
showImage(); //Do whatever you want to
} else {
isShowing = true;
hideImage();
}
yourImageView.postDelayed(runnable, ONE_SECOND);
}
你只需要在开始时调用showHideImage()
一次,其余的将自行处理。
它是这样工作的:
声明...
private int currentPosition = 0;
public ArrayList<Bitmap> ItemsBitmap;
public Bitmap bitmap;
public Bits items;
初始化:
ItemsBitmap = new ArrayList<>();
for(int i = 0; i < L.size(); i++){
Bits item = L.get(i);
ItemsBitmap.add(BitmapFactory.decodeFile(item.getbImage()));
}
for (int x = 0; x < L.size(); x++){
Bits item = L.get(x);
nameBit.setText(item.getbText());
}
showImages();
方法:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showImages() {
new CountDownTimer(2000, 1000) {
boolean continuar = true; // Si quieres que se detenga, vuelve continuar false en algun momento
int number = 0;
public void onTick(long millisUntilFinished) {
if(number % 2 != 0) {
if(number == 101)
number = -1; // esto hace que la bandera no crezca mucho
imageBit.setVisibility(View.VISIBLE);
nameBit.setVisibility(View.GONE);
}
else {
bitmap = ItemsBitmap.get(currentPosition);
imageBit.setImageBitmap(bitmap);
items = L.get(currentPosition);
nameBit.setText(items.getbText());
currentPosition++;
if (ItemsBitmap.size() == currentPosition) {
currentPosition = 0; // Esto hace que se vuelva a repetir la lista de Bitmaps
}
imageBit.setVisibility(View.GONE);
if (L.size() == currentPosition){
currentPosition = 0;
}
nameBit.setVisibility(View.VISIBLE);
}
number++;
}
public void onFinish() {
if (continuar) {
this.start();
}
}
}.start();
}
感谢您的帮助!!