1,在网上看了下好看的加载框,看了一下,挺好看的,再看了下源码,就是纯paint画出来的,再加上属性动画就搞定了

再来看一下我们的源码

LvGhost.java

package com.qianmo.retrofitdemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator; /**
* Created by lumingmin on 16/6/29.
*/ public class LVGhost extends View { float mWidth = 0f;
float mHight = 0f;
Paint mPaint, mPaintHand, mPaintShadow, mPaintArms;
RectF rectFGhost = new RectF();
RectF rectFGhostShadow = new RectF();
float mPadding = 0f;
int mskirtH = 0;
Path path = new Path(); public LVGhost(Context context) {
this(context, null);
} public LVGhost(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public LVGhost(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHight = getMeasuredHeight();
mPadding = 10;
mskirtH = (int) (mWidth / 40);
} private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.WHITE);
mPaintHand = new Paint();
mPaintHand.setAntiAlias(true);
mPaintHand.setStyle(Paint.Style.FILL);
mPaintHand.setColor(Color.argb(220, 0, 0, 0)); mPaintShadow = new Paint();
mPaintShadow.setAntiAlias(true);
mPaintShadow.setStyle(Paint.Style.FILL);
mPaintShadow.setColor(Color.argb(60, 0, 0, 0)); mPaintArms = new Paint();
mPaintArms.setAntiAlias(true);
mPaintArms.setStrokeWidth(8);
mPaintArms.setStyle(Paint.Style.FILL);
mPaintArms.setColor(Color.argb(150, 0, 0, 0)); startAnim();
} private void drawShadow(Canvas canvas) {
canvas.drawArc(rectFGhostShadow, 0, 360, false, mPaintShadow); } private void drawHead(Canvas canvas) {
canvas.drawCircle(rectFGhost.left + rectFGhost.width() / 2
, rectFGhost.width() / 2 + rectFGhost.top
, rectFGhost.width() / 2 - 15
, mPaint
);
} private void drawHand(Canvas canvas) { canvas.drawCircle(rectFGhost.left + rectFGhost.width() / 2 - mskirtH * 3 / 2 + mskirtH * onAnimationRepeatFlag , rectFGhost.width() / 2 + mskirtH + rectFGhost.top,
mskirtH * 0.9f, mPaintHand
);
canvas.drawCircle(rectFGhost.left + rectFGhost.width() / 2 + mskirtH * 3 / 2 + mskirtH * onAnimationRepeatFlag
, rectFGhost.width() / 2 + mskirtH + rectFGhost.top,
mskirtH * 0.9f, mPaintHand
); } float wspace = 10f;
float hspace = 10f; private void drawBody(Canvas canvas) {
path.reset(); float x = (float) ((rectFGhost.width() / 2 - 15) * Math.cos(5 * Math.PI / 180f));
float y = (float) ((rectFGhost.width() / 2 - 15) * Math.sin(5 * Math.PI / 180f)); float x2 = (float) ((rectFGhost.width() / 2 - 15) * Math.cos(175 * Math.PI / 180f));
float y2 = (float) ((rectFGhost.width() / 2 - 15) * Math.sin(175 * Math.PI / 180f)); path.moveTo(rectFGhost.left + rectFGhost.width() / 2 - x, rectFGhost.width() / 2 - y + rectFGhost.top);
path.lineTo(rectFGhost.left + rectFGhost.width() / 2 - x2, rectFGhost.width() / 2 - y2 + rectFGhost.top);
path.quadTo(rectFGhost.right + wspace / 2, rectFGhost.bottom
, rectFGhost.right - wspace, rectFGhost.bottom - hspace); float a = mskirtH;//(mskirtH/2); float m = (rectFGhost.width() - 2 * wspace) / 7f; for (int i = 0; i < 7; i++) {
if (i % 2 == 0) {
path.quadTo(rectFGhost.right - wspace - m * i - (m / 2), rectFGhost.bottom - hspace - a
, rectFGhost.right - wspace - (m * (i + 1)), rectFGhost.bottom - hspace);
} else {
path.quadTo(rectFGhost.right - wspace - m * i - (m / 2), rectFGhost.bottom - hspace + a
, rectFGhost.right - wspace - (m * (i + 1)), rectFGhost.bottom - hspace); }
} path.quadTo(rectFGhost.left - 5, rectFGhost.bottom
, rectFGhost.left + rectFGhost.width() / 2 - x, rectFGhost.width() / 2 - y + rectFGhost.top); path.close();
canvas.drawPath(path, mPaint); }
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save(); float distance = (mWidth - 2 * mPadding) / 3 * 2 * mAnimatedValue; rectFGhost.left = mPadding + distance;
rectFGhost.right = (mWidth - 2 * mPadding) / 3 + distance;
float moveY = 0f;
float moveYMax = mHight / 4f / 2f;
float shadowHighMax = 5f;
float shadowHigh = 0f; if (mAnimatedValue <= 0.25) {
moveY = (float) (moveYMax / 0.25 * mAnimatedValue);
rectFGhost.top = moveY; rectFGhost.bottom = mHight / 4 * 3 + moveY; shadowHigh = shadowHighMax / 0.25f * mAnimatedValue; } else if (mAnimatedValue > 0.25 && mAnimatedValue <= 0.5f) { moveY = (float) (moveYMax / 0.25 * (mAnimatedValue - 0.25f));
rectFGhost.top = moveYMax - moveY;
rectFGhost.bottom = mHight / 4 * 3 + moveYMax - moveY; shadowHigh = shadowHighMax - shadowHighMax / 0.25f * (mAnimatedValue - 0.25f); } else if (mAnimatedValue > 0.5 && mAnimatedValue <= 0.75f) {
moveY = (float) (moveYMax / 0.25 * (mAnimatedValue - 0.5f));
rectFGhost.top = moveY;
rectFGhost.bottom = mHight / 4 * 3 + moveY;
shadowHigh = shadowHighMax / 0.25f * (mAnimatedValue - 0.5f); } else if (mAnimatedValue > 0.75 && mAnimatedValue <= 1f) {
moveY = (float) (moveYMax / 0.25 * (mAnimatedValue - 0.75f));
rectFGhost.top = moveYMax - moveY;
rectFGhost.bottom = mHight / 4 * 3 + moveYMax - moveY;
shadowHigh = shadowHighMax - shadowHighMax / 0.25f * (mAnimatedValue - 0.75f); } rectFGhostShadow.top = mHight - 25 + shadowHigh;
rectFGhostShadow.bottom = mHight - 5 - shadowHigh;
rectFGhostShadow.left = rectFGhost.left + 5 + shadowHigh * 3;
rectFGhostShadow.right = rectFGhost.right - 5 - shadowHigh * 3;
drawShadow(canvas);
drawHead(canvas);
drawBody(canvas);
drawHand(canvas);
canvas.restore(); } public void startAnim() {
stopAnim();
startViewAnim(0f, 1f, 2500);
} private ValueAnimator valueAnimator;
private float mAnimatedValue = 0.f; public void stopAnim() {
if (valueAnimator != null) {
clearAnimation();
valueAnimator.setRepeatCount(0);
valueAnimator.cancel();
valueAnimator.end();
mAnimatedValue = 0f;
wspace = 10;
onAnimationRepeatFlag = 1;
postInvalidate();
}
} int onAnimationRepeatFlag = 1; 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 valueAnimator) { mAnimatedValue = (float) valueAnimator.getAnimatedValue(); invalidate();
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() { @Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
} @Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation); } @Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
onAnimationRepeatFlag = onAnimationRepeatFlag * -1; if (onAnimationRepeatFlag == -1) {
wspace = 22;
} else {
wspace = -2;
} } });
if (!valueAnimator.isRunning()) {
wspace = -2;
valueAnimator.start(); } return valueAnimator;
} }

