美文网首页Androidviewapp开发
ZoomLoadView自定义view可以做app的启动首页效果

ZoomLoadView自定义view可以做app的启动首页效果

作者: 夏洛克的喵 | 来源:发表于2016-08-16 15:34 被阅读571次

    虽然这个控件类似的功能github上也有,但还是自己实现了下.
    这个控件可以保持图片的比例去填充view,如果按图片比例计算出的大小超出view的大小,会展示图片的中心对称的中心区域.

    github地址:
    https://github.com/X-FAN/ZoomLoadView 欢迎star

    test1的原始图片 test2的原始图片

    效果图

    test1 test2

    源码比较简单,主要是要算出图片的坐标的正确位置,让图片显示中间部分

    public class ZoomLoadView extends View {
        private int mHeight;
        private int mWidth;
        private int mViewHeight;//可见的高度
        private int mViewWidth;//可见的宽度
        private float mScale;
        private EndListener mEndListener;
    
        private Drawable mZoomDrawable;
        private ValueAnimator mAnimator;
    
    
        public ZoomLoadView(Context context) {
            this(context, null);
        }
    
        public ZoomLoadView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public ZoomLoadView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs) {
            if (attrs == null) {
                throw new NullPointerException("zoomDrawable  attr is needed");
            }
            final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZoomLoadView);
            mZoomDrawable = a.getDrawable(R.styleable.ZoomLoadView_zoomDrawable);
            if (mZoomDrawable == null) {
                throw new NullPointerException("zoom drawable is null");
            }
            int duration = a.getInt(R.styleable.ZoomLoadView_animatorDuration, 3000);
            float scale = a.getFloat(R.styleable.ZoomLoadView_zoomScale, 0.3f);
            a.recycle();
            mHeight = mZoomDrawable.getIntrinsicHeight();
            mWidth = mZoomDrawable.getIntrinsicWidth();
            initAnimator(duration, scale);
        }
    
        private void initAnimator(int duration, float scale) {
            mAnimator = ValueAnimator.ofFloat(1.0f, 1.0f + scale);
            mAnimator.setDuration(duration);
            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mScale = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
            mAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mEndListener.onEndListener();
                }
            });
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            int width;
            int height;
            int scaleHeight;//根据width按图片原始宽高比例所需要的高度
    
    
            if (widthMode == MeasureSpec.EXACTLY) {
                width = widthSize;
            } else if (widthMode == MeasureSpec.AT_MOST) {
                width = Math.min(mWidth, widthSize);
            } else {
                width = mWidth;
            }
    
            if (heightMode == MeasureSpec.EXACTLY) {
                height = heightSize;
            } else if (heightMode == MeasureSpec.AT_MOST) {
                height = Math.min(mHeight, heightSize);
            } else {
                height = mHeight;
            }
            mViewWidth = width;
            mViewHeight = height;
            scaleHeight = mHeight * width / mWidth;
            float scale = (float) height / (float) scaleHeight;
            if (scale >= 1) {
                mWidth = (int) (width * scale);
                mHeight = (int) (scaleHeight * scale);
            } else {
                mWidth = width;
                mHeight = scaleHeight;
            }
            //将绘制的drawable的中心位置设置在view的中心位置,即展示drawable的对称中心区域
            mZoomDrawable.setBounds((mViewWidth - mWidth) / 2, (mViewHeight - mHeight) / 2, (mViewWidth + mWidth) / 2, (mViewHeight + mHeight) / 2);
            setMeasuredDimension(width, height);
    
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (mEndListener != null) {
                super.onDraw(canvas);
                canvas.save();
                canvas.scale(mScale, mScale, mViewWidth / 2, mViewHeight / 2);
                mZoomDrawable.draw(canvas);
                canvas.restore();
            }
        }
    
    
        public void setEndListener(EndListener endListener) {
            mEndListener = endListener;
            mAnimator.start();
        }
    
        public interface EndListener {
            void onEndListener();
        }
    
    
    }
    

    相关文章

      网友评论

      • C_Sev:我收了,自己打一遍,记一下!!
        C_Sev:@活着就是为了体验死亡 mViewWidth和mWidth代表啥,到后面又多了一个scaleheight
        C_Sev:下面这段代码,我已经晕厥了,我已经搞不清楚,哪个是哪个了。。。这个scale和前面动画的那个scale有啥区别,真心没分开。
        mViewWidth = width;
        mViewHeight = height;
        scaleHeight = mHeight * width / mWidth;
        float scale = (float) height / (float) scaleHeight;
        if (scale >= 1) {
        mWidth = (int) (width * scale);
        mHeight = (int) (scaleHeight * scale);
        } else {
        mWidth = width;
        mHeight = scaleHeight;
        }
        //将绘制的drawable的中心位置设置在view的中心位置,即展示drawable的对称中心区域
        mZoomDrawable.setBounds((mViewWidth - mWidth) / 2, (mViewHeight - mHeight) / 2, (mViewWidth + mWidth) / 2, (mViewHeight + mHeight) / 2);
        夏洛克的喵:@活着就是为了体验死亡 有帮助就好
      • 828b5c62f91c:棒棒的

      本文标题:ZoomLoadView自定义view可以做app的启动首页效果

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