提供一个Android原生的Progress——SwipeToRefreshLayout下拉刷新时的等待动画

先来上个图看看效果:


这里我为什么要单独把这个拿出来呢,因为最近才开始接触Android最新的东西,也就是5.0以上的东西,发现Android提供的SwipeToRefreshLayout是没有上拉加载更多的,在网上找了不少第三方提供加载更多的项目,大部分都还在使用以前ListView时候使用的那一套加载更多的效果,但是效果都不是很好,所以萌生了要把这部分单独抽出来的想法。


第一张图就是在下拉刷新以及加载更多的时候的效果,第二张是加载动画,也是可以放大的,不过在某些手机版本上是没效果的。

我给这个Progress命名为:MaterialProgress,这个文件内部完全是从SwipeToRefreshLayout中扒过来,然后删除了一部分影响使用的东西,所以内部还是很多不必须的代码的,所以,有需要的可以适当的删除。


它的主要用处是可以作为自定义加载更多的加载动画,你一定需要这个。



贴一部分主要代码:

/**
 * SwipeToRefreshLayout中下拉刷新时的等待圆环
 * Created by Sahadev on 2015/11/13.
 */
public class MaterialProgress extends FrameLayout {
    <span style="white-space:pre">	</span>...

    public MaterialProgress(Context context) {
        this(context, null, 0);
    }

    public MaterialProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MaterialProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mRefreshing = false;
        this.mTotalDragDistance = -1.0F;
        this.mParentScrollConsumed = new int[2];
        this.mOriginalOffsetCalculated = false;
        this.mActivePointerId = -1;
        this.mCircleViewIndex = -1;
        this.mRefreshListener = new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                if (MaterialProgress.this.mRefreshing) {
                    MaterialProgress.this.mProgress.setAlpha(255);
                    MaterialProgress.this.mProgress.start();
                } else {
                    MaterialProgress.this.mProgress.stop();
                    MaterialProgress.this.mCircleView.setVisibility(View.VISIBLE);
                    MaterialProgress.this.setColorViewAlpha(255);
                    if (MaterialProgress.this.mScale) {
                        MaterialProgress.this.setAnimationProgress(0.0F);
                    } else {
                        MaterialProgress.this.setTargetOffsetTopAndBottom(MaterialProgress.this.mOriginalOffsetTop - MaterialProgress.this.mCurrentTargetOffsetTop, true);
                    }
                }

