Android自定义控件实现水波纹效果
程序员文章站
2022-03-23 13:39:25
本文实例为大家分享了android自定义控件实现水波纹的具体代码,供大家参考,具体内容如下
示例代码:
mainactivity.java
package c...
本文实例为大家分享了android自定义控件实现水波纹的具体代码,供大家参考,具体内容如下
示例代码:
mainactivity.java
package com.example.mhy.shuibowen; import android.support.v7.app.appcompatactivity; import android.os.bundle; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mhy.shuibowen.mainactivity"> <com.example.mhy.shuibowen.myringwave android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
myringwave.java
package com.example.mhy.shuibowen; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.os.handler; import android.util.attributeset; import android.view.motionevent; import android.view.view; import java.util.arraylist; /** * 水波纹效果 * created by mhy on 2016/6/16. */ public class myringwave extends view { /** * 二个相临波浪中心点的最小距离 */ private static final int dis_solp = 13; protected boolean isrunning = false; private arraylist<wave> wlist; public myringwave(context context,attributeset attrs) { super(context, attrs); wlist = new arraylist<myringwave.wave>(); } @override protected void ondraw(canvas canvas) { for(int i=0; i<wlist.size(); i++) { wave wave = wlist.get(i); canvas.drawcircle(wave.cx, wave.cy, wave.r, wave.p); } } @override public boolean ontouchevent(motionevent event) { super.ontouchevent(event); switch(event.getaction()) { case motionevent.action_down: case motionevent.action_move: int x = (int) event.getx(); int y = (int) event.gety(); addpoint(x, y); break; default: break; } return true; } private handler handler = new handler(){ public void handlemessage(android.os.message msg) { //刷新数据 flushdata(); //刷新页面 invalidate(); //循环动画 if (isrunning) { handler.sendemptymessagedelayed(0, 50); } } }; /** * 刷新数据 */ private void flushdata() { for (int i = 0; i < wlist.size(); i++) { wave w = wlist.get(i); //如果透明度为 0 从集合中删除 int alpha = w.p.getalpha(); if(alpha == 0){ wlist.remove(i); //删除i 以后,i的值应该再减1 否则会漏掉一个对象,不过,在此处影响不大,效果上看不出来。 continue; } alpha-=5; if(alpha<5){ alpha =0; } //降低透明度 w.p.setalpha(alpha); //扩大半径 w.r = w.r+3; //设置半径厚度 w.p.setstrokewidth(w.r/3); } /* * 如果集合被清空,就停止刷新动画 */ if(wlist.size() == 0){ isrunning = false; } } /** * 添加新的波浪中心点 * @param x * @param y */ private void addpoint(int x, int y) { if(wlist.size() == 0) { addpoint2list(x, y); isrunning = true; handler.sendemptymessage(0); }else{ wave w = wlist.get(wlist.size()-1); if(math.abs(w.cx - x)>dis_solp || math.abs(w.cy-y)>dis_solp){ addpoint2list(x,y); } }; } /** * 添加新的波浪 * @param x * @param y */ private void addpoint2list(int x, int y) { wave w = new wave(); w.cx = x; w.cy=y; paint pa=new paint(); pa.setcolor(colors[(int)(math.random()*4)]); pa.setantialias(true); pa.setstyle(paint.style.stroke); w.p = pa; wlist.add(w); } private int [] colors = new int[]{color.blue,color.red,color.yellow,color.green}; private class wave { //圆心 int cx; int cy; //画笔 paint p; //半径 int r; } }
myring.java
package com.example.mhy.shuibowen; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.os.handler; import android.os.message; import android.util.attributeset; import android.view.motionevent; import android.view.view; /** * created by mhy on 2016/6/16. */ public class myring extends view { /** * 圆心的x坐标 */ private float cx; /** * 圆心的y坐标 */ private float cy; /** * 圆环半径 */ private float radius = 0; /** * 默认画笔 */ private paint paint; private boolean isruning = false; public myring(context context, attributeset attrs) { super(context, attrs); initview(); } private void initview() { radius = 0; paint = new paint(); paint.setantialias(true); paint.setstyle(paint.style.stroke); // 空心圆 paint.setstrokewidth(radius / 4); // 画笔宽度 半径4分之一 paint.setcolor(color.green); // 画笔颜色 paint.setalpha(255); //不透明 } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { if(changed) { cx = getwidth() / 2; cy = getheight() / 2; } } @override protected void ondraw(canvas canvas) { canvas.drawcircle(cx, cy, radius, paint); } @override protected void onattachedtowindow() { super.onattachedtowindow(); } @override protected void ondetachedfromwindow() { super.ondetachedfromwindow(); isruning = false; } @override public boolean ontouchevent(motionevent event) { super.ontouchevent(event); if(event.getaction() == motionevent.action_down) { cx = event.getx(); cy = event.gety(); initview(); startanim(); } return true; } private handler handler = new handler(){ @override public void handlemessage(message msg) { // 设置透明度 int alpha = paint.getalpha(); if(alpha == 0) { isruning = false; } // 透明度 慢慢变透明 alpha = math.max(0, alpha-10); paint.setalpha(alpha); system.out.println(alpha); // 设置半径 radius += 5; paint.setstrokewidth(radius / 3); invalidate(); if(isruning) { handler.sendemptymessagedelayed(0, 50); } } }; private void startanim() { isruning = true; handler.sendemptymessagedelayed(0, 50); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Vue.js 源码分析(二十七) 高级应用 异步组件 详解
下一篇: Oracle自定义脱敏函数