最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義view實現(xiàn)圓形waveview

 更新時間:2020年09月17日 10:01:21   作者:新助錦鵬  
這篇文章主要為大家詳細(xì)介紹了Android自定義view實現(xiàn)圓形waveview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近學(xué)習(xí)了貝塞爾曲線的一些知識,剛好項目中需要實現(xiàn)一個圓形進(jìn)度,然后就將實現(xiàn)的waveView記錄一下。需要使用的知識大概有自定義view、貝塞爾曲線、valueAnimator(屬性動畫)、Xfermode等。

以下為效果圖:

廢話不多說,直接上代碼這里只是一些重要的代碼。如果需要demo可以去下載。

下載地址

首先需要自定義view的屬性:

<declare-styleable name="custom_wave_view_attr">

 <attr name="circle_color" format="color"></attr> //圓的顏色
 <attr name="circle_background_color" format="color"></attr> //圓的背景色
 <attr name="progress_wave_color" format="color"></attr> //水波紋的顏色
 <attr name="progress_text_size" format="dimension"></attr> //字體的大小
 <attr name="progress_text_color" format="color"></attr> //字體的顏色

</declare-styleable>

第二步自定義CustomWaveView

1、實現(xiàn)構(gòu)造方法,在構(gòu)造方法中獲取屬性值

TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.custom_wave_view_attr);
//圓的顏色
circle_color = ta.getColor(R.styleable.custom_wave_view_attr_circle_color,getResources().getColor(android.R.color.black));
//圓的背景色
circle_bg_color = ta.getColor(R.styleable.custom_wave_view_attr_circle_background_color,getResources().getColor(android.R.color.white));
//水波紋顏色
wave_color = ta.getColor(R.styleable.custom_wave_view_attr_progress_wave_color,getResources().getColor(android.R.color.holo_blue_dark));
//字體的顏色
text_color = ta.getColor(R.styleable.custom_wave_view_attr_progress_text_color,getResources().getColor(android.R.color.black));
//字體的大小
textSize = ta.getDimension(R.styleable.custom_wave_view_attr_progress_text_size,30f);
//釋放資源
ta.recycle();

2、初始化畫筆

//初始化背景圓畫筆
mBgCirclePaint = new Paint();
//抗鋸齒
mBgCirclePaint.setAntiAlias(true);
//設(shè)置背景圓的背景色
mBgCirclePaint.setColor(circle_bg_color);
//設(shè)置充滿
mBgCirclePaint.setStyle(Paint.Style.FILL);
//初始化水波紋畫筆
mWavePaint = new Paint();
//抗鋸齒
mWavePaint.setAntiAlias(true);
//設(shè)置水波紋的背景色
mWavePaint.setColor(wave_color);
//設(shè)置充滿
mWavePaint.setStyle(Paint.Style.FILL);
//使用Xfermode獲取重疊部分
mWavePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

3、繪制貝塞爾曲線。以下為原理圖。

/**
* 初始化貝塞爾曲線上的點
*/
private void reset() {
 startP = new PointF(-width, height);
 nextP = new PointF(-width/2, height);
 threeP = new PointF(0, height);
 fourP = new PointF(width/2, height);
 endP = new PointF(width, height);
 controllerP1 = new PointF(-width/4, height);
 controllerP2 = new PointF(-width * 3/4, height);
 controllerP3 = new PointF(width/4, height);
 controllerP4 = new PointF(width * 3/4, height);
}

4、在onDraw方法中畫貝塞爾曲線和圓

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //在透明畫布上畫背景圓
 mCanvas.drawCircle(width/2, height/2, radius, mBgCirclePaint);
 //貝塞爾曲線
 mPath.reset();
 mPath.moveTo(startP.x, startP.y);
 mPath.quadTo(controllerP1.x, controllerP1.y, nextP.x, nextP.y);
 mPath.quadTo(controllerP2.x, controllerP2.y, threeP.x, threeP.y);
 mPath.quadTo(controllerP3.x, controllerP3.y, fourP.x, fourP.y);
 mPath.quadTo(controllerP4.x, controllerP4.y, endP.x, endP.y);
 mPath.lineTo(endP.x, height);
 mPath.lineTo(-width, height);
 //在透明畫布上繪制水波紋
 mCanvas.drawPath(mPath,mWavePaint);
 //將畫好的圓繪制在畫布上
 canvas.drawBitmap(mBitmap, 0, 0, null);
}

5、使用動畫讓貝塞爾曲線動起來

