How to swipe ViewPager images automatically using TimeTask

后端 未结 13 2238
天涯浪人
天涯浪人 2020-12-25 13:54

Hi I am new in android and in my app I have a lot of images in my ArrayList that \'s why I want to swipe those images automatically for every 3 seconds with he

13条回答
  •  暖寄归人
    2020-12-25 14:37

    Try to use ViewFlipper instead of viewpager

    Layout xml

    
    
    
    
    
    
    

    Activity

    public class SlideShowActivity extends Activity  {
    
    
    
    ViewFlipper imageFrame;
    Button slideShowBtn;
    Handler handler;
    Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_slideshow_main);
        imageFrame = (ViewFlipper) findViewById(R.id.imageFrames);
    
                //get sd card path for images
    
        File parentFolder = new
         File(Environment.getExternalStorageDirectory()
         .getAbsolutePath()
         + "/images");
    
        addFlipperImages(imageFrame, parentFolder);
    
        handler = new Handler();
        imageFrame.setOnClickListener(SlideShowActivity.this);
    
        slideShowBtn = (Button) findViewById(R.id.slideShowBtn);
        slideShowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
    
                runnable = new Runnable() {
    
                    @Override
                    public void run() {
                        handler.postDelayed(runnable, 3000);
                        imageFrame.showNext();
    
                    }
                };
                handler.postDelayed(runnable, 500);
            }
        });
    
    }
    
    private void addFlipperImages(ViewFlipper flipper, File parent) {
        int imageCount = parent.listFiles().length;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        for (int count = 0; count < imageCount - 1; count++) {
            ImageView imageView = new ImageView(this);
            Bitmap imbm = BitmapFactory.decodeFile(parent.listFiles()[count]
                    .getAbsolutePath());
            imageView.setImageBitmap(imbm);
            imageView.setLayoutParams(params);
            flipper.addView(imageView);
        }
    
    }
    
    }
    
    @Override
    public void onClick(View view) {
    
    }
    }
    

提交回复
热议问题