Android加载动画系列——PlayBallLoadingAnim
来源:互联网 发布:中国省市地区json数据 编辑:程序博客网 时间:2024/06/09 19:10
Android加载动画系列——PlayBallLoadingAnim
PlayBallLoadingAnim是一款小球弹跳动画,很像小时玩过得弹簧床。
让我们先来看看效果图:
1、PlayBallLoadingAnim.java源码如下:
public class PlayBallLoadingAnim extends View { private Paint mPaint, mPaintCircle, mPaintBall; private float mPaintStrokeWidth; private float mHeight = 0f; private float mWidth = 0f; private float quadToStart = 0f; private float mRadius = 0f; private float mRadiusBall = 0f; private float ballY = 0f; Path path = new Path(); public PlayBallLoadingAnim(Context context) { this(context, null); } public PlayBallLoadingAnim(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PlayBallLoadingAnim(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mHeight = getMeasuredHeight(); mWidth = getMeasuredWidth(); quadToStart = mHeight / 2; mRadius = dip2px(3); mPaintStrokeWidth = 2; ballY = mHeight / 2; mRadiusBall = dip2px(4); } public int dip2px(float dpValue) { final float scale = getContext().getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); path = new Path(); path.moveTo(0 + mRadius * 2 + mPaintStrokeWidth, getMeasuredHeight() / 2); path.quadTo(mWidth / 2, quadToStart, mWidth - mRadius * 2 - mPaintStrokeWidth, mHeight / 2); mPaint.setStrokeWidth(2); canvas.drawPath(path, mPaint); mPaintCircle.setStrokeWidth(mPaintStrokeWidth); canvas.drawCircle(mRadius + mPaintStrokeWidth, mHeight / 2, mRadius, mPaintCircle); canvas.drawCircle(mWidth - mRadius - mPaintStrokeWidth, mHeight / 2, mRadius, mPaintCircle); if (ballY - mRadiusBall > mRadiusBall) { canvas.drawCircle(mWidth / 2, ballY - mRadiusBall, mRadiusBall, mPaintBall); } else { canvas.drawCircle(mWidth / 2, mRadiusBall, mRadiusBall, mPaintBall); } } private void initPaint() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.WHITE); mPaintCircle = new Paint(); mPaintCircle.setAntiAlias(true); mPaintCircle.setStyle(Paint.Style.STROKE); mPaintCircle.setColor(Color.WHITE); mPaintBall = new Paint(); mPaintBall.setAntiAlias(true); mPaintBall.setStyle(Paint.Style.FILL); mPaintBall.setColor(Color.WHITE); } ValueAnimator valueAnimator = null; public void stopAnim() { if (valueAnimator != null) { clearAnimation(); quadToStart = mHeight / 2; ballY = mHeight / 2; valueAnimator.setRepeatCount(0); valueAnimator.cancel(); valueAnimator.end(); } } private ValueAnimator startViewAnim(float startF, final float endF, long time) { valueAnimator = ValueAnimator.ofFloat(startF, endF); valueAnimator.setDuration(time); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.setRepeatCount(ValueAnimator.INFINITE); valueAnimator.setRepeatMode(ValueAnimator.REVERSE); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) valueAnimator.getAnimatedValue(); if (value > 0.75) { quadToStart = mHeight / 2 - (1f - (float) valueAnimator.getAnimatedValue()) * mHeight / 3f; } else { quadToStart = mHeight / 2 + (1f - (float) valueAnimator.getAnimatedValue()) * mHeight / 3f; } if (value > 0.35f) { ballY = mHeight / 2 - (mHeight / 2 * value); } else { ballY = mHeight / 2 + (mHeight / 6 * value); } invalidate(); } }); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); } }); if (!valueAnimator.isRunning()) { valueAnimator.start(); } return valueAnimator; } public void startAnim() { stopAnim(); startViewAnim(0f, 1f, 800); }}
2、接下来我们在layout中引用自定义的动画控件,如下所示:
<com.cyril.loadinganim.PlayBallLoadingAnim android:id="@+id/playball" android:layout_width="50dp" android:layout_height="50dp" />
3、然后在Activity中实现动画的播放和停止,使用事例如下:
playBallLoadingAnim = (PlayBallLoadingAnim) findViewById(R.id.playball);playBallLoadingAnim.startAnim();
4、 戳这里,小编带你去源码的下载地址:http://download.csdn.net/detail/zhimingshangyan/9582830
0 0
- Android加载动画系列——PlayBallLoadingAnim
- Android加载动画系列——PlayBallLoadingAnim
- Android加载动画系列——BalloonLoading
- Android加载动画系列——WaterBottleLoading
- Android加载动画系列——BatteryLoading
- Android加载动画系列——CircularLoadingAnim
- Android加载动画系列——CircularCDLoadingAnim
- Android加载动画系列——CircularRingLoadingAnim
- Android加载动画系列—— CircularJumpLoadingAnim
- Android加载动画系列—— CircularSmileLoadingAnim
- Android加载动画系列——CircularZoomLoadingAnim
- Android加载动画系列—— LineWithTextLoadingAnim
- Android加载动画系列——WifiLoadingAnim
- Android加载动画系列——GearsLoadingAnim
- Android加载动画系列——ChromeLogoLoadingAnim
- Android加载动画系列——GearsTwoLoadingAnim
- Android加载动画系列—— GhostLoadingAnim
- Android加载动画系列——FinePoiStarLoadingAnim
- 数据结构实验之栈四:括号匹配
- Android 动画系列之属性(Property)动画详解
- js 实现树 递归
- HTTP LIVE Streams
- [require.js插件] text.js异步加载文本资源
- Android加载动画系列——PlayBallLoadingAnim
- POJ_2082_Terrible Sets
- UVA12013
- psql常用命令
- 错误码及其文字描述
- 第一个理解的python程序--ZIP文件的字典破解
- 如何从List,ArrayList集合中删除对象,或者其他数据
- JAVA设计模式之——构建者设计模式
- String的转换功能: