前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZoomLoadView 自定义view 可以做app的启动首页效果

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

作者头像
夏洛克的猫
发布2018-10-18 14:22:36
3590
发布2018-10-18 14:22:36
举报
文章被收录于专栏:移动开发移动开发

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

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

test1的原始图片

这里写图片描述
这里写图片描述

test2的原始图片

这里写图片描述
这里写图片描述

效果图

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

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

代码语言:javascript
复制
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();
    }


}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年08月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档