热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android实现一个带粘连效果的LoadingBar

Loading效果相信大家应该都实现过,最近发现了一个不错的效果,决定分享给大家,所以下面这篇文章主要给大家介绍了关于利用Android实现一个带粘连效果的LoadingBar的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

我们平时在开发的时候,发起网络请求前,会需要显示一个Loading,一般的做法都是在xml布局上添加好Loading,然后在Activity中,setVisibility来控制Loading的显示和隐藏,这样使用起来就很不方便,因为每一个xml都得引入一个Loading布局。

而LoadingBar就更好的解决了这个问题

最近设计师在外国的一个网站上挑了一个Loading的效果图,尝试实现之后,虽然和原图有点不太一样,但是效果还是不错的。难点就是粘连效果的实现,贝塞尔曲线的点点们简直要把我折磨死了。

先上效果图:


实例代码

然后是源码,就是一个简单VIew,可以直接放在xml中使用。

package top.greendami.greendami;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
 * Created by GreendaMi on 2017/3/17.
 */
public class PPView extends View {
 String TAG = "PPView";
 //动画开关
 boolean isLoading = true;
 Context mContext;
 private int mWidth = 100;
 private int mheight = 100;
 public int mColor;
 public Paint mPaint = new Paint();
 float time = 0;
 //小球与中间打球的最远距离
 float distance = 100;

