欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球

這篇文章給大家介紹怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

創(chuàng)新互聯(lián)建站專注于貴州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供貴州營(yíng)銷型網(wǎng)站建設(shè),貴州網(wǎng)站制作、貴州網(wǎng)頁(yè)設(shè)計(jì)、貴州網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造貴州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供貴州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

1、定義BallProgress,BallProgress繼承View,重寫onDraw()方法,用于實(shí)現(xiàn)進(jìn)度球的view。

public class BallProgress extends View {
 private float mProgress = 0.0f; //取值位 0 - 1.0

 private boolean selected = true;

 public BallProgress(Context context) {
 super(context);
 }

 public BallProgress(Context context, @Nullable AttributeSet attrs) {
 super(context, attrs);
 }

 public BallProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init();
 }

 private void init() {
 mProgressPaint = new Paint();//初始化,定義畫筆。
 mProgressPaint.setAntiAlias(true);//設(shè)置抗鋸齒
 }

 public float getProgress() {
 return mProgress;
 }

 public void setProgress(float progress) {//設(shè)置進(jìn)度,通過(guò)進(jìn)度的大小實(shí)現(xiàn)裁剪的大小
 mProgress = progress;
 invalidate();
 }

 private Paint mProgressPaint;


 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 Bitmap dst = getRectangleBitmap();//獲取bitmap
 setLayerType(LAYER_TYPE_HARDWARE, null); //開(kāi)啟硬件離屏緩存
 canvas.drawBitmap(dst, 0, 0, mProgressPaint);
 }
 private Bitmap getRectangleBitmap() {
 int width = getWidth();
 int height = getHeight();
 
 Bitmap dstBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
 
 ClipDrawable clipDrawable = null;
  clipDrawable = (ClipDrawable) getContext().getResources().getDrawable(R.drawable.bottom_top_clip_single_color);//獲取球形的背景圖片,用于裁剪,就是上面看到的進(jìn)度球中的圖片
  
 clipDrawable.setBounds(new Rect(0, 0, width, height));//設(shè)置邊界
 
 clipDrawable.setLevel((int) (10000 * mProgress));//設(shè)置進(jìn)度,
 
 Canvas canvas = new Canvas(dstBitmap);//設(shè)置畫布
 
 clipDrawable.draw(canvas);//繪制
 
 return dstBitmap;//將bitmap返回
 }

}

有了自定義的BallProgress,就可以在布局中使用了,定義的xml文件如下:

<?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"
 tools:context=".MainActivity">

 <com.example.admin.floatprogressbar.BallProgress
 android:id="@+id/progress"
 android:layout_width="@dimen/progress_size"
 android:layout_height="@dimen/progress_size"
 android:layout_centerInParent="true" />
 <ImageView
 android:layout_width="@dimen/progress_size"
 android:layout_height="@dimen/progress_size"
 android:layout_centerInParent="true"
 android:background="@drawable/main_tab_un_select_bg" />

</RelativeLayout>

上面布局中的ImageView是懸浮球的邊界。在MainActivity中來(lái)定時(shí)的改變進(jìn)度球的大小。代碼如下:

public class MainActivity extends AppCompatActivity {
 private final int PROGRESS_MESSAGE = 0;
 private float progress = 0.0f;
 private BallProgress mBallProgress;
 private Handler mHandler = new Handler(new Handler.Callback() {
 @Override
 public boolean handleMessage(Message msg) {
  switch (msg.what) {
  case PROGRESS_MESSAGE:
   progress = (progress > 0.9f) ? 0.9f : progress;
   mBallProgress.setProgress(progress);//接收消息,改變進(jìn)度球的進(jìn)度
   break;
  }
  return true;
 }
 });
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initView();
 initAction();
 }

 private void initView() {
 mBallProgress = findViewById(R.id.progress);
 }
 //發(fā)消息
 private void initAction() {
 new Thread(new Runnable() {
  @Override
  public void run() {
  while (true) {
   progress += 0.02f;
   try {
   Thread.sleep(100);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   mHandler.sendEmptyMessage(PROGRESS_MESSAGE);//每隔100毫秒發(fā)送一次消息,對(duì)進(jìn)度球進(jìn)度進(jìn)行更新。
  }
  }
 }).start();
 }

}

上面代碼在inAction()中開(kāi)一個(gè)線程,每隔100毫秒發(fā)送消息,在handler中處理更新,在handler使用中并沒(méi)有直接重寫hanldeMessage方法,而是傳入Handler.Callback并在callback中實(shí)現(xiàn)handleMessage方法,這樣可以防止內(nèi)存泄漏。實(shí)際應(yīng)用中,可以是跟業(yè)務(wù)相關(guān)的具體進(jìn)度。

總結(jié)

自定義進(jìn)度球,用的是繼承view,并且通過(guò)自定義畫筆,重寫onDraw()方法來(lái)實(shí)現(xiàn),一般自定義view都會(huì)重寫onDraw()方法,一般進(jìn)度條都是ClipDrawable來(lái)實(shí)現(xiàn)的。

源碼地址:進(jìn)度球代碼

帶波浪的進(jìn)度球

上面已經(jīng)實(shí)現(xiàn)了簡(jiǎn)單的進(jìn)度球,但是效果不是很好,根據(jù)評(píng)論區(qū)中的提議加上動(dòng)畫和貝塞爾曲線波紋實(shí)現(xiàn)了下面的效果,只取了進(jìn)度處于某一固定進(jìn)度的動(dòng)畫效果如圖:

怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球

準(zhǔn)備知識(shí)

二階貝塞爾曲線

貝塞爾曲線是用一系列點(diǎn)來(lái)控制曲線狀態(tài)的,將這些點(diǎn)簡(jiǎn)單分為兩類:

怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球

二階貝塞爾曲線的路徑由給定點(diǎn)P0、P1、P2的函數(shù)B(t)函數(shù)方程如下:

怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球

二階曲線由兩個(gè)數(shù)據(jù)點(diǎn)(P0 和 P2),一個(gè)控制點(diǎn)(P1)來(lái)描述曲線狀態(tài),大致如下:

怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球

android中自帶實(shí)現(xiàn)二階貝塞爾曲線的api,在Path類中的函數(shù)quadTo 。

public void quadTo (float x1, 
    float y1, 
    float x2, 
    float y2)

其中(x1,y1)是上圖中控制點(diǎn)p1的坐標(biāo),(x2,y2)是上圖中數(shù)據(jù)點(diǎn)結(jié)束位置p2的坐標(biāo),數(shù)據(jù)點(diǎn)p0的默認(rèn)坐標(biāo)是(0,0),也可以用函數(shù)moveTo(x,y)來(lái)改變p0坐標(biāo)。要實(shí)現(xiàn)上面進(jìn)度球進(jìn)度的波動(dòng)效果,就要將兩個(gè)貝塞爾曲線結(jié)合起來(lái),并且動(dòng)態(tài)的改變兩個(gè)貝塞爾曲線的數(shù)據(jù)點(diǎn)和控制點(diǎn),這樣就會(huì)使用戶感覺(jué)到波動(dòng)的效果。

實(shí)現(xiàn)

/**
 * Created time 17:24.
 *
 * @author huhanjun
 * @since 2019/1/2
 */
public class BezierFloatView extends View {

 private double rArc=0;
 private double percent=0;

 public BezierFloatView(Context context) {
  super(context);
 }

 public BezierFloatView(Context context, @Nullable AttributeSet attrs) {
  super(context, attrs);
  initPaint();
  initAnimator();
 }
 private RectF rectF;
 private int mWidth, mHeight;//畫布的寬帶和高度
 private float cycleWidth, cycleHeight = 30f;//周期的寬度和高度
 private Path pathRipple;//畫的路徑
 private Paint paintRipple;//畫筆
 private float moveSet = 0;//移動(dòng)的值
 private ValueAnimator animator;//動(dòng)畫
 private boolean isStart = false;

 /**
  * 初始化動(dòng)畫
  */
 private void initAnimator() {
  animator = ValueAnimator.ofFloat(0, mWidth);
  animator.setRepeatCount(ValueAnimator.INFINITE);
  animator.setDuration(800);
  animator.setInterpolator(new LinearInterpolator());
  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    percent=valueAnimator.getAnimatedFraction();
    moveSet = valueAnimator.getAnimatedFraction() * mWidth;
    invalidate();
   }
  });
 }

 /**
  * 初始化畫的路徑
  */
 private void initPath() {
  rArc = mWidth*(1-2*percent);
  double angle= Math.acos(rArc/mWidth);
  pathRipple = new Path();
  pathRipple.moveTo(-6 * cycleWidth + moveSet, 0);
  pathRipple.quadTo(-5 * cycleWidth + moveSet, cycleHeight, -4 * cycleWidth + moveSet, 0);
  pathRipple.quadTo(-3 * cycleWidth + moveSet, -cycleHeight, -2 * cycleWidth + moveSet, 0);
  pathRipple.quadTo(-cycleWidth + moveSet, cycleHeight, moveSet, 0);
  pathRipple.quadTo(cycleWidth + moveSet, -cycleHeight, 2 * cycleWidth + moveSet, 0);
  pathRipple.addArc(rectF,0,180);
  pathRipple.close();
  pathRipple.setFillType(Path.FillType.WINDING);
 }

 /**
  * 初始化畫筆
  */
 private void initPaint() {
  paintRipple = new Paint();
  paintRipple.setStrokeWidth(2);
  paintRipple.setStyle(Paint.Style.FILL);
  paintRipple.setColor(Color.BLUE);
  paintRipple.setAntiAlias(true);
 }

 /**
  * 畫波紋
  *
  * @param canvas
  */
 private void drawRipple(Canvas canvas) {
  initPath();
  canvas.drawPath(pathRipple, paintRipple);
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  mHeight = h;
  mWidth = w;
  cycleWidth = mWidth / 4;
  float r = Math.min(mWidth,mHeight)*0.48f;
  rectF = new RectF(-r,-r,r,r);
  initAnimator();
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.translate(mWidth / 2, mHeight / 2);
  drawRipple(canvas);
 }

 /**
  * 開(kāi)始動(dòng)畫
  */
 public void start() {
  if (isStart) return;
  isStart = true;
  if (animator == null) {
   initAnimator();
  }
  animator.start();
 }

 /**
  * 結(jié)束動(dòng)畫
  */
 public void stop() {
  if (!isStart) return;
  isStart = false;
  moveSet = 0;
  animator.cancel();
  animator = null;
  invalidate();
 }

}

關(guān)于怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文標(biāo)題:怎么在Android中自定義View實(shí)現(xiàn)球體進(jìn)度球
轉(zhuǎn)載來(lái)源:http://chinadenli.net/article0/piedoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航ChatGPT云服務(wù)器網(wǎng)站策劃動(dòng)態(tài)網(wǎng)站企業(yè)建站

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司