安卓广告横幅

发布于 2024-10-17 11:40:00 字数 277 浏览 2 评论 0原文

我想实现一个旋转横幅。什么控件最适合这样的 UI?

这是它需要做的:

  1. 每个视图旋转 1 个图像
  2. 仅在滑动(或所谓的 onFling())时旋转它们
  3. 记住当前横幅的索引,并在用户重定向到另一个活动时显示下一个横幅
  4. 两个方向旋转都是无限的

因此,本质上,当您进入第一个活动时,会显示第一个索引。当您滑动时,它应该显示下一个或上一个图像,具体取决于滑动是向左还是向右。这应该在无限循环中迭代。

先感谢您

I want to implement a rotating banner. What control is best suitable for such UI?

here is what it needs to do:

  1. rotate 1 image per view
  2. rotate them only on swipe (or what's called onFling())
  3. remember the index of the current banner and show next banner when user is redirected onto another activity
  4. rotation is infinite in both directions

So essentially when you land on first activity the first index is shown. When you do swipe it should show next or previous image depending if the swipe is to the left or to the right. This should iterate in the infinite loop.

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里泪两行 2024-10-24 11:40:00

如果有人想要解决方案,这里是代码:

public class CustomBanner extends RelativeLayout {
        //================================================================================
        //==== declaration.
        protected List<Banner> _items = new ArrayList<Banner>();
        protected GestureDetector gestureDetector;
            protected View.OnTouchListener gestureListener;
            protected ImageView _banner = null;
        protected String TAG = "CustomBanner";
        //================================================================================

        //================================================================================
        //==== properties
        /**
         * This property pulls the banners from the state manager. If the banners are not there they will be pulled from the database.
         * */
        protected List<Banner> getBanners()
        {
            // get your banners however way you get them
            return this._items;
        }
        //================================================================================

        //================================================================================
        //==== constructors
        public CustomBanner(Context context, AttributeSet attributes)
        {
            super(context, attributes);
            this.Init(context, attributes);
        }
        //================================================================================

        //================================================================================
        //==== protected methods
        protected void Init(Context context, AttributeSet attributes)
        {
            //---- inflate our layout
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layoutInflater.inflate(R.drawable.controls_custombanner, this);

            this._banner = (ImageView)findViewById(R.id.imgBanner);

                // swipe detection on the image
             this.gestureDetector = new GestureDetector(new MyGestureDetector());
            this.gestureListener = new View.OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event) {
                        if (gestureDetector.onTouchEvent(event)) {
                            return true;
                        }
                        return false;
                    }
                };
                // leave this, it needs to be here in order to redirect the swipe correctly to touch gesture direction
               this._banner.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // leave blank, click is redirected to gesture 
                    }
                }); 

             this._banner.setOnTouchListener(gestureListener);

                // set banner image
                this.setBanner(this.getCurrentIndexByWndId());
        }


        protected int getCurrentIndexByWndId() {

            // add your logic here
            // you need to access your state to get the last saved id
        }

        /**
         * this function does nothing but set the banner image by the position from the array of banners. 
         * */
        protected void setBanner(int position)
        {
            _banner.setImageResource(this.getBanners().get(position).ResourceId);
        }

        /**
         * this function tests the validity of the index in the banners array and resets it to 0 if its not valid.
         * */
        protected int checkValidIndex(int currentIndex){
            if (currentIndex > this.getBanners().size()-1)
                currentIndex = 0;
            return currentIndex;
        }

        protected void processBannerClick(Banner clickedBanner){
            try
            {
                // here do your logic for the click
            }
            catch (Exception ex)
            {
                Log.e(App.Current.getErrorTag(), ex.getMessage());
            }
        }

        //================================================================================

        //================================================================================
        //==== helper classes
        /**
         * this is a helper class to detect the gesture or swipe. It uses the onFling() to test whether it was a right or left swipe. 
         * It increments the position of and sets the appropriate next image on the banner.
         * */
        protected class MyGestureDetector extends SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.i(TAG, "BANNER: onFling() happened");
                int currentIndex = 0;
                try {
                    currentIndex = FromState.getCurrentAdRotatorPosition();
                    if(isScrollingLeft(e1, e2)) {
                        // left swipe
                        if (currentIndex > 0)
                            currentIndex --;
                        else currentIndex = getBanners().size()-1;
                    }  
                    else {
                        // right swipe
                        if (currentIndex < getBanners().size()-1)
                            currentIndex ++;
                        else currentIndex = 0;
                    }
                } catch (Exception e) {
                    // do nothing
                }
                FromState..setCurrentAdRotatorPosition(currentIndex);
                setBanner(FromState.getCurrentAdRotatorPosition());
                return false;
            }

            @Override
            public boolean onSingleTapUp(MotionEvent ev)
            {
                Log.i(TAG, "BANNER: onSingleTapUp() happened");
                processBannerClick(getBanners().get(FromState.getCurrentAdRotatorPosition()));
                return false;
            }


            /** check if left swipe happened */
            private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
                return e2.getX() > e1.getX(); 
           }
        }
        //================================================================================
    }

