ViewFlipper实现View轮播点击等效果

public class MagicViewFlipperLayout extends RelativeLayout implements View.OnTouchListener,Animation.AnimationListener {

    private int[] imgRes;
    private Context mcontext;
    private ViewFlipper mviewflipper;
    private List listlistener = new ArrayList<>();

    private GestureDetector gestureDetector = null;
    private static final int FLING_MIN_DISTANCE = 80;
    private static final int FLING_MIN_VELOCITY = 150;

    private LinearLayout layout;

    private Animation animin,animaout,animaleftin,animaleftout;

    private boolean autoflipping;

    public MagicViewFlipperLayout(Context context) {
        super(context);
        initView(context);
    }

    public MagicViewFlipperLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MagicViewFlipperLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mcontext = context;
        animin = AnimationUtils.loadAnimation(mcontext, R.anim.img_in);
        animaout = AnimationUtils.loadAnimation(mcontext, R.anim.img_out);

        animaleftin = AnimationUtils.loadAnimation(mcontext, R.anim.img_in_left);
        animaleftout = AnimationUtils.loadAnimation(mcontext, R.anim.img_out_left);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return  gestureDetector.onTouchEvent(event);
    }

    @Override
    public void onAnimationStart(Animation animation) {
        selectedPoint();
    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    public interface listenerFunction {
        void listf();
    }

    /*
   * imgs:要放入的图片
   * autoflipping:是否自动滚动
   * flipinterval:滚动间隔时间
   * listlistener:对应imgs的点击事件
   * spotWidth:spot的宽度
   * spotHeight:spot的高度
   * paddingSize:spot间的间隔*/
    public void startViewFlipper(int[] imgs,boolean autoflipping, int flipinterval, List listlistener,int spotWidth,int spotHeigt,int paddingSize) {
        this.listlistener = listlistener;
        this.imgRes = imgs;
        this.autoflipping = autoflipping;

        mviewflipper = new ViewFlipper(mcontext);

        if (listlistener.size() != imgRes.length)
            throw new UnsupportedOperationException("The number of listlistener is not equal the number of imgs");
        for (int i = 0; i < imgRes.length; i++) {
            ImageView img = new ImageView(mcontext);
            img.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            img.setBackgroundResource(imgRes[i]);
            img.setScaleType(ImageView.ScaleType.CENTER);
            img.setTag(i);
            mviewflipper.addView(img);
        }
        mviewflipper.setInAnimation(animin);
        mviewflipper.setOutAnimation(animaout);
        // 开启自动启动并设置时间间隔
        setViewFlipperToFlipping(autoflipping, true);
        mviewflipper.setFlipInterval(flipinterval);

        gestureDetector = new GestureDetector(mcontext, new GerstureListener());
        animin.setAnimationListener(this);

        addView(mviewflipper);


        layout = new LinearLayout(mcontext);
        RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(spotWidth,spotHeigt);
        RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rlayout.addRule(CENTER_HORIZONTAL);
        rlayout.addRule(ALIGN_PARENT_BOTTOM);
        ll.setMargins(0, 0, paddingSize, 20);
        layout.setLayoutParams(rlayout);
        for(int i = 0 ; i < imgRes.length ; i ++){
            ImageView img = new ImageView(mcontext);
            img.setLayoutParams(ll);
            img.setImageResource(R.drawable.viewpager_spot);
            if(i == 0)img.setSelected(true);
            layout.addView(img);
        }
        addView(layout);

        setLongClickable(true);
        setOnTouchListener(this);
        mviewflipper.getInAnimation().setAnimationListener(this);


    }

    public void setViewFlipperToFlipping(boolean flipping,boolean startOrstop){
        if(mviewflipper == null)return;
        if(flipping){
            if(startOrstop){
                mviewflipper.startFlipping();
            }else{
                mviewflipper.stopFlipping();
            }
        }
    }

    private class GerstureListener implements GestureDetector.OnGestureListener {
        public boolean onDown(MotionEvent e) {
            return false;
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            setViewFlipperToFlipping(autoflipping, false);
            if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                    && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                // 左滑时,按照规定动画切换
                mviewflipper.setInAnimation(animin);
                mviewflipper.setOutAnimation(animaout);
                mviewflipper.showNext();
            } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                    && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                // 右滑时,按照规定动画切换
                mviewflipper.setInAnimation(animaleftin);
                mviewflipper.setOutAnimation(animaleftout);
                mviewflipper.showPrevious();
                selectedPoint();
            }
            setViewFlipperToFlipping(autoflipping,true);
            // 滑动动作完毕后,设置回右进左出的动画
            mviewflipper.setInAnimation(animin);
            mviewflipper.setOutAnimation(animaout);
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            listlistener.get((int)mviewflipper.getCurrentView().getTag()).listf();
            return true;
        }
    }

    private void selectedPoint(){
        int i = (int)mviewflipper.getCurrentView().getTag();
        for(int j = 0 ; j < layout.getChildCount();j++){
            Log.i("ls", "i = " + i + " j = " + j);
            if(i == j){
                layout.getChildAt(j).setSelected(true);
            }else{
                layout.getChildAt(j).setSelected(false);
            }
        }
    }

动画example:




    
实现:
List listlistener = new ArrayList<>();
        for(int i = 0; i < imgRes.length ; i ++){
            final int finalI = i;
            MagicViewFlipperLayout.listenerFunction mlistener = new MagicViewFlipperLayout.listenerFunction() {
                @Override
                public void listf() {
                    Toast.makeText(getActivity(), "" + imgRes[finalI], Toast.LENGTH_SHORT).show();
                }
            };
            listlistener.add(mlistener);
        }
        mViewPager.startViewFlipper(imgRes, true, 2000, listlistener, 28, 28,20);

你可能感兴趣的:(android)