Android自定義View實(shí)現(xiàn)簡單炫酷的球體進(jìn)度球?qū)嵗a
前言
最近一直在研究自定義view,正好項(xiàng)目中有一個(gè)根據(jù)下載進(jìn)度來實(shí)現(xiàn)球體進(jìn)度的需求,所以自己寫了個(gè)進(jìn)度球,代碼非常簡單。先看下效果:

效果還是非常不錯(cuò)的。
準(zhǔn)備知識
要實(shí)現(xiàn)上面的效果我們只要掌握兩個(gè)知識點(diǎn)就好了,一個(gè)是Handler機(jī)制,用于發(fā)消息刷新我們的進(jìn)度球,一個(gè)是clipDrawable。網(wǎng)上關(guān)于Handler的教程很多,這里重點(diǎn)介紹一下clipDrawable,進(jìn)度球的實(shí)現(xiàn)全靠clipDrawable。
clipDrawable
如下圖所示:ClipDrawable和InsertDrawable一樣繼承DrawableWrapper,DrawableWrapper繼承Drawable。

ClipDrawable是可以進(jìn)行裁剪操作的drawable,提供了函數(shù)setLevel(@IntRange(from=0,to=10000) int level)來設(shè)置裁剪的大小。level越大圖片越大。level=0時(shí)圖片完全不顯示,level=10000時(shí)圖片完全顯示。
ClipDrawable clipDrawable = (ClipDrawable) getContext().getResources().getDrawable(R.drawable.bottom_top_clip_gradient_color);//獲取圖片 clipDrawable.setLevel(100);//進(jìn)行裁剪
一般還要設(shè)置裁剪的方向,垂直裁剪還是水平裁剪,我們這個(gè)進(jìn)度球用的是垂直從下向上裁剪。
思路:
知道了ClipDrawable的用法,進(jìn)度球就好實(shí)現(xiàn)了。只需要一個(gè)球形的圖片,從下往上裁剪,通過設(shè)置setLevel從0到10000,就可以實(shí)現(xiàn)進(jìn)度球從0到進(jìn)度100的效果了。
實(shí)現(xiàn)
1、定義BallProgress,BallProgress繼承View,重寫onDraw()方法,用于實(shí)現(xiàn)進(jìn)度球的view。
/**
* Created time 15:02.
*
* @author huhanjun
* @since 2018/12/26
*/
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)度,通過進(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); //開啟硬件離屏緩存
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中來定時(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ā)送一次消息,對進(jìn)度球進(jìn)度進(jìn)行更新。
}
}
}).start();
}
}
上面代碼在inAction()中開一個(gè)線程,每隔100毫秒發(fā)送消息,在handler中處理更新,在handler使用中并沒有直接重寫hanldeMessage方法,而是傳入Handler.Callback并在callback中實(shí)現(xiàn)handleMessage方法,這樣可以防止內(nèi)存泄漏。實(shí)際應(yīng)用中,可以是跟業(yè)務(wù)相關(guān)的具體進(jìn)度。
總結(jié)
自定義進(jìn)度球,用的是繼承view,并且通過自定義畫筆,重寫onDraw()方法來實(shí)現(xiàn),一般自定義view都會重寫onDraw()方法,一般進(jìn)度條都是ClipDrawable來實(shí)現(xiàn)的。
源碼地址:進(jìn)度球代碼
帶波浪的進(jìn)度球
上面已經(jīng)實(shí)現(xiàn)了簡單的進(jìn)度球,但是效果不是很好,根據(jù)評論區(qū)中的提議加上動(dòng)畫和貝塞爾曲線波紋實(shí)現(xiàn)了下面的效果,只取了進(jìn)度處于某一固定進(jìn)度的動(dòng)畫效果如圖:

準(zhǔn)備知識
二階貝塞爾曲線
貝塞爾曲線是用一系列點(diǎn)來控制曲線狀態(tài)的,將這些點(diǎn)簡單分為兩類:

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

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

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)來改變p0坐標(biāo)。要實(shí)現(xiàn)上面進(jìn)度球進(jìn)度的波動(dòng)效果,就要將兩個(gè)貝塞爾曲線結(jié)合起來,并且動(dòng)態(tài)的改變兩個(gè)貝塞爾曲線的數(shù)據(jù)點(diǎn)和控制點(diǎn),這樣就會使用戶感覺到波動(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);
}
/**
* 開始動(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();
}
}
在initPath()中繪制了兩條貝塞爾曲線,通過動(dòng)態(tài)的改變moveset的值來改變p0,p2的值,形成波動(dòng)效果,通過pathRipple.addArc(rectF,0,180);實(shí)現(xiàn)一個(gè)與貝塞爾曲線形成包圍的半圓,通過start()來開始動(dòng)畫,通過stop()來結(jié)束動(dòng)畫。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Android利用ObjectAnimator實(shí)現(xiàn)ArcMenu
- Android獲取其他應(yīng)用中的assets資源
- Android中WindowManager與WMS的解析
- Android自定義動(dòng)態(tài)壁紙開發(fā)(時(shí)鐘)
- Android自定義View仿騰訊TIM下拉刷新View
- Android高性能日志寫入方案的實(shí)現(xiàn)
- Android動(dòng)態(tài)修改應(yīng)用圖標(biāo)與名稱的方法實(shí)例
- Android實(shí)現(xiàn)百度地圖兩點(diǎn)畫弧線
- Android百度地圖定位、顯示用戶當(dāng)前位置
- ObjectAnimator屬性動(dòng)畫源碼分析篇
相關(guān)文章
解決WebView通過URL加載H5界面出現(xiàn)空白的問題
這篇文章主要介紹了解決WebView通過URL加載H5界面出現(xiàn)空白的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Flutter 和 Android 互相傳遞數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了Flutter 和 Android 互相傳遞數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑
下面小編就為大家?guī)硪黄獪\談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果
這篇文章主要為大家詳細(xì)介紹了Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09

