Android自定義View實現(xiàn)簡單水波紋效果
本文實例為大家分享了Android自定義View實現(xiàn)水波紋效果的具體代碼,供大家參考,具體內(nèi)容如下
效果如下:
原理
控制代碼
//這里用的kotlin //主線程刷新控件 ?val mHandler = object : Handler() { ? ? ? ? override fun handleMessage(msg: Message?) { ? ? ? ? ? ? waterRippleView.refreshView() ? ? ? ? } ? ?? //開啟動畫,開線程,延時刷新period值,畫布進行x方向平移 private fun progressAdd() { ? ? ? ? isAnimate = true ? ? ? ? Thread(Runnable { ? ? ? ? ? ? while (isAnimate) { ? ? ? ? ? ? ? ? Thread.sleep(100) ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0) ? ? ? ? ? ? } ? ? ? ? }).start() ? ? } //停止動畫 ? ? private fun progressReduce() { ? ? ? ? isAnimate = false ? ? }
控件源碼:
//java編寫 public class WaterRippleView extends View { ? ?? ? ? private float mWidth; ? ? private float mHeight; ? ? //總周期為2s ? ? private int CIRCLE_PERIOD = 2000; ? ? //振幅,波紋高度 ? ? private float ampltitude = 100; ? ? //填充顏色 ? ? private int paintColor = 0xff57c011; ? ? //當(dāng)前時間值,累加并循環(huán) ? ? private int period = 0; ? ? private Paint paint; ? ? private Path path; ? ? public WaterRippleView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public WaterRippleView(Context context, @Nullable AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public WaterRippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? paint = new Paint(); ? ? ? ? paint.setColor(paintColor); ? ? ? ? paint.setStyle(Paint.Style.FILL); ? ? ? ? paint.setAntiAlias(true); ? ? ? ? path = new Path(); ? ? } ? ? @Override ? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) { ? ? ? ? super.onSizeChanged(w, h, oldw, oldh); ? ? ? ? mWidth = w; ? ? ? ? mHeight = h; ? ? } ? ? public void refreshView() { ? ? ? ? period += 100; ? ? ? ? if (period > CIRCLE_PERIOD) period %= CIRCLE_PERIOD; ? ? ? ? invalidate(); ? ? } ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? //周期性的移動畫布 ? ? ? ? float offsetX = mWidth / CIRCLE_PERIOD * period; ? ? ? ? canvas.translate(-offsetX, 0); ? ? ? ? path.reset(); ? ? ? ? //第一個正弦曲線 ? ? ? ? path.moveTo(0, mHeight / 2); ? ? ? ? path.cubicTo(mWidth / 4, mHeight / 2 - ampltitude, ? ? ? ? ? ? ? ? mWidth * 3 / 4, mHeight / 2 + ampltitude, mWidth, mHeight / 2); ? ? ? ? //第二個正弦曲線 ? ? ? ? path.cubicTo(mWidth * 5 / 4, mHeight / 2 - ampltitude, ? ? ? ? ? ? ? ? mWidth * 7 / 4, mHeight / 2 + ampltitude, 2 * mWidth, mHeight / 2); ? ? ? ? //形成閉合路徑 ? ? ? ? path.lineTo(2 * mWidth, mHeight); ? ? ? ? path.lineTo(0, mHeight); ? ? ? ? path.lineTo(0, mHeight / 2); ? ? ? ? canvas.drawPath(path, paint); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android水波紋載入控件CircleWaterWaveView使用詳解
- android自定義WaveView水波紋控件
- Android自定義View控件實現(xiàn)多種水波紋漣漪擴散效果
- Android自定義WaveProgressView實現(xiàn)水波紋加載需求
- Android自定義View實現(xiàn)水波紋效果
- Android自定義View實現(xiàn)水波紋引導(dǎo)動畫
- Android 自定義view實現(xiàn)水波紋動畫效果
- Android自定義View 實現(xiàn)水波紋動畫引導(dǎo)效果
- Android自定義view實現(xiàn)水波紋進度球效果
- Android項目實戰(zhàn)手把手教你畫圓形水波紋loadingview
相關(guān)文章
Android基于AccessibilityService制作的釘釘自動簽到程序代碼
這篇文章主要介紹了Android基于AccessibilityService制作的釘釘自動簽到程序代碼,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05解決Android Studio4.1沒有Gsonfomat插件,Plugin “GsonFormat” is inco
這篇文章主要介紹了解決Android Studio4.1沒有Gsonfomat插件,Plugin “GsonFormat” is incompatible (supported only in IntelliJ IDEA)的問題 ,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12Kotlin字節(jié)碼層探究構(gòu)造函數(shù)與成員變量和init代碼塊執(zhí)行順序
這篇文章主要介紹了字節(jié)碼層Kotlin構(gòu)造函數(shù)與成員變量和init代碼塊執(zhí)行順序,kotlin里面的構(gòu)造函數(shù)分為主構(gòu)造函數(shù)和次構(gòu)造函數(shù)。主構(gòu)造函數(shù)只能有一個,次構(gòu)造函數(shù)個數(shù)不限制,可以有一個或者多個2022-11-11Android 中FloatingActionButton(懸浮按鈕)實例詳解
這篇文章主要介紹了Android 中FloatingActionButton(懸浮按鈕)實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 進度條顯示在標(biāo)題欄的實現(xiàn)方法
android進度條顯示在標(biāo)題欄的實現(xiàn)方法,大概分文xml文件和java文件,具體代碼內(nèi)容大家可以通過本文學(xué)習(xí)下2017-01-01Android之PreferenceActivity應(yīng)用詳解(2)
看到很多書中都沒有對PreferenceActivity做介紹,而我正好又在項目中用到,所以就把自己的使用的在這總結(jié)一下,也方便日后查找2012-11-11詳解Android中常見的內(nèi)存優(yōu)化及內(nèi)存泄露場景
本文主要給大家介紹了Android中常見的內(nèi)存優(yōu)化及Android開發(fā)中容易造成內(nèi)存泄露的場景,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08