/**
* 開始動畫 讓startP的x點坐標(biāo)在2S時間內(nèi)循環(huán)移動到0點。
* depth---進(jìn)度
* waveRipple----水波紋的振幅
*/
private void startAnimator() {
 animator = ValueAnimator.ofFloat(startP.x, 0);
 animator.setInterpolator(new LinearInterpolator());
 animator.setDuration(2000);
 //重復(fù)循環(huán)
 animator.setRepeatCount(ValueAnimator.INFINITE);
 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator animation) {
 startP.x = (Float) animation.getAnimatedValue();
 startP = new PointF(startP.x, height - depth);
 nextP = new PointF(startP.x + width/2, height - depth);
 threeP = new PointF(nextP.x + width/2, height - depth);
 fourP = new PointF(threeP.x + width/2, height - depth);
 endP = new PointF(fourP.x + width/2, height - depth);
 controllerP1 = new PointF(startP.x + width/4, height - depth + waveRipple);
 controllerP2 = new PointF(nextP.x + width/4, height - depth - waveRipple);
 controllerP3 = new PointF(threeP.x + width/4, height - depth + waveRipple);
 controllerP4 = new PointF(fourP.x + width/4, height - depth - waveRipple);
 invalidate();
 }
 });
 animator.start();
}

第三步在XML中使用自定義View

<com.criclewaveview_master.CustomWaveView
 android:id="@+id/custom_circle_wave_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 wave:circle_color = "@color/circle_color"
 android:layout_centerInParent="true"
 wave:circle_background_color = "@color/circle_bg_color"
 wave:progress_wave_color = "@color/colorAccent"
 wave:progress_text_size = "20sp"
 wave:progress_text_color = "@color/circle_color"/>

這樣就完成了自定義WaveView。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實現(xiàn)QQ登錄界面遇到問題及解決方法

    Android實現(xiàn)QQ登錄界面遇到問題及解決方法

    本文給大家介紹android仿qq登錄界面的實現(xiàn)代碼,在實現(xiàn)此功能過程中遇到各種問題,但是最終都順利解決,如果大家對android qq登錄界面實現(xiàn)方法感興趣的朋友一起學(xué)習(xí)吧
    2016-09-09
  • Android  GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實例詳解

    Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實例詳解

    這篇文章主要介紹了Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實例詳解的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Android Messenger實現(xiàn)進(jìn)程間通信及其原理

    Android Messenger實現(xiàn)進(jìn)程間通信及其原理

    這篇文章主要為大家詳細(xì)介紹了Android Messenger實現(xiàn)進(jìn)程間通信及其原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android UniversalVideoView實現(xiàn)視頻播放器

    Android UniversalVideoView實現(xiàn)視頻播放器

    這篇文章主要為大家詳細(xì)介紹了Android UniversalVideoView實現(xiàn)視頻播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Flutter?LinearProgressIndicator使用指南分析

    Flutter?LinearProgressIndicator使用指南分析

    這篇文章主要為大家介紹了Flutter?LinearProgressIndicator使用指南分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android開發(fā)新手常見的10個誤區(qū)

    Android開發(fā)新手常見的10個誤區(qū)

    這篇文章主要介紹了Android開發(fā)新手常見的10個誤區(qū),我們?nèi)匀豢吹搅四男┬碌腁ndr&#8203;&#8203;oid開發(fā)人員不斷重復(fù)的錯誤,這里有10個最常見的誤區(qū),需要的朋友可以參考下
    2015-03-03
  • Android實例代碼理解設(shè)計模式SOLID六大原則

    Android實例代碼理解設(shè)計模式SOLID六大原則

    程序設(shè)計領(lǐng)域, SOLID (單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉(zhuǎn))是由羅伯特·C·馬丁在21世紀(jì)早期 引入的記憶術(shù)首字母縮略字,指代了面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計的基本原則
    2021-10-10
  • Android實現(xiàn)日期時間選擇對話框

    Android實現(xiàn)日期時間選擇對話框

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)日期以及時間選擇對話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android自定義View實現(xiàn)搜索框(SearchView)功能

    Android自定義View實現(xiàn)搜索框(SearchView)功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)搜索框SearchView功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android APK優(yōu)化工具Zipalign詳解

    Android APK優(yōu)化工具Zipalign詳解

    本文主要介紹Android APK優(yōu)化工具Zipalign,這里整理了相關(guān)資料,并詳細(xì)介紹如何使用Zipalign工具及使用技巧,有需要的小伙伴可以參考下
    2016-09-09

最新評論

兴宁市| 泰来县| 喀什市| 南投市| 白银市| 宣城市| 灵山县| 泾阳县| 晋中市| 阿荣旗| 铁力市| 汤原县| 保亭| 丹阳市| 江西省| 日照市| 偃师市| 南召县| 长阳| 岐山县| 蒙自县| 金川县| 利辛县| 长海县| 达尔| 广水市| 泉州市| 广元市| 洛川县| 南澳县| 泊头市| 恩平市| 天水市| 恩施市| 阿勒泰市| 西和县| 澳门| 昌宁县| 正阳县| 射阳县| 陆丰市|