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 * 0.8) {
     y3 = mheight / 2 + smallR * 0.8f;
     x3 = smalx - smallR * (float) (Math.sqrt(1-0.64f));
    }
    //下面的贝塞尔曲线的第一个点,在大圆身上
    float x4 = bigx + bigR * (float) Math.cos(Math.toRadians(time));
    float y4 = mheight / 2 + bigR * (float) Math.sin(Math.toRadians(time));
    if (y4 < mheight / 2 + smallR) {
     y4 = mheight / 2 + smallR;
     x4 = bigx + (float) (Math.sqrt(bigR * bigR - smallR * smallR));
    }
    path.moveTo(x1, y1);
    path.quadTo((bigx + smalx) / 2, mheight / 2, x2, y2);
    // 绘制贝赛尔曲线(Path)
    path.lineTo(x3, y3);
    path.quadTo((bigx + smalx) / 2, mheight / 2, x4, y4);
    canvas.drawPath(path, 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 * 0.8) {
     y3 = mheight / 2 + smallR * 0.8f;
     x3 = smalx + smallR * (float) (Math.sqrt(1-0.64f));
    }
    float x4 = bigx + bigR * (float) Math.cos(Math.toRadians(time));
    float y4 = mheight / 2 + bigR * (float) Math.sin(Math.toRadians(time));
    if (y4 < mheight / 2 + smallR) {
     y4 = mheight / 2 + smallR;
     x4 = bigx - (float) (Math.sqrt(bigR * bigR - smallR * smallR));
    }
    path.moveTo(x1, y1);
    path.quadTo((bigx + smalx) / 2, mheight / 2, x2, y2);
    // 绘制贝赛尔曲线(Path)
    path.lineTo(x3, y3);
    path.quadTo((bigx + smalx) / 2, mheight / 2, x4, y4);
    canvas.drawPath(path, mPaint);
   }
   postInvalidate();
  }
 }
 //计算小球的X坐标
 private float getSmallCenterX() {
  //此处控制速度
  time = time + 2.5f;
  return mWidth / 2 + distance * (float) Math.cos(Math.toRadians(time));
 }
}

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

在代码中使用

<?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="@color/white" tools:context="top.greendami.greendami.MainActivity">
 <top.greendami.greendami.PPView
  android:layout_centerInParent="true"
  android:layout_width="400dp"
  android:layout_height="80dp" />
</RelativeLayout>

总结

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

(0)

