内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Android如何實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar

小編給大家分享一下Android如何實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站長(zhǎng)期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為信陽(yáng)企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),信陽(yáng)網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

前言

我們平時(shí)在開發(fā)的時(shí)候,發(fā)起網(wǎng)絡(luò)請(qǐng)求前,會(huì)需要顯示一個(gè)Loading,一般的做法都是在xml布局上添加好Loading,然后在Activity中,setVisibility來控制Loading的顯示和隱藏,這樣使用起來就很不方便,因?yàn)槊恳粋€(gè)xml都得引入一個(gè)Loading布局。

而LoadingBar就更好的解決了這個(gè)問題

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

先上效果圖:

Android如何實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar

實(shí)例代碼

然后是源碼,就是一個(gè)簡(jiǎ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";
 //動(dòng)畫開關(guān)
 boolean isLoading = true;
 Context mContext;
 private int mWidth = 100;
 private int mheight = 100;
 public int mColor;
 public Paint mPaint = new Paint();
 float time = 0;
 //小球與中間打球的最遠(yuǎn)距離
 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);
   //畫鏈接
   //小球在右側(cè)
   if (smalx > bigx) {
    Path path = new Path();
    //上面的貝塞爾曲線的第一個(gè)點(diǎn),在大圓身上
    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));
    }
    //上面的貝塞爾曲線的第三個(gè)點(diǎn),在小圓身上
    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));
    }
    //下面的貝塞爾曲線的第三個(gè)點(diǎn),在小圓身上
    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));
    }
    //下面的貝塞爾曲線的第一個(gè)點(diǎn),在大圓身上
    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);
   }
   //小球在左側(cè)
   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();
  }
 }
 //計(jì)算小球的X坐標(biāo)
 private float getSmallCenterX() {
  //此處控制速度
  time = time + 2.5f;
  return mWidth / 2 + distance * (float) Math.cos(Math.toRadians(time));
 }
}

“精心”畫了一張圖,對(duì)代碼做了說明。

Android如何實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar

在代碼中使用

<?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如何實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞標(biāo)題:Android如何實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar
URL鏈接:http://www.rwnh.cn/article12/jsdpgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、全網(wǎng)營(yíng)銷推廣、域名注冊(cè)、Google、網(wǎng)站建設(shè)ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
张掖市| 富裕县| 洛隆县| 岳西县| 满城县| 井研县| 景东| 武乡县| 洛浦县| 太保市| 灵武市| 射阳县| 广东省| 北海市| 通渭县| 枣阳市| 乌鲁木齐县| 萨嘎县| 扶绥县| 乌兰县| 西峡县| 民乐县| 中山市| 岳普湖县| 丹江口市| 伊通| 如皋市| 朝阳县| 江源县| 仙游县| 互助| 罗城| 莲花县| 富阳市| 农安县| 荣成市| 江北区| 镇坪县| 孟津县| 太保市| 当涂县|