欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)一個帶粘連效果的LoadingBar

 更新時間:2017年12月15日 09:54:49   作者:Greenda米  
Loading效果相信大家應該都實現(xiàn)過,最近發(fā)現(xiàn)了一個不錯的效果,決定分享給大家,所以下面這篇文章主要給大家介紹了關于利用Android實現(xiàn)一個帶粘連效果的LoadingBar的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

我們平時在開發(fā)的時候,發(fā)起網(wǎng)絡請求前,會需要顯示一個Loading,一般的做法都是在xml布局上添加好Loading,然后在Activity中,setVisibility來控制Loading的顯示和隱藏,這樣使用起來就很不方便,因為每一個xml都得引入一個Loading布局。

而LoadingBar就更好的解決了這個問題

最近設計師在外國的一個網(wǎng)站上挑了一個Loading的效果圖,嘗試實現(xiàn)之后,雖然和原圖有點不太一樣,但是效果還是不錯的。難點就是粘連效果的實現(xiàn),貝塞爾曲線的點點們簡直要把我折磨死了。

先上效果圖:


實例代碼

然后是源碼,就是一個簡單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>

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • Android入門之彈出式對話框的實現(xiàn)

    Android入門之彈出式對話框的實現(xiàn)

    Android Studio里有一種Dialog叫PopWindow,它是一種“可阻塞式Dialog”,即彈出后除非你給它一個“動作”否則就一直顯示在那。本文就將實現(xiàn)這樣的彈出式對話框,感興趣的可以了解一下
    2022-11-11
  • Android自定義輸入框提示功能

    Android自定義輸入框提示功能

    這篇文章主要為大家詳細介紹了Android自定義輸入框提示功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Android判斷11位手機號碼的方法(正則表達式)

    Android判斷11位手機號碼的方法(正則表達式)

    項目里頭需要做一個判斷用戶輸入的號碼是否是正確的手機號碼,正確的手機號碼應該是11位的,這里我們需要用一個正則表達式來進行判斷,下面我把寫法分享給大家
    2016-12-12
  • Flutter輸入框TextField屬性及監(jiān)聽事件介紹

    Flutter輸入框TextField屬性及監(jiān)聽事件介紹

    這篇文章主要介紹了Flutter輸入框TextField屬性及監(jiān)聽事件介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-11-11
  • Android鍵盤輸入語言設置默認打開myanmar緬甸語的步驟

    Android鍵盤輸入語言設置默認打開myanmar緬甸語的步驟

    如何實現(xiàn)Android鍵盤輸入語言默認打開為myanmar緬甸語,如果要設置某種語言在輸入法默認打開可按一下步驟添加文件,我這里已經(jīng)驗證時OK的
    2013-06-06
  • Android搜索框SearchView屬性和用法詳解

    Android搜索框SearchView屬性和用法詳解

    這篇文章主要為大家詳細介紹了Android搜索框SearchView屬性和用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • android利用xml實現(xiàn)分割線

    android利用xml實現(xiàn)分割線

    這篇文章主要介紹了android利用xml實現(xiàn)分割線的方法,如何用xml產生一個分割線?感興趣的小伙伴們可以參考一下
    2015-12-12
  • Flutter 如何設置App的主色調與字體

    Flutter 如何設置App的主色調與字體

    App 開發(fā)過程中,肯定希望給用戶帶來一致的體驗,這其中最基礎的就是色調、字體保持一致。在 Flutter 中,可以設置全局的主題色調和字體,從而在其他頁面引用主色調和字體,實現(xiàn)頁面展示層面的一致。
    2021-05-05
  • Android原生項目集成React Native的方法

    Android原生項目集成React Native的方法

    本篇文章主要介紹了Android原生項目集成React Native的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android 如何使用log4j及注意事項

    Android 如何使用log4j及注意事項

    這篇文章主要介紹了Android 如何使用log4j及注意事項的相關資料,需要的朋友可以參考下
    2017-01-01

最新評論