相关推荐

  • Android自定义View实现loading动画加载效果

    项目开发中对Loading的处理是比较常见的,安卓系统提供的不太美观,引入第三发又太麻烦,这时候自己定义View来实现这个效果,并且进行封装抽取给项目提供统一的loading样式是最好的解决方式了. 先自定义一个View,继承自LinearLayout,在Layout中,添加布局控件 /** * Created by xiedong on 2017/3/7. */ public class Loading_view extends LinearLayout { private Context m

  • Android中Market的Loading效果实现方法

    本文实例讲述了Android中Market的Loading效果实现方法.分享给大家供大家参考.具体如下: 在Android中,要实现Loading效果,一般情况下都使用ProgressDialog控件.ApiDemos/src/com/example/android/apis/view/ProgressBar3.java 提供两个demo: 仔细看了Android Market,发现却是不一样的,请看截图: 那到底如何实现呢?首先,我们创建一个布局文件, res/layout/fullscree

  • Android 自定义通用的loadingview实现代码

    功能 1.显示加载视图,加载失败的时候显示加载失败视图,数据为空时显示数据为空视图,支持为失败视图设置点击事件重新加载数据. 2.支持个性化设置,自定义设置 加载.失败.空数据视图. 先放一张效果图压压惊 实现 实现思路其实就是一个FrameLayout里添加三个布局做处理显示隐藏,自定义视图其实就是替换里面的view ,代码比较简单,如果直接看过我的自定义view系列文章,或者对自定义view有所了解,都很容易看懂,所有直接上代码了. 具体代码 Java 代码 public class Com

  • 三款Android炫酷Loading动画组件推荐

    最近突然心血来潮,对一些Loading感兴趣,Loading这玩意说重要也重要,说不重要也不重要,因为这是一个提升你产品体验的一个细节,如果loading做的好,对于一些耗时需要用户等待的页面来说会转移用户注意力,不会显得那么烦躁,所以你可以看到市面上各种各样好玩的Loading动画,那么这篇博客就准备收集下一些Loading动画吧,从这些实现思路上可以打开你们自己的思维,没准也会有创新好玩的Loading动画出现. 暂且先列举些最近GitHub新鲜出炉的Loading CircleProgre

  • Android项目实战手把手教你画圆形水波纹loadingview

    本文实例讲解的是如何画一个满满圆形水波纹loadingview,这类效果应用场景很多,比如内存占用百分比之类的,分享给大家供大家参考,具体内容如下 效果图如下: 预备的知识: 1.贝塞尔曲线    如果你不了解,可以来这里进行基础知识储备:神奇的贝塞尔曲线 2.Paint.setXfermode()  以及PorterDuffXfermode 千万不要被这个b的名字吓到,不熟悉看到可能会认为很难记,其实 只要站在巨人的丁丁上 还是很简单的. 好了 废话不多说 ,跟我一步步来做一个炫酷的view吧

  • Android自定义加载loading view动画组件

    在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress )的代码 package com.hysmarthotel.view; import com.hysmarthotel.roomcontrol.R; import com.hysmarthotel.util.EaseInOutCubicInterpolator; import android.ani

  • Android自定义环形LoadingView效果

    最近项目有要用到环形的进度条,Github上有一个类似的DashedCircularProgress控件,但是他画的进度是通过设置画笔的虚线效果来实现间隔的:progressPaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, dashSpace));如果内层还有一层圆环,在动态设置时,内层和外层有细微的偏差.于是我在原有基础上改了一个,实现了我要的效果(设置进度时可以选择加动画或者不加动画): 控件实现

  • Android实现创意LoadingView动画效果

    Android上的热火锅煮萝卜蔬菜的Loading动画效果. 这是一个锅煮萝卜的Loading动画,效果仿照自之前IOS上看到的一个效果,觉得挺有意思,就移植过来了,在此完成了Dialog的样式,方便使用者作为LoadingView去使用. 关键性代码: package yellow5a5.demo.boilingloadingview.View; import android.animation.Animator; import android.animation.AnimatorListen

  • 一看就喜欢的loading动画效果Android分析实现

    还是比较有新意,复杂度也不是非常高,所以就花时间整理一下,我们先一起看下原gif图效果: 从效果上看,我们需要考虑以下几个问题: 1.叶子的随机产生: 2.叶子随着一条正余弦曲线移动: 3.叶子在移动的时候旋转,旋转方向随机,正时针或逆时针: 4.叶子遇到进度条,似乎是融合进入: 5.叶子不能超出最左边的弧角: 7.叶子飘出时的角度不是一致,走的曲线的振幅也有差别,否则太有规律性,缺乏美感: 总的看起来,需要注意和麻烦的地方主要是以上几点,当然还有一些细节问题,比如最左边是圆弧等等: 那接下来我

  • Android Studio卡很久(loading)的问题解决办法

    Android Studio卡很久(loading)的问题 关于Android Studio卡在某个地方很久(更准确说应该是Loading很久)的问题,大多是因为可能在下载Gradle或者在连接其他需要fan墙的网址.事实上,目前每一个使用Android Studio的Coder都应该有个fan墙工具和对应的账号.以ShadowShocks为例,如下. 1.打开ShadowShocks客户端开启fan墙模式(或其他工具). 2.Android Studio中设置HTTP Proxy,点击"Set

随机推荐