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)
站点信息
- 文章2305
- 用户1336
- 访客11455538
每日一句
Talent without working hard is nothing.
没有努力,天份不代表什么。
没有努力,天份不代表什么。
MySQL 数据库优化
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
免ROOT实现模拟点击任意位置
Mobaxterm终端神器
CreateProcessW要注意的细节问题
Autonomous NAT Traversal
【教程】win10 彻底卸载edge浏览器
eclipse工程基于Xposed的一个简单Hook
排名前5的开源在线机器学习
Mac OS最简单及(Karabiner)快捷键设置
发一款C++编写的麻将
VMware NAT端口映射外网访问虚拟机linux
独家发布最新可用My-AutoPost——wordpress 采集器
新会员