 public PPView(Context context) {
  super(context);
  mCOntext= context;
 }
 public PPView(Context context, @Nullable AttributeSet attrs) {
  super(context, attrs);
  mCOntext= context;
  mColor = context.getResources().getColor(R.color.colorPrimary);
  init();
 }
 private void init() {
  mPaint.setAntiAlias(true);
  mPaint.setColor(mColor);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
  int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
  //宽度至少是高度的4倍
  if (widthSpecSize <4 * heightSpecSize) {
   widthSpecSize = 4 * heightSpecSize;
  }
  mWidth = widthSpecSize;
  mheight = heightSpecSize;
  distance = 1.2f * mheight;
  setMeasuredDimension(widthSpecSize, heightSpecSize);
 }
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  if (isLoading) {
   //大圆半径
   float bigR = mheight * 0.32f + mheight * 0.03f * Math.abs((float) Math.sin(Math.toRadians(time)));
   float smallR = mheight * 0.22f + mheight * 0.03f * Math.abs((float) Math.cos(Math.toRadians(time)));
   float bigx = (getWidth()) / 2;
   //画中间大圆
   canvas.drawCircle(bigx, mheight / 2, bigR, mPaint);
   float smalx = getSmallCenterX();
   //画小圆
   canvas.drawCircle(smalx, mheight / 2, smallR, mPaint);
   //画链接
   //小球在右侧
   if (smalx > bigx) {
    Path path = new Path();
    //上面的贝塞尔曲线的第一个点,在大圆身上
    float x1 = bigx + bigR * (float) Math.cos(Math.toRadians(time));
    float y1 = mheight / 2 - bigR * (float) Math.sin(Math.toRadians(time));
    if (y1 > mheight / 2 - smallR) {
     y1 = mheight / 2 - smallR;
     x1 = bigx + (float) (Math.sqrt(bigR * bigR - smallR * smallR));
    }
    //上面的贝塞尔曲线的第三个点,在小圆身上
    float x2 = smalx - smallR * (float) Math.cos(Math.toRadians(time));
    float y2 = mheight / 2 - smallR * (float) Math.sin(Math.toRadians(time));
    if (y2 > mheight / 2 - smallR * 0.8) {
     y2 = mheight / 2 - smallR * 0.8f;
     x2 = smalx - smallR * (float) (Math.sqrt(1-0.64f));
    }
    //下面的贝塞尔曲线的第三个点,在小圆身上
    float x3 = smalx - smallR * (float) Math.cos(Math.toRadians(time));
    float y3 = mheight / 2 + smallR * (float) Math.sin(Math.toRadians(time));
    if (y3  mheight / 2 - smallR) {
     y1 = mheight / 2 - smallR;
     x1 = bigx - (float) (Math.sqrt(bigR * bigR - smallR * smallR));
    }
    float x2 = smalx - smallR * (float) Math.cos(Math.toRadians(time));
    float y2 = mheight / 2 - smallR * (float) Math.sin(Math.toRadians(time));
    if (y2 > mheight / 2 - smallR * 0.8) {
     y2 = mheight / 2 - smallR * 0.8f;
     x2 = smalx + smallR * (float) (Math.sqrt(1-0.64f));
    }
    float x3 = smalx - smallR * (float) Math.cos(Math.toRadians(time));
    float y3 = mheight / 2 + smallR * (float) Math.sin(Math.toRadians(time));
    if (y3 

“精心”画了一张图,对代码做了说明。


在代码中使用

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。


推荐阅读
  • MyBatis模糊查询和多条件查询一、ISmbmsUserDao层根据姓名模糊查询publicListgetUser();多条件查询publicList ... [详细]
  • 代码:在mysql5.6,运行正常,5.7报错SELECTsum((selecta.numwherea.status1))astotalFROMmes_order_productA ... [详细]
  • Git(1)
    安装Git完毕(在开始菜单打开的话,打开的不是你想要的路径,切换路径很麻烦)1.D盘新建GitTest文件夹2.打开GitTest,在空白的地方右键,3.单击GitBashHere ... [详细]
  • python基础(二、pycharm安装、卸载)
    3.在Ubuntu中安装PyCharmPyCharm的官方网站地址是:https:www.jetbrains.compycharm注意:安装时不要使用root用户安装,否则后期使用 ... [详细]
  • 在Android源码环境下编译系统App使用第三方jar包的方法(备忘)
    1将要使用的jar包放入App的根目录,即Android.mk所在目录2按如下方式编写Android.mk文件(########之间的行用于编译和使用jar包)LOCAL_PATH:$(ca ... [详细]
  • 这一篇主要总结一下jQuery这个js在引入的时候做的一些初始化工作第一句window.undefinedwindow.undefined;是为了兼容低版本的IE而写的因为在低版本 ... [详细]
  • spotify engineering culture part 1
    原文,因为原视频说的太快太长,又没有字幕,于是借助youtube,把原文听&打出来了。中文版日后有时间再翻译。oneofthebigsucceessfactorshereatSpo ... [详细]
  • 载波|等份_NR SRS时频域位置
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了NRSRS时频域位置相关的知识,希望对你有一定的参考价值。微信同步更新欢迎关注同名modem协议笔记S ... [详细]
  • Mac下Flutter安装AndroidStudio配置
    补一个Mac下Flutter安装AndroidStudio配置(官网地址:https:flutter.devdocsget-startedinstallmacos)1.下载安装包; ... [详细]
  • vscode里的html标签导航的一系列问题
    哈喽,我今天带来的经验是,vscode在18年10月更新后的1.29以后,编辑html文档时,会发现最上面有个类似于HTML标签导航的玩意儿,可能部分同学和我一样不习惯用它们,现在 ... [详细]
  • Android Rxbus事件总线
    我的视频课程:《FFmpeg打造Android万能音频播放器》最近在项目中使用了Rxjava和RxAndroid,确实感觉挺不错的,然后听说可以用RxBus来替换 ... [详细]
  • Illustrator绘制逼真的愤怒的小鸟实例教程
    Illustrator教程: ... [详细]
  • Android 自定义控件基础 canvas paint
    1、首先说一下canvas类:ClassOverviewTheCanvasclassholdsthedrawcalls.Todrawsomething,youne ... [详细]
  • 抓取百万知乎用户设计之实体设计
    一.实体的关系实体是根据返回的Json数据来设计的教育经历方面用户可以有很多教育经理,USER和education是一对多的关系,一个education对应一个education一 ... [详细]
  • iOS之富文本
    之前做项目时遇到一个问题:使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ... [详细]
author-avatar
手机用户2502877255
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有