美文网首页
ViewPager指示器,随页面滑动指示器跟随滑动效果

ViewPager指示器,随页面滑动指示器跟随滑动效果

作者: 闫鹏飞写字的地方 | 来源:发表于2022-09-08 17:12 被阅读0次

    刚做安卓一直百度找轮子,现在可以尝试自己实现了,虽然实现方式很烂,但是效果实现了,还是挺有意思的

    image.png Screenshot_2022_0908_172239.gif

    指示器:

    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.content.res.AppCompatResources;
    
    /**
     * Time:2022/9/8
     * Author:ypf
     * Description:viewpager指示器
     */
    public class ViewPagerIndicator extends ViewGroup {
    
    
        private Context context;
    
        private int parentWidth;
    
        private int parentHeight;
    
        private int position;
    
        private float positionOffset;
    
        public ViewPagerIndicator(Context context) {
            super(context);
            init(context);
        }
    
        public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context);
        }
    
        private void init(Context context) {
            this.context = context;
        }
    
        public void initIndicator(int viewPagerSize) {
            initIndicator(viewPagerSize, R.mipmap.ic_2, R.mipmap.ic_1);
        }
    
        public void initIndicator(int viewPagerSize, int unSelectIndicatorResId, int selectedIndicatorResId) {
            int size = viewPagerSize + 1;
            for (int i = 0; i < size; i++) {
                ImageView imageView = new ImageView(context);
                LayoutParams params = new LayoutParams(0, 0);
                imageView.setLayoutParams(params);
                if (i == viewPagerSize) {
                    imageView.setImageDrawable(AppCompatResources.getDrawable(context, selectedIndicatorResId));
                } else {
                    imageView.setImageDrawable(AppCompatResources.getDrawable(context, unSelectIndicatorResId));
                }
                addView(imageView);
            }
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
    
            int childCount = getChildCount() - 1;
    
            //指示器宽高 1/2高
            int indicatorWidthHeight = parentHeight / 2;
    
            //间隔距离
            int indicatorSpace = (parentWidth - indicatorWidthHeight * childCount) / (childCount + 1);
    
            int childLeft = indicatorSpace;
            int childTop = parentHeight / 4;
            int childRight = childLeft + indicatorWidthHeight;
            int childBottom = parentHeight - childTop;
    
            for (int i = 0; i < childCount; i++) {
                View childAt = getChildAt(i);
                LayoutParams layoutParams = childAt.getLayoutParams();
                layoutParams.width = indicatorWidthHeight;
                layoutParams.height = indicatorWidthHeight;
                childAt.setLayoutParams(layoutParams);
                childAt.layout(childLeft + (indicatorWidthHeight + indicatorSpace) * i, childTop, childRight + (indicatorWidthHeight + indicatorSpace) * i, childBottom);
            }
    
            //绘制指引器指引
            childLeft = (int) (indicatorSpace + (indicatorWidthHeight + indicatorSpace) * positionOffset) + (indicatorWidthHeight + indicatorSpace) * position;
    
            View childAt = getChildAt(childCount);
            LayoutParams layoutParams = childAt.getLayoutParams();
            layoutParams.width = indicatorWidthHeight;
            layoutParams.height = indicatorWidthHeight;
            childAt.setLayoutParams(layoutParams);
            childAt.layout(childLeft, childTop, childLeft + indicatorWidthHeight, childBottom);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            //ViewGroup解析的父亲给我的宽度
            parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            // ViewGroup解析的父亲给我的高度
            parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        }
    
        public void moveIndicator(int position, float positionOffset) {
            this.position = position;
            this.positionOffset = positionOffset;
            requestLayout();
        }
    
    }
    
    

    主页面

    import android.os.Bundle;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager.widget.ViewPager;
    
    public class MainActivity extends AppCompatActivity {
    
        private ViewPager mViewPager;
    
        private ViewPagerIndicator mViewPagerIndicator;
    
        private GuidePageAdapter guidePageAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mViewPager = findViewById(R.id.viewpager);
            mViewPagerIndicator = findViewById(R.id.viewPagerIndicator);
            loadGuideDataToView();
        }
    
        private void loadGuideDataToView() {
    
            guidePageAdapter = new GuidePageAdapter();
            mViewPager.setCurrentItem(0);
            mViewPager.setAdapter(guidePageAdapter);
    
            mViewPagerIndicator.initIndicator(guidePageAdapter.getCount());
    
            mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    mViewPagerIndicator.moveIndicator(position, positionOffset);
                }
    
                @Override
                public void onPageSelected(int position) {
    
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
    
        }
    
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <com.pf.bannerviewpager.ViewPagerIndicator
            android:id="@+id/viewPagerIndicator"
            android:layout_width="300dp"
            android:layout_height="40dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    public class GuidePageAdapter extends PagerAdapter {
    
        private final SparseArray<ImageView> mHolderArray = new SparseArray<>();
    
        private int mSize;
    
        private int[] drawableId = new int[]{R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3, R.mipmap.guide_4};
    
        /**
         *  GuidePageAdapter
         */
        public GuidePageAdapter() {
            mSize = drawableId.length;
        }
    
        @Override
        public int getCount() {
            return mSize;
        }
    
        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }
    
        @Override
        public Object instantiateItem(ViewGroup view, int position) {
    
            ImageView imageView = new ImageView(view.getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(drawableId[position]);
    
    
            view.addView(imageView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mHolderArray.put(position, imageView);
            return imageView;
        }
    
        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView(mHolderArray.get(position));
        }
    
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:ViewPager指示器,随页面滑动指示器跟随滑动效果

          本文链接:https://www.haomeiwen.com/subject/dcuonrtx.html