package com.roger.catloadinglibrary; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Animation; /** * Created by Administrator on 2016/3/30. */ public class EyelidView extends View { private float progress; private boolean isLoading; private Paint mPaint; private boolean isStop = true; private int duration = 1000; private ValueAnimator valueAnimator; private boolean isFromFull; public EyelidView(Context context) { super(context); init(); } public EyelidView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public EyelidView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.FILL); setBackground(null); setFocusable(false); setEnabled(false); setFocusableInTouchMode(false); valueAnimator = ValueAnimator.ofFloat(0, 1).setDuration(duration); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.setRepeatCount(Animation.INFINITE); valueAnimator.setRepeatMode(ValueAnimator.REVERSE); valueAnimator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { progress = (float) animation.getAnimatedValue(); invalidate(); } }); } public void setColor(int color) { mPaint.setColor(color); } public void startLoading() { if (!isStop) { return; } isLoading = true; isStop = false; valueAnimator.start(); } public void resetAnimator(){ valueAnimator.start(); } public void stopLoading() { isLoading = false; valueAnimator.end(); valueAnimator.cancel(); isStop = true; } public void setDuration(int duration) { this.duration = duration; } @Override protected void onVisibilityChanged(View changedView, int visibility) { super.onVisibilityChanged(changedView, visibility); if (!isLoading) { return; } if (visibility == View.VISIBLE) { valueAnimator.resume(); } else { valueAnimator.pause(); } } public void setFromFull(boolean fromFull) { isFromFull = fromFull; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isStop) { float bottom = 0.0f; if (!isFromFull) { bottom = progress * getHeight(); } else { bottom = (1.0f - progress) * getHeight(); } bottom = bottom >= (getHeight() / 2) ? (getHeight() / 2) : bottom; canvas.drawRect(0, 0, getWidth(), bottom, mPaint); } } private boolean whenStop() { return (isLoading == false && progress <= 0.001f); } }简单看了下工程,利用动画和自定义onDraw实现的这个效果,整体看起来效果不错。值得学习。源码:https://github.com/Rogero0o/CatLoadingView
本站下载:CatLoadingView-master
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2313
- 用户1336
- 访客11719447
每日一句
Small steps still move you forward.
小步也是向前。
小步也是向前。
Linux(Centos7)系统下给已有分区进行扩容
手机屏幕碎了怎么备份操作?
wampserver安装:因为计算机中丢失MSVCR110.dll
命令行svnsync完整迁移SVN仓库数据
Win10定时执行php任务
如何删除(360流氓)鲁大师360base64.dll
Cocos2d-x 3.17.2和Android Studio环境搭建
自动阈值化函数AdaptiveThreshold
阿里云再次搬迁笔记
iOS开发——storyboard多语言适配
OnePlus3 Android8.0的Root教程
Android Studio 提示Session 'app':Error Installing APK
彻底关闭win10自动更新(多套组合拳合击)
新会员