本文執行個體為大家分享了Android自定義View的實作水波紋,供大家參考,具體内容如下
一、實作效果
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SZiJGO5MmMzMmYyQGOmZjZkJ2NyEWOzUmN1UTMkVWM58CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
MainActivity.xml
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@+id/diffuseView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:diffuse_color="@color/colorAccent"
app:diffuse_coreColor="@color/colorPrimaryDark"
app:diffuse_coreImage="@android:drawable/ic_menu_search"
app:diffuse_coreRadius="100"
app:diffuse_maxWidth="300"
app:diffuse_speed="5"
app:diffuse_width="4"/>
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始擴散"/>
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="停止擴散"/>
MainActivity中的點選事件
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button2;
private DiffuseView diffuseView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
diffuseView = findViewById(R.id.diffuseView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
diffuseView.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
diffuseView.stop();
}
});
}
}
自定義view類
public class DiffuseView extends View {
private int mColor = getResources().getColor(R.color.colorAccent);
private int mCoreColor = getResources().getColor(R.color.colorPrimary);
private float mCoreRadius = 150;
private int mDiffuseWidth = 3;
private Integer mMaxWidth = 255;
private int mDiffuseSpeed = 5;
private boolean mIsDiffuse = false;
// 透明度集合
private List mAlphas = new ArrayList<>();
// 擴散圓半徑集合
private List mWidths = new ArrayList<>();
private Paint mPaint;
public DiffuseView(Context context) {
this(context, null);
}
public DiffuseView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public DiffuseView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DiffuseView, defStyleAttr, 0);
mColor = a.getColor(R.styleable.DiffuseView_diffuse_color, mColor);
mCoreColor = a.getColor(R.styleable.DiffuseView_diffuse_coreColor, mCoreColor);
mCoreRadius = a.getFloat(R.styleable.DiffuseView_diffuse_coreRadius, mCoreRadius);
mDiffuseWidth = a.getInt(R.styleable.DiffuseView_diffuse_width, mDiffuseWidth);
mMaxWidth = a.getInt(R.styleable.DiffuseView_diffuse_maxWidth, mMaxWidth);
mDiffuseSpeed = a.getInt(R.styleable.DiffuseView_diffuse_speed, mDiffuseSpeed);
a.recycle();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mAlphas.add(255);
mWidths.add(0);
}
@Override
public void invalidate() {
if(hasWindowFocus()){
super.invalidate();
}
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if(hasWindowFocus){
invalidate();
}
}
@Override
public void onDraw(Canvas canvas) {
// 繪制擴散圓
mPaint.setColor(mColor);
for (int i = 0; i < mAlphas.size(); i ++) {
// 設定透明度
Integer alpha = mAlphas.get(i);
mPaint.setAlpha(alpha);
// 繪制擴散圓
Integer width = mWidths.get(i);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mCoreRadius + width, mPaint);
if(alpha > 0 && width < mMaxWidth){
mAlphas.set(i, alpha - mDiffuseSpeed > 0 ? alpha - mDiffuseSpeed : 1);
mWidths.set(i, width + mDiffuseSpeed);
}
}
// 判斷當擴散圓擴散到指定寬度時添加新擴散圓
if (mWidths.get(mWidths.size() - 1) >= mMaxWidth / mDiffuseWidth) {
mAlphas.add(255);
mWidths.add(0);
}
// 超過10個擴散圓,删除最外層
if(mWidths.size() >= 10){
mWidths.remove(0);
mAlphas.remove(0);
}
// 繪制中心圓
mPaint.setAlpha(255);
mPaint.setColor(mCoreColor);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mCoreRadius, mPaint);
if(mIsDiffuse){
invalidate();
}
}
public void start() {
mIsDiffuse = true;
invalidate();
}
public void stop() {
mIsDiffuse = false;
mWidths.clear();
mAlphas.clear();
mAlphas.add(255);
mWidths.add(0);
invalidate();
}
public boolean isDiffuse(){
return mIsDiffuse;
}
public void setColor(int colorId){
mColor = colorId;
}
public void setCoreColor(int colorId){
mCoreColor = colorId;
}
public void setCoreRadius(int radius){
mCoreRadius = radius;
}
public void setDiffuseWidth(int width){
mDiffuseWidth = width;
}
public void setMaxWidth(int maxWidth){
mMaxWidth = maxWidth;
}
public void setDiffuseSpeed(int speed){
mDiffuseSpeed = speed;
}
}
自己添加的attrs.xml(建立在Values包底下,切勿倒錯)
這樣就搞定了。
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援腳本之家。