Android自定義View實(shí)現(xiàn)簡(jiǎn)單水波紋效果
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)水波紋效果的具體代碼,供大家參考,具體內(nèi)容如下
效果如下:

原理

控制代碼
//這里用的kotlin
//主線程刷新控件
?val mHandler = object : Handler() {
? ? ? ? override fun handleMessage(msg: Message?) {
? ? ? ? ? ? waterRippleView.refreshView()
? ? ? ? }
? ??
//開(kāi)啟動(dòng)畫(huà),開(kāi)線程,延時(shí)刷新period值,畫(huà)布進(jìn)行x方向平移
private fun progressAdd() {
? ? ? ? isAnimate = true
? ? ? ? Thread(Runnable {
? ? ? ? ? ? while (isAnimate) {
? ? ? ? ? ? ? ? Thread.sleep(100)
? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0)
? ? ? ? ? ? }
? ? ? ? }).start()
? ? }
//停止動(dòng)畫(huà)
? ? private fun progressReduce() {
? ? ? ? isAnimate = false
? ? }控件源碼:
//java編寫(xiě)
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)前時(shí)間值,累加并循環(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);
? ? ? ? //周期性的移動(dòng)畫(huà)布
? ? ? ? float offsetX = mWidth / CIRCLE_PERIOD * period;
? ? ? ? canvas.translate(-offsetX, 0);
? ? ? ? path.reset();
? ? ? ? //第一個(gè)正弦曲線
? ? ? ? path.moveTo(0, mHeight / 2);
? ? ? ? path.cubicTo(mWidth / 4, mHeight / 2 - ampltitude,
? ? ? ? ? ? ? ? mWidth * 3 / 4, mHeight / 2 + ampltitude, mWidth, mHeight / 2);
? ? ? ? //第二個(gè)正弦曲線
? ? ? ? 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);
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android水波紋載入控件CircleWaterWaveView使用詳解
- android自定義WaveView水波紋控件
- Android自定義View控件實(shí)現(xiàn)多種水波紋漣漪擴(kuò)散效果
- Android自定義WaveProgressView實(shí)現(xiàn)水波紋加載需求
- Android自定義View實(shí)現(xiàn)水波紋效果
- Android自定義View實(shí)現(xiàn)水波紋引導(dǎo)動(dòng)畫(huà)
- Android 自定義view實(shí)現(xiàn)水波紋動(dòng)畫(huà)效果
- Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)效果
- Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
- Android項(xiàng)目實(shí)戰(zhàn)手把手教你畫(huà)圓形水波紋loadingview
相關(guān)文章
Android基于AccessibilityService制作的釘釘自動(dòng)簽到程序代碼
這篇文章主要介紹了Android基于AccessibilityService制作的釘釘自動(dòng)簽到程序代碼,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
解決Android Studio4.1沒(méi)有Gsonfomat插件,Plugin “GsonFormat” is inco
這篇文章主要介紹了解決Android Studio4.1沒(méi)有Gsonfomat插件,Plugin “GsonFormat” is incompatible (supported only in IntelliJ IDEA)的問(wèn)題 ,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12
一分鐘快速定位Android啟動(dòng)耗時(shí)問(wèn)題
做開(kāi)發(fā)除了實(shí)現(xiàn)功能,還要注重優(yōu)化,性能優(yōu)化包括的東西還是非常多的,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一分鐘快速定位Android啟動(dòng)耗時(shí)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2021-07-07
Kotlin字節(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è),次構(gòu)造函數(shù)個(gè)數(shù)不限制,可以有一個(gè)或者多個(gè)2022-11-11
Android 中FloatingActionButton(懸浮按鈕)實(shí)例詳解
這篇文章主要介紹了Android 中FloatingActionButton(懸浮按鈕)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android 進(jìn)度條顯示在標(biāo)題欄的實(shí)現(xiàn)方法
android進(jìn)度條顯示在標(biāo)題欄的實(shí)現(xiàn)方法,大概分文xml文件和java文件,具體代碼內(nèi)容大家可以通過(guò)本文學(xué)習(xí)下2017-01-01
安卓(Android)實(shí)現(xiàn)選擇時(shí)間功能
安卓開(kāi)發(fā)過(guò)程中難免會(huì)碰到需要選擇日期時(shí)間的情況,當(dāng)然不可能讓用戶自己輸入日期時(shí)間,小編收集整理了一些資料,總結(jié)了一下如何實(shí)現(xiàn)android選擇時(shí)間的功能,方便后來(lái)者參考2016-08-08
Android之PreferenceActivity應(yīng)用詳解(2)
看到很多書(shū)中都沒(méi)有對(duì)PreferenceActivity做介紹,而我正好又在項(xiàng)目中用到,所以就把自己的使用的在這總結(jié)一下,也方便日后查找2012-11-11
詳解Android中常見(jiàn)的內(nèi)存優(yōu)化及內(nèi)存泄露場(chǎng)景
本文主要給大家介紹了Android中常見(jiàn)的內(nèi)存優(yōu)化及Android開(kāi)發(fā)中容易造成內(nèi)存泄露的場(chǎng)景,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08

