日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义控件:水波纹

發(fā)布時間:2025/4/16 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义控件:水波纹 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

效果圖

實現(xiàn)代碼

public class RingWave extends View {private List<Wave> mWaveList = new ArrayList<>();private int[] colors = new int[]{Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};private Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {flushData();invalidate();if (!mWaveList.isEmpty()){mHandler.sendEmptyMessageDelayed(0,50);}}};private void flushData() {ArrayList<Wave> removeList = new ArrayList<>();for (Wave wave : mWaveList){wave.radius += 3;wave.paint.setStrokeWidth(wave.radius/3);if (wave.paint.getAlpha() < 0){removeList.add(wave);continue;}int alpha = wave.paint.getAlpha();alpha -= 5;if (alpha < 0) alpha = 0;wave.paint.setAlpha(alpha);}mWaveList.removeAll(removeList);}public RingWave(Context context) {this(context, null);}public RingWave(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RingWave(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {for (Wave wave : mWaveList){canvas.drawCircle(wave.cx, wave.cy, wave.radius, wave.paint);}}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (MotionEventCompat.getActionMasked(event)){case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:addPoint((int)event.getX(), (int)event.getY());break;}return true;}private void addPoint(int x, int y) {if (mWaveList.isEmpty()){addWave(x, y);mHandler.sendEmptyMessageDelayed(0,50);}else {Wave lastWave = mWaveList.get(mWaveList.size() - 1);if (Math.abs(x - lastWave.cx) > 10 || Math.abs(y - lastWave.cy) > 10){addWave(x, y);}}}private void addWave(int x, int y) {Wave wave = new Wave();wave.cx = x;wave.cy = y;Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(wave.radius/3);paint.setAlpha(255);paint.setAntiAlias(true);int colorIndex = (int) (Math.random()*4);paint.setColor(colors[colorIndex]);wave.paint = paint;mWaveList.add(wave);}private class Wave{public int cx;public int cy;public int radius;public Paint paint;} }

布局文件

<?xml version="1.0" encoding="utf-8"?> <com.google.widget.view.RingWave xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"></com.google.widget.view.RingWave> public class RingWaveActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_wave);SpannableString title = new SpannableString("水波紋");title.setSpan(new ForegroundColorSpan(Color.WHITE),0,title.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);ActionBar actionBar = getSupportActionBar();actionBar.setTitle(title);} }

總結(jié)

以上是生活随笔為你收集整理的自定义控件:水波纹的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。