如何为我的Android应用实现旋转横幅?

时间:2018-12-21 23:21:54

标签: android image banner

在我的登录活动中,我想要一个横幅(实际上是3个),该横幅将在特定时间切换。就像网站上的新闻一样。带有箭头可以传递到下一个箭头,但主要是自动传递。

我想要的示例,当我滑动或等待时,Tinder登录屏幕上的这些图像会切换: Screenshot of Tinder app with carousel image

如何在Android上执行类似的操作?有办法本地做吗?还是至少有外部资源?

1 个答案:

答案 0 :(得分:0)

尝试此代码,

public class TestActivity  extends AppCompatActivity {

    int images[] = {R.drawable.ic_earth, R.drawable.oval_btn,
             R.drawable.ic_group_black_24dp, R.drawable.ic_earth};
    ImagePagerAdapter myAdapter;

            @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.tests);

                ViewPager viewPager = findViewById(R.id.viewPager);

                myAdapter = new ImagePagerAdapter(TestActivity.this, images);
                viewPager.setAdapter(myCustomPagerAdapter);
                setupViewPager(viewPager);

            }

            int currentPage = 0;
            Timer timer;
            final long DELAY_MS = 500;
            final long PERIOD_MS = 3000;
            int NUM_PAGES = 4;

            private void setupViewPager(final ViewPager viewPager) {


                final Handler handler = new Handler();
                final Runnable Update = new Runnable() {
                    public void run() {
                        if (currentPage == NUM_PAGES) {
                            currentPage = 0;
                        }
                        viewPager.setCurrentItem(currentPage++, true);
                    }
                };

                timer = new Timer(); // This will create a new Thread
                timer .schedule(new TimerTask() { // task to be scheduled

                    @Override
                    public void run() {
                        handler.post(Update);
                    }
                }, DELAY_MS, PERIOD_MS);

            }
            public class ImagePagerAdapter extends PagerAdapter {

                Context context;
                int images[];
                LayoutInflater layoutInflater;


                public ImagePagerAdapter(Context context, int images[]) {
                    this.context = context;
                    this.images = images;
                    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }

                @Override
                public int getCount() {
                    return images.length;
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == ((LinearLayout) object);
                }

                @Override
                public Object instantiateItem(ViewGroup container, final int position) {
                    View itemView = layoutInflater.inflate(R.layout.item, container, false);

                    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
                    imageView.setImageResource(images[position]);

                    container.addView(itemView);

                    //listening to image click
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    });

                    return itemView;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView((LinearLayout) object);
                }
            }
        }