In case anybody wanted the solution, here is the code:

public class CustomBanner extends RelativeLayout {
        //================================================================================
        //==== declaration.
        protected List<Banner> _items = new ArrayList<Banner>();
        protected GestureDetector gestureDetector;
            protected View.OnTouchListener gestureListener;
            protected ImageView _banner = null;
        protected String TAG = "CustomBanner";
        //================================================================================

        //================================================================================
        //==== properties
        /**
         * This property pulls the banners from the state manager. If the banners are not there they will be pulled from the database.
         * */
        protected List<Banner> getBanners()
        {
            // get your banners however way you get them
            return this._items;
        }
        //================================================================================

        //================================================================================
        //==== constructors
        public CustomBanner(Context context, AttributeSet attributes)
        {
            super(context, attributes);
            this.Init(context, attributes);
        }
        //================================================================================

        //================================================================================
        //==== protected methods
        protected void Init(Context context, AttributeSet attributes)
        {
            //---- inflate our layout
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layoutInflater.inflate(R.drawable.controls_custombanner, this);

            this._banner = (ImageView)findViewById(R.id.imgBanner);

                // swipe detection on the image
             this.gestureDetector = new GestureDetector(new MyGestureDetector());
            this.gestureListener = new View.OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event) {
                        if (gestureDetector.onTouchEvent(event)) {
                            return true;
                        }
                        return false;
                    }
                };
                // leave this, it needs to be here in order to redirect the swipe correctly to touch gesture direction
               this._banner.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // leave blank, click is redirected to gesture 
                    }
                }); 

             this._banner.setOnTouchListener(gestureListener);

                // set banner image
                this.setBanner(this.getCurrentIndexByWndId());
        }


        protected int getCurrentIndexByWndId() {

            // add your logic here
            // you need to access your state to get the last saved id
        }

        /**
         * this function does nothing but set the banner image by the position from the array of banners. 
         * */
        protected void setBanner(int position)
        {
            _banner.setImageResource(this.getBanners().get(position).ResourceId);
        }

        /**
         * this function tests the validity of the index in the banners array and resets it to 0 if its not valid.
         * */
        protected int checkValidIndex(int currentIndex){
            if (currentIndex > this.getBanners().size()-1)
                currentIndex = 0;
            return currentIndex;
        }

        protected void processBannerClick(Banner clickedBanner){
            try
            {
                // here do your logic for the click
            }
            catch (Exception ex)
            {
                Log.e(App.Current.getErrorTag(), ex.getMessage());
            }
        }

        //================================================================================

        //================================================================================
        //==== helper classes
        /**
         * this is a helper class to detect the gesture or swipe. It uses the onFling() to test whether it was a right or left swipe. 
         * It increments the position of and sets the appropriate next image on the banner.
         * */
        protected class MyGestureDetector extends SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.i(TAG, "BANNER: onFling() happened");
                int currentIndex = 0;
                try {
                    currentIndex = FromState.getCurrentAdRotatorPosition();
                    if(isScrollingLeft(e1, e2)) {
                        // left swipe
                        if (currentIndex > 0)
                            currentIndex --;
                        else currentIndex = getBanners().size()-1;
                    }  
                    else {
                        // right swipe
                        if (currentIndex < getBanners().size()-1)
                            currentIndex ++;
                        else currentIndex = 0;
                    }
                } catch (Exception e) {
                    // do nothing
                }
                FromState..setCurrentAdRotatorPosition(currentIndex);
                setBanner(FromState.getCurrentAdRotatorPosition());
                return false;
            }

            @Override
            public boolean onSingleTapUp(MotionEvent ev)
            {
                Log.i(TAG, "BANNER: onSingleTapUp() happened");
                processBannerClick(getBanners().get(FromState.getCurrentAdRotatorPosition()));
                return false;
            }


            /** check if left swipe happened */
            private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
                return e2.getX() > e1.getX(); 
           }
        }
        //================================================================================
    }
恏ㄋ傷疤忘ㄋ疼 2024-10-24 11:40:00

http://permalink.gmane.org/gmane.comp.handhelds.android .devel/101327 此代码可以帮助您创建旋转的 onFling 视图。只要去掉无用的功能就差不多完成了。
为了记住当前的横幅,您可能必须将横幅类创建为 Singletone,然后加载它的实例和状态以了解当前显示的横幅。

http://permalink.gmane.org/gmane.comp.handhelds.android.devel/101327 This code may help you creating rotating onFling views. Just cut off useless functionality and you are nearly done.
In order to remember the current banner you will probably have to create the banner class as a Singletone and then load it's instance and state to learn which banner is shown currently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文