在我们的布局文件中使用一下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
tools:context="com.qianmo.retrofitdemo.MainActivity"> <TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="还没有数据"
android:visibility="gone"
/> <Button
android:id="@+id/btn_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:visibility="gone"
android:text="请求网络数据"/> <com.qianmo.retrofitdemo.LVGhost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading"
style="@style/loading_style"/>
</RelativeLayout>

这样就可以简单的使用了

  

Android--自定义加载框的更多相关文章

  1. LoaderDialog自定义加载框的实现

    package com.loaderman.loadingdialogdemo; import android.app.Dialog; import android.content.Context; ...

  2. android笔记--加载框

    package com.fuda.ui; import android.app.Activity; import android.os.Bundle; import android.os.Handle ...

  3. Android:webView加载h5网页视频,播放不了,以及横屏全屏的问题和实现自定义加载进度条的效果

    1.webView加载h5网页视频,播放不了,android3.0之后要在menifest添加硬件加速的属性 android:hardwareAccelerated="true". ...

  4. Android图片加载框架最全解析(六),探究Glide的自定义模块功能

    不知不觉中,我们的Glide系列教程已经到了第六篇了,距离第一篇Glide的基本用法发布已经过去了半年的时间.在这半年中,我们通过用法讲解和源码分析配合学习的方式,将Glide的方方面面都研究了个遍, ...

  5. Android 自定义View修炼-自定义加载进度动画XCLoadingImageView

    一.概述 本自定义View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进度颜色. ...

  6. ios新手开发——toast提示和旋转图片加载框

    不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...

  7. 使用Dialog实现全局Loading加载框

    Dialog实现全局Loading加载框 很多人在实现Loading加载框的时候,都是在当前的页面隐藏一个Loading布局,需要加载的时候,显示出来,加载完再隐藏 使用Dialog实现Loading ...

  8. mui---取消掉默认加载框

    我们在进行打开页面新页面的时候,在APP中会在中间有一个加载框,考虑到用户体验,要取消掉,具体方法是,对openWindow进行配置: 具体参考:http://dev.dcloud.net.cn/mu ...

  9. Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能

    我们的Glide系列文章终于要进入收尾篇了.从我开始写这个系列的第一篇文章时,我就知道这会是一个很长的系列,只是没有想到竟然会写这么久. 在前面的六篇文章中,我们对Glide的方方面面都进行了学习,包 ...

随机推荐

  1. 纯CSS完成tab实现5种不同切换对应内容效果

    很常用的一款特效纯CSS完成tab实现5种不同切换对应内容效果 实例预览 下载地址 实例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  2. MongoDB各种查询操作详解

    这篇文章主要介绍了MongoDB各种查询操作详解,包括比较查询.关联查询.数组查询等,需要的朋友可以参考下   一.find操作 MongoDB中使用find来进行查询,通过指定find的第一个参数可 ...

  3. redis 3.0的集群部署

    转载请注明出处:http://hot66hot.iteye.com/admin/blogs/2050676 最近研究redis-cluster,正好搭建了一个环境,遇到了很多坑,系统的总结下,等到re ...

  4. 李洪强经典面试题145-Runloop

    李洪强经典面试题145-Runloop   Runloop 什么是 Runloop? 从字面上讲就是运行循环. 它内部就是do-while循环,在这个循环内部不断地处理各种任务. 一个线程对应一个Ru ...

  5. Daily Scrum02 12.09

    今天星期一,各们课程要结课了,同时也是众多大作业要提交的时间, 但是我们仍然要继续坚持! 引用拿破仑将军的一句话, 最困难之日便是离成功不远之时! Member 任务进度 下一步工作 吴文会 寻找美术 ...

  6. javascript学习之运动框架

    模仿新浪博客首页的,最新评论: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"&g ...

  7. 2016huasacm暑假集训训练三 G - 还是畅通工程

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...

  8. 二进制mysql5.7.16下载地址

    下载地址:http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz 2. 解压 tar  xx ...

  9. Struts2_ValueStack,OGNL详解(转)

    原文地址:http://blog.csdn.net/wyply115/article/details/8257140 一.OGNL表达式 1.ognl是struts2中使用的一种表达式语言,可用于js ...

  10. JQUERY attr prop 的区别 一个已经被淘汰

    在做jquery 全选 全不选的项目中, 1..prop( propertyName ) 获取匹配集合中第一个元素的Property的值 2. .prop( propertyName, value ) ...