提供一个Android原生的Progress——SwipeToRefreshLayout下拉刷新时的等待动画
先來(lái)上個(gè)圖看看效果:
這里我為什么要單獨(dú)把這個(gè)拿出來(lái)呢,因?yàn)樽罱砰_(kāi)始接觸Android最新的東西,也就是5.0以上的東西,發(fā)現(xiàn)Android提供的SwipeToRefreshLayout是沒(méi)有上拉加載更多的,在網(wǎng)上找了不少第三方提供加載更多的項(xiàng)目,大部分都還在使用以前ListView時(shí)候使用的那一套加載更多的效果,但是效果都不是很好,所以萌生了要把這部分單獨(dú)抽出來(lái)的想法。
第一張圖就是在下拉刷新以及加載更多的時(shí)候的效果,第二張是加載動(dòng)畫(huà),也是可以放大的,不過(guò)在某些手機(jī)版本上是沒(méi)效果的。
我給這個(gè)Progress命名為:MaterialProgress,這個(gè)文件內(nèi)部完全是從SwipeToRefreshLayout中扒過(guò)來(lái),然后刪除了一部分影響使用的東西,所以?xún)?nèi)部還是很多不必須的代碼的,所以,有需要的可以適當(dāng)?shù)膭h除。
它的主要用處是可以作為自定義加載更多的加載動(dòng)畫(huà),你一定需要這個(gè)。
貼一部分主要代碼:
/*** SwipeToRefreshLayout中下拉刷新時(shí)的等待圓環(huán)* 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);//設(shè)置一個(gè)內(nèi)邊距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>...}在使用的時(shí)候?qū)㈨?xiàng)目中的JAVA文件拷入工程即可,不用單獨(dú)作為一個(gè)項(xiàng)目,每個(gè)環(huán)境可能不一樣,可能會(huì)出現(xiàn)編譯錯(cuò)誤。快來(lái)試試吧。
項(xiàng)目地址:http://git.oschina.net/sahadev/MaterialProgress
總結(jié)
以上是生活随笔為你收集整理的提供一个Android原生的Progress——SwipeToRefreshLayout下拉刷新时的等待动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Java】关于Java中的各种流
- 下一篇: Android官方开发文档Trainin