Android自定義View實現(xiàn)水波紋擴散效果
效果:水波紋擴散
場景:雷達、按鈕點擊效果、搜索等
實現(xiàn):先上效果圖,之前記得支付寶有一個咻一咻,當時就是水波紋效果,實現(xiàn)起來一共兩步,第一畫內(nèi)圓,第二畫多個外圓,不同時創(chuàng)建有間隔創(chuàng)建然后緩慢增大外圓半徑,到達最遠距離時移除掉,擴散時把透明度從255-1不斷賦值即可。復(fù)雜在第二步,開工。

開工
1、創(chuàng)建RippleView.class, 繼承與View
RippleView主要初始化一些數(shù)據(jù),
onSizeChanged主要獲取位置坐標
onDraw主要繪制圖像,關(guān)鍵
public class RippleView extends View {
public RippleView(Context context) {
this(context, null);
}
public RippleView(Context context, @Nullable AttributeSet attrs) {
//this(context, null, 0);//如果第二個參數(shù)寫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數(shù)組:目的是讓每個外圓(擴散圓)透明度從不透明到透明(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<>();//擴散圓層級數(shù),元素為擴散的距離 private List<Integer> alphas = new ArrayList<>();//對應(yīng)每層圓的透明度
1.2新建attrs.xml文件(res/values)
我們需要在xml中使用自定義屬性來控制初始值,如內(nèi)圓半徑,擴散顏色,內(nèi)圓顏色等
<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();釋放掉,為什么釋放掉這也是一個學(xué)問,自行百度。
centerColor為內(nèi)圓顏色,
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);
} /**在控件大小發(fā)生改變時調(diào)用。所以這里初始化會被調(diào)用一次*/<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()
我們已經(jīng)做了好前奏,剩下的就開始繪制了,首先我們要確定幾個圓才能形成水波紋效果,1,2還是3,不確定那就先從一個開始,spreadRadius我們在創(chuàng)建畫筆時已經(jīng)添加了一個圓,那我們就遍歷spreadRadius數(shù)組,透明度alphas[i]把值遞減(255-1),spreadRadius[i]圓半徑遞增,圓數(shù)量超過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);
}到此這篇關(guān)于Android自定義View實現(xiàn)水波紋擴散效果的文章就介紹到這了,更多相關(guān)Android水波紋擴散內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲
這篇文章主要介紹了Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲的相關(guān)資料,數(shù)據(jù)存儲是Android開發(fā)的重要的知識,這里提供了實例,需要的朋友可以參考下2017-07-07
Android App啟動圖啟動界面(Splash)的簡單實現(xiàn)代碼
這篇文章主要介紹了Android App啟動圖啟動界面(Splash)的簡單實現(xiàn)代碼,本文通過實例圖文詳解相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android實現(xiàn)畫板、寫字板功能(附源碼下載)
這篇文章主要介紹了Android實現(xiàn)畫板、寫字板功能的方法,文中給出了簡單的介紹和示例代碼,想要了解更多的朋友可以下載源碼進行學(xué)習(xí),感興趣的朋友們下面來一起看看吧。2017-01-01

