zoukankan      html  css  js  c++  java
  • Gallery平滑移动

    看了些网上的方法弄了下平滑移动的效果,虽说最后是实现了,实现后发现也不是我想要的效果,对于我幸苦写过的代码先存放在这上面了

    package com.layout;

    import android.content.Context;
    import android.graphics.Camera;
    import android.graphics.Matrix;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.VelocityTracker;
    import android.view.View;
    import android.view.ViewConfiguration;
    import android.view.animation.Transformation;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Scroller;

    public class GalleryFlow extends Gallery{

        private Camera mCamera = new Camera();
        private int mMaxRotationAngle = 60;
        private int mMaxZoom = -300;
        private int mCoveflowCenter;
        
        //test
        private Scroller mScroller;
        private int mTouchSlop;
        private int mMinimumVelocitx;
        private int mMaximumVelocitx;
        private VelocityTracker mVelocityTracker;
        private float mLastMotionX;
        private boolean mIsInEdge = false;
        private Context context;
        
        public GalleryFlow(Context context) {
            super(context);
            this.context = context;
            this.setStaticTransformationsEnabled(true);
        }
        public GalleryFlow(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.setStaticTransformationsEnabled(true);
            this.context = context;
            init(context);
        }

        public void init(Context context) {
            mScroller = new Scroller(context);
            setFocusable(true);
            setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
            setWillNotDraw(false);
            final ViewConfiguration configuration = ViewConfiguration.get(context);
            mTouchSlop = configuration.getScaledTouchSlop();
            mMinimumVelocitx = configuration.getScaledMinimumFlingVelocity();
            mMaximumVelocitx = configuration.getScaledMaximumFlingVelocity();
        }


        public void fling(int velocityX) {
                if (getChildCount() > 0) {
                        mScroller.fling(getScrollX(), getScrollY(), velocityX, 0, 0, 20, 0,
                                        0);
                        final boolean movingDown = velocityX > 0;
                        awakenScrollBars(mScroller.getDuration());
                        invalidate();
                }
        }


        private void obtainVelocityTracker(MotionEvent event) {
                if (mVelocityTracker == null) {
                        mVelocityTracker = VelocityTracker.obtain();
                }
                mVelocityTracker.addMovement(event);
        }

        private void releaseVelocityTracker() {
                if (mVelocityTracker != null) {
                        mVelocityTracker.recycle();
                        mVelocityTracker = null;
                }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    && event.getEdgeFlags() != 0) {
                return false;
            }

            obtainVelocityTracker(event);

            final int action = event.getAction();
            final float x = event.getX();
            final float y = event.getY();

            switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                mLastMotionX = x;
                break;

            case MotionEvent.ACTION_MOVE:
                final int deltaX = (int) (mLastMotionX - x);
                mLastMotionX = x;
                if (deltaX < 0) {
                    if (getScrollX() > 0) {
    //                    scrollBy(0,deltaX);
                        scrollBy(deltaX,0);
                    }
            }else if(deltaX > 0) {
                    int childTotalWidth = 0;
                    int width = 0;
                    if(getChildAt(0) != null){
                       width = getChildAt(0).getWidth();
                    }
                    for (int i = 0; i < getChildCount(); i++) {
                        childTotalWidth += this.getChildAt(i).getWidth();
                    }
                    mIsInEdge = getScrollX() <= childTotalWidth - 20;//width
                    if (mIsInEdge) {
    //                    scrollBy(0, deltaX);
                        scrollBy(deltaX, 0);
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocitx);
                int initialVelocity = (int) velocityTracker.getYVelocity();

                if ((Math.abs(initialVelocity) > mMinimumVelocitx)
                        && getChildCount() > 0) {
                    fling(-initialVelocity);
                }

                releaseVelocityTracker();
                break;
            }

            return true;

        }


        public void computeScroll() {
                if (mScroller.computeScrollOffset()) {
                        int scrollX = getScrollX();
                        int scrollY = getScrollY();
                        int oldX = scrollX;
                        int oldY = scrollY;
                        int x = mScroller.getCurrX();
                        int y = mScroller.getCurrY();
                        scrollX = x ;
                        scrollX = scrollX + 10;
                        scrollY = y;
    //                    scrollY = scrollY + 10;
                        scrollTo(scrollX, scrollY);
                        postInvalidate();
                }
        }
        @Override
        public int getChildCount() {
            // TODO Auto-generated method stub
            return super.getChildCount();
        }
        

        public int getMaxRotationAngle() {
            return mMaxRotationAngle;
        }
        public void setMaxRotationAngle(int maxRotationAngle) {
            mMaxRotationAngle = maxRotationAngle;
        }
        public int getMaxZoom() {
            return mMaxZoom;
        }
        public void setMaxZoom(int maxZoom) {
            mMaxZoom = maxZoom;
        }
        private int getCenterOfCoverflow() {
            return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
                            + getPaddingLeft();
        }
        private static int getCenterOfView(View view) {
            System.out.println("view left :"+view.getLeft());
            System.out.println("view width :"+view.getWidth());
            return view.getLeft() + view.getWidth() / 2;
            
        }
       
       
        @Override
        protected boolean getChildStaticTransformation(View child, Transformation t) {
            /*/
             final int childCenter = getCenterOfView(child);
             final int childWidth = child.getWidth();
             int rotationAngle = 0;

             t.clear();
             t.setTransformationType(Transformation.TYPE_MATRIX);

             if (childCenter == mCoveflowCenter) {
                     transformImageBitmap((LinearLayout) child, t, 0);
             } else {
                     rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
                     if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                             rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
                                             : mMaxRotationAngle;
                     }
                     transformImageBitmap((LinearLayout) child, t, rotationAngle);
             }
             //*/
            return true;
        }
        
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            mCoveflowCenter = getCenterOfCoverflow();
            super.onSizeChanged(w, h, oldw, oldh);
        }
        
        private void transformImageBitmap(LinearLayout child, Transformation t,
                        int rotationAngle) {
            mCamera.save();
            final Matrix imageMatrix = t.getMatrix();
            final int imageHeight = child.getLayoutParams().height;
            final int imageWidth = child.getLayoutParams().width;
           
            final int rotation = Math.abs(rotationAngle);
           
            mCamera.translate(0.0f, 0.0f, 100.0f);
            if (rotation < mMaxRotationAngle) {
                float zoomAmount = (float) (mMaxZoom + (rotation * 1));//1.5
                mCamera.translate(0.0f, 0.0f, 0);
            }
            mCamera.rotateY(rotationAngle);
            mCamera.getMatrix(imageMatrix);
            imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
            imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
            mCamera.restore();
            
            
        }

    }


  • 相关阅读:
    LeetCode 121. Best Time to Buy and Sell Stock
    LeetCode 221. Maximal Square
    LeetCode 152. Maximum Product Subarray
    LeetCode 53. Maximum Subarray
    LeetCode 91. Decode Ways
    LeetCode 64. Minimum Path Sum
    LeetCode 264. Ugly Number II
    LeetCode 263. Ugly Number
    LeetCode 50. Pow(x, n)
    LeetCode 279. Perfect Squares
  • 原文地址:https://www.cnblogs.com/riskyer/p/3253536.html
Copyright © 2011-2022 走看看