網站首頁 編程語言 正文
效果:水波紋擴散
場景:雷達、按鈕點擊效果、搜索等
實現:先上效果圖,之前記得支付寶有一個咻一咻,當時就是水波紋效果,實現起來一共兩步,第一畫內圓,第二畫多個外圓,不同時創建有間隔創建然后緩慢增大外圓半徑,到達最遠距離時移除掉,擴散時把透明度從255-1不斷賦值即可。復雜在第二步,開工。
開工
1、創建RippleView.class, 繼承與View
RippleView主要初始化一些數據,
onSizeChanged主要獲取位置坐標
onDraw主要繪制圖像,關鍵
public class RippleView extends View {
public RippleView(Context context) {
this(context, null);
}
public RippleView(Context context, @Nullable AttributeSet attrs) {
//this(context, null, 0);//如果第二個參數寫null,則自定義屬性將不可用
this(context, attrs, 0);
}
public RippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
........
} <br>
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
........
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
........
}
}
1.1特殊屬性解釋?
alpha數組:目的是讓每個外圓(擴散圓)透明度從不透明到透明(255-1)
spreadRadius:擴散圓的半徑是遞增的
private Paint centerPaint; //中心圓paint
private int radius = 100; //中心圓半徑
private Paint spreadPaint; //擴散圓paint
private float centerX;//圓心x
private float centerY;//圓心y
private int distance = 5; //每次圓遞增間距
private int maxRadius = 80; //最大圓半徑
private int delayMilliseconds = 30;//擴散延遲間隔,越大擴散越慢
private List<Integer> spreadRadius = new ArrayList<>();//擴散圓層級數,元素為擴散的距離
private List<Integer> alphas = new ArrayList<>();//對應每層圓的透明度
1.2新建attrs.xml文件(res/values)
我們需要在xml中使用自定義屬性來控制初始值,如內圓半徑,擴散顏色,內圓顏色等
<resources> <declare-styleable name="SpreadView"> <!--中心圓顏色--> <attr name="spread_center_color" format="color" /> <!--中心圓半徑--> <attr name="spread_radius" format="integer" /> <!--擴散圓顏色--> <attr name="spread_spread_color" format="color" /> <!--擴散間距--> <attr name="spread_distance" format="integer" /> <!--擴散最大半徑--> <attr name="spread_max_radius" format="integer" /> <!--擴散延遲間隔--> <attr name="spread_delay_milliseconds" format="integer" /> </declare-styleable> </resources>
在RippleView中拿到值
public RippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0);
radius = typedArray.getInt(R.styleable.SpreadView_spread_radius, radius);
maxRadius = typedArray.getInt(R.styleable.SpreadView_spread_max_radius, maxRadius);
int centerColor = typedArray.getColor(R.styleable.SpreadView_spread_center_color,
ContextCompat.getColor(context, android.R.color.holo_red_light));
int spreadColor = typedArray.getColor(R.styleable.SpreadView_spread_spread_color,
ContextCompat.getColor(context, R.color.color_F71816));
distance = typedArray.getInt(R.styleable.SpreadView_spread_distance, distance);
typedArray.recycle();
}
context.obtainStyledAttributes可以獲取我們在xml文件的屬性值,最后typedArray.recycle();釋放掉,為什么釋放掉這也是一個學問,自行百度。
centerColor為內圓顏色,
spreadColor擴散顏色
1.3初始化畫筆
public RippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
.....
//中心圓
centerPaint = new Paint();
centerPaint.setColor(centerColor);<br> //消除鋸齒
centerPaint.setAntiAlias(true);
//水波紋擴散
spreadPaint = new Paint();
spreadPaint.setColor(spreadColor);
spreadPaint.setAntiAlias(true);<br> //填充和描邊,上面2張圖片效果不同取決于該屬性
spreadPaint.setStyle(Paint.Style.STROKE);
spreadPaint.setAlpha(255);
//初始化第一個水波紋,擴散半徑為0,透明度為255(不透明)
spreadRadius.add(0);
alphas.add(255);
} /**在控件大小發生改變時調用。所以這里初始化會被調用一次*/<br>@Override<br>protected void onSizeChanged(int w, int h, int oldw, int oldh) {<br> super.onSizeChanged(w, h, oldw, oldh);<br> //圓心位置<br> centerX = w / 2;<br> centerY = h / 2;<br> }
2、開始繪制onDraw()
我們已經做了好前奏,剩下的就開始繪制了,首先我們要確定幾個圓才能形成水波紋效果,1,2還是3,不確定那就先從一個開始,spreadRadius我們在創建畫筆時已經添加了一個圓,那我們就遍歷spreadRadius數組,透明度alphas[i]把值遞減(255-1),spreadRadius[i]圓半徑遞增,圓數量超過8個就移除第1個,如果最外圓擴散半徑達到最大半徑時添加新擴散圓。最后通過postInvalidateDelayed(30),延遲刷新來達到擴散的樣式。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < spreadRadius.size(); i++) {
//透明度
int alpha = alphas.get(i);
//半徑
int width = spreadRadius.get(i);
spreadPaint.setAlpha(alpha);
//繪制擴散的圓
canvas.drawCircle(centerX, centerY, radius + width, spreadPaint);
if (alpha > 0 && width < 255) {
//遞減
alpha = (alpha - distance) > 0 ? (alpha - distance) : 1;
alphas.set(i, alpha);
//遞增
spreadRadius.set(i, width + distance);
}
}
if (spreadRadius.get(spreadRadius.size() - 1) > maxRadius) {
spreadRadius.add(0);
alphas.add(255);
}
if (spreadRadius.size() > 8) {
spreadRadius.remove(0);
alphas.remove(0);
}
//中間的圓
canvas.drawCircle(centerX, centerY, radius, centerPaint);
//延遲更新,達到擴散視覺差效果
postInvalidateDelayed(delayMilliseconds);
}
原文鏈接:https://www.cnblogs.com/cmusketeer/p/16628367.html
相關推薦
- 2022-07-14 C++實現一個簡單的線程池的示例代碼_C 語言
- 2022-07-22 jQuery實現點擊顯示密碼框密碼
- 2022-02-20 給定一個數組,讓數組的每一項都乘以2幾種實現方法
- 2022-12-09 CALL命令無法在PowerShell中使用的原因及解決方法_DOS/BAT
- 2022-09-25 Identity Server4/生產模式/證書/certificate/AddSigningCre
- 2022-12-07 C++中new的用法及說明_C 語言
- 2023-07-09 .Net下驗證MongoDB 的 Linq 模式聯合查詢是否可用
- 2023-06-17 C或C++報錯:ld?returned?1?exit?status報錯的原因及解決方法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支