                MaterialProgress.this.mCurrentTargetOffsetTop = MaterialProgress.this.mCircleView.getTop();
            }
        };
        this.mAnimateToCorrectPosition = new Animation() {
            public void applyTransformation(float interpolatedTime, Transformation t) {
                boolean targetTop = false;
                boolean endTarget = false;
                int endTarget1;
                if (!MaterialProgress.this.mUsingCustomStart) {
                    endTarget1 = (int) (MaterialProgress.this.mSpinnerFinalOffset - (float) Math.abs(MaterialProgress.this.mOriginalOffsetTop));
                } else {
                    endTarget1 = (int) MaterialProgress.this.mSpinnerFinalOffset;
                }

                int targetTop1 = MaterialProgress.this.mFrom + (int) ((float) (endTarget1 - MaterialProgress.this.mFrom) * interpolatedTime);
                int offset = targetTop1 - MaterialProgress.this.mCircleView.getTop();
                MaterialProgress.this.setTargetOffsetTopAndBottom(offset, false);
                MaterialProgress.this.mProgress.setArrowScale(1.0F - interpolatedTime);
            }
        };
        this.mPeek = new Animation() {
            public void applyTransformation(float interpolatedTime, Transformation t) {
                boolean targetTop = false;
                boolean endTarget = false;
                int endTarget1;
                if (!MaterialProgress.this.mUsingCustomStart) {
                    endTarget1 = (int) (MaterialProgress.this.mSpinnerFinalOffset - (float) Math.abs(MaterialProgress.this.mOriginalOffsetTop));
                } else {
                    endTarget1 = (int) MaterialProgress.this.mSpinnerFinalOffset;
                }

                int targetTop1 = MaterialProgress.this.mFrom + (int) ((float) (endTarget1 - MaterialProgress.this.mFrom) * interpolatedTime);
                int offset = targetTop1 - MaterialProgress.this.mCircleView.getTop();
                MaterialProgress.this.setTargetOffsetTopAndBottom(offset, false);
                MaterialProgress.this.mProgress.setArrowScale(1.0F - interpolatedTime);
            }
        };
        this.mAnimateToStartPosition = new Animation() {
            public void applyTransformation(float interpolatedTime, Transformation t) {
                MaterialProgress.this.moveToStart(interpolatedTime);
            }
        };
        this.mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.mMediumAnimationDuration = 500;
        this.setWillNotDraw(false);
        this.mDecelerateInterpolator = new DecelerateInterpolator(2.0F);
        TypedArray a = context.obtainStyledAttributes(attrs, LAYOUT_ATTRS);
        this.setEnabled(a.getBoolean(0, true));
        a.recycle();
        DisplayMetrics metrics = this.getResources().getDisplayMetrics();
        this.mCircleWidth = (int) (40.0F * metrics.density);
        this.mCircleHeight = (int) (40.0F * metrics.density);
        this.createProgressView();
        ViewCompat.setChildrenDrawingOrderEnabled(this, true);
        this.mSpinnerFinalOffset = 64.0F * metrics.density;
        this.mTotalDragDistance = this.mSpinnerFinalOffset;
        this.mNestedScrollingParentHelper = new NestedScrollingParentHelper(this);
        this.mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
        this.setNestedScrollingEnabled(true);


        setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        setRefreshing(true);
        this.finishSpinner(mTotalDragDistance + 10);
    }

    private void setColorViewAlpha(int targetAlpha) {
        this.mCircleView.getBackground().setAlpha(targetAlpha);
        this.mProgress.setAlpha(targetAlpha);
    }


    public void setSize(int size) {
        if (size == 0 || size == 1) {
            DisplayMetrics metrics = this.getResources().getDisplayMetrics();
            if (size == 0) {
                this.mCircleHeight = this.mCircleWidth = (int) (56.0F * metrics.density);
            } else {
                this.mCircleHeight = this.mCircleWidth = (int) (40.0F * metrics.density);
            }

            this.mCircleView.setImageDrawable((Drawable) null);
            this.mProgress.updateSizes(size);
            this.mCircleView.setImageDrawable(this.mProgress);
        }
    }

    private void createProgressView() {
        this.mCircleView = new CircleImageView(this.getContext(), -328966, 20.0F);
        this.mProgress = new MaterialProgressDrawable(this.getContext(), this);
        this.mProgress.setBackgroundColor(-328966);
        this.mCircleView.setImageDrawable(this.mProgress);
        this.mCircleView.setVisibility(View.VISIBLE);
        this.mCircleView.setPadding(5, 5, 5, 5);//设置一个内边距
        this.addView(this.mCircleView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

<span style="white-space:pre">	</span>...

    public void setRefreshing(boolean refreshing) {
        if (refreshing && this.mRefreshing != refreshing) {
            this.mRefreshing = refreshing;
            boolean endTarget = false;
            int endTarget1;
            if (!this.mUsingCustomStart) {
                endTarget1 = (int) (this.mSpinnerFinalOffset + (float) this.mOriginalOffsetTop);
            } else {
                endTarget1 = (int) this.mSpinnerFinalOffset;
            }

            this.setTargetOffsetTopAndBottom(endTarget1 - this.mCurrentTargetOffsetTop, true);
            this.mNotify = false;
            this.startScaleUpAnimation(this.mRefreshListener);
        } else {
            this.setRefreshing(refreshing, false);
        }

    }

<span style="white-space:pre">	</span>...

}

在使用的时候将项目中的JAVA文件拷入工程即可,不用单独作为一个项目,每个环境可能不一样,可能会出现编译错误。快来试试吧。

项目地址:http://git.oschina.net/sahadev/MaterialProgress

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值