欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现水波纹效果

程序员文章站 2024-03-04 09:07:23
一、效果   点击开始:   点击停止: 二、在mainactivity中 import android....

一、效果

Android实现水波纹效果 

点击开始:

Android实现水波纹效果 

点击停止:

Android实现水波纹效果

二、在mainactivity中

import android.graphics.paint;
import android.os.bundle;
import android.support.v4.view.animation.linearoutslowininterpolator;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.button;
public class mainactivity extends appcompatactivity implements view.onclicklistener {
// private waveview mwaveview1;
private waveview mwaveview2;
private button mstart;
private button mstop;
private circleimageview circleimageview;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mstart = (button) findviewbyid(r.id.start);
mstop = (button) findviewbyid(r.id.stop);
circleimageview = (circleimageview) findviewbyid(r.id.civ_info);
circleimageview.setimageresource(r.mipmap.icon_ku);
mwaveview2 = (waveview) findviewbyid(r.id.wave_view2);
mwaveview2.setduration(5000);
mwaveview2.setstyle(paint.style.fill_and_stroke);
mwaveview2.setcolor(getresources().getcolor(r.color.green));
mwaveview2.setinterpolator(new linearoutslowininterpolator());
mstart.setonclicklistener(this);
mstop.setonclicklistener(this);
}
@override
public void onclick(view v) {
switch (v.getid()) {
case r.id.start:
mwaveview2.setcolor(getresources().getcolor(r.color.green));
mwaveview2.start();
circleimageview.setimageresource(r.mipmap.icon_xiao);
break;
case r.id.stop:
mwaveview2.setcolor(getresources().getcolor(r.color.red));
mwaveview2.stop();
circleimageview.setimageresource(r.mipmap.icon_ku);
break;
}
}
}

三、在activity_main中

<?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:id="@+id/activity_main"
android:layout_width="match_parent" 
android:layout_height="match_parent"
>
<button
android:id="@+id/start"
android:layout_margintop="10dp"
android:layout_marginleft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
<button
android:id="@+id/stop"
android:layout_torightof="@id/start"
android:layout_margintop="10dp"
android:layout_marginleft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
<cn.hnshangyu.xiuyixiu.waveview
android:id="@+id/wave_view2"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_centerinparent="true"
android:layout_margintop="10dp"
android:textsize="24dp" />
<cn.hnshangyu.xiuyixiu.circleimageview
android:id="@+id/civ_info"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerinparent="true"
android:gravity="center"
android:src="@color/green"
android:text="@string/app_name"
android:textcolor="@color/colorprimary" />

四、在waveview中:

import android.content.context;
import android.graphics.canvas;
import android.graphics.paint;
import android.util.attributeset;
import android.view.view;
import android.view.animation.interpolator;
import android.view.animation.linearinterpolator;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
/**
* 水波纹特效
*/
public class waveview extends view {
private float minitialradius=100; // 初始波纹半径
private float mmaxradiusrate = 0.85f; // 如果没有设置mmaxradius,可mmaxradius = 最小长度 * mmaxradiusrate;
private float mmaxradius; // 最大波纹半径
private long mduration = 2000; // 一个波纹从创建到消失的持续时间
private int mspeed = 2000; // 波纹的创建速度,每2000ms创建一个
private interpolator minterpolator = new linearinterpolator();
private list<circle> mcirclelist = new arraylist<circle>();
private boolean misrunning;
private boolean mmaxradiusset;
private paint mpaint;
private long mlastcreatetime;
private runnable mcreatecircle = new runnable() {
@override
public void run() {
if (misrunning) {
newcircle();
postdelayed(mcreatecircle, mspeed);
}
}
};
public waveview(context context) {
this(context, null);
}
public waveview(context context, attributeset attrs) {
super(context, attrs);
mpaint = new paint(paint.anti_alias_flag);
setstyle(paint.style.fill);
}
public void setstyle(paint.style style) {
mpaint.setstyle(style);
}
@override
protected void onsizechanged(int w, int h, int oldw, int oldh) {
if (!mmaxradiusset) {
mmaxradius = math.min(w, h) * mmaxradiusrate / 2.0f;
}
}
public void setmaxradiusrate(float maxradiusrate) {
this.mmaxradiusrate = maxradiusrate;
}
public void setcolor(int color) {
mpaint.setcolor(color);
}
/**
* 开始
*/
public void start() {
if (!misrunning) {
misrunning = true;
mcreatecircle.run();
}
}
/**
* 停止
*/
public void stop() {
misrunning = false;
}
protected void ondraw(canvas canvas) {
iterator<circle> iterator = mcirclelist.iterator();
while (iterator.hasnext()) {
circle circle = iterator.next();
if (system.currenttimemillis() - circle.mcreatetime < mduration) {
mpaint.setalpha(circle.getalpha());
canvas.drawcircle(getwidth() / 2, getheight() / 2, circle.getcurrentradius(), mpaint);
} else {
iterator.remove();
}
}
if (mcirclelist.size() > 0) {
postinvalidatedelayed(10);
}
}
public void setinitialradius(float radius) {
minitialradius = radius;
}
public void setduration(long duration) {
this.mduration = duration;
}
public void setmaxradius(float maxradius) {
this.mmaxradius = maxradius;
mmaxradiusset = true;
}
public void setspeed(int speed) {
mspeed = speed;
}
private void newcircle() {
long currenttime = system.currenttimemillis();
if (currenttime - mlastcreatetime < mspeed) {
return;
}
circle circle = new circle();
mcirclelist.add(circle);
invalidate();
mlastcreatetime = currenttime;
}
private class circle {
private long mcreatetime;
public circle() {
this.mcreatetime = system.currenttimemillis();
}
public int getalpha() {
float percent = (system.currenttimemillis() - mcreatetime) * 1.0f / mduration;
return (int) ((1.0f - minterpolator.getinterpolation(percent)) * 255);
}
public float getcurrentradius() {
float percent = (system.currenttimemillis() - mcreatetime) * 1.0f / mduration;
return minitialradius + minterpolator.getinterpolation(percent) * (mmaxradius - minitialradius);
}
}
public void setinterpolator(interpolator interpolator) {
minterpolator = interpolator;
if (minterpolator == null) {
minterpolator = new linearinterpolator();
}
}
}

五、在circleimageview中:

import android.content.context;
import android.content.res.typedarray;
import android.graphics.bitmap;
import android.graphics.bitmapshader;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.colorfilter;
import android.graphics.matrix;
import android.graphics.paint;
import android.graphics.rectf;
import android.graphics.shader;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.colordrawable;
import android.graphics.drawable.drawable;
import android.net.uri;
import android.support.annotation.colorint;
import android.support.annotation.colorres;
import android.support.annotation.drawableres;
import android.util.attributeset;
import android.widget.imageview;
public class circleimageview extends imageview {
private static final scaletype scale_type = scaletype.center_crop;
private static final bitmap.config bitmap_config = bitmap.config.argb_8888;
private static final int colordrawable_dimension = 2;
private static final int default_border_width = 0;
private static final int default_border_color = color.black;
private static final int default_fill_color = color.transparent;
private static final boolean default_border_overlay = false;
private final rectf mdrawablerect = new rectf();
private final rectf mborderrect = new rectf();
private final matrix mshadermatrix = new matrix();
private final paint mbitmappaint = new paint();
private final paint mborderpaint = new paint();
private final paint mfillpaint = new paint();
private int mbordercolor = default_border_color;
private int mborderwidth = default_border_width;
private int mfillcolor = default_fill_color;
private bitmap mbitmap;
private bitmapshader mbitmapshader;
private int mbitmapwidth;
private int mbitmapheight;
private float mdrawableradius;
private float mborderradius;
private colorfilter mcolorfilter;
private boolean mready;
private boolean msetuppending;
private boolean mborderoverlay;
private boolean mdisablecirculartransformation;
public circleimageview(context context) {
super(context);
init();
}
public circleimageview(context context, attributeset attrs) {
this(context, attrs, 0);
}
public circleimageview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
typedarray a = context.obtainstyledattributes(attrs, r.styleable.circleimageview, defstyle, 0);
mborderwidth = a.getdimensionpixelsize(r.styleable.circleimageview_civ_border_width, default_border_width);
mbordercolor = a.getcolor(r.styleable.circleimageview_civ_border_color, default_border_color);
mborderoverlay = a.getboolean(r.styleable.circleimageview_civ_border_overlay, default_border_overlay);
mfillcolor = a.getcolor(r.styleable.circleimageview_civ_fill_color, default_fill_color);
a.recycle();
init();
}
private void init() {
super.setscaletype(scale_type);
mready = true;
if (msetuppending) {
setup();
msetuppending = false;
}
}
@override
public scaletype getscaletype() {
return scale_type;
}
@override
public void setscaletype(scaletype scaletype) {
if (scaletype != scale_type) {
throw new illegalargumentexception(string.format("scaletype %s not supported.", scaletype));
}
}
@override
public void setadjustviewbounds(boolean adjustviewbounds) {
if (adjustviewbounds) {
throw new illegalargumentexception("adjustviewbounds not supported.");
}
}
@override
protected void ondraw(canvas canvas) {
if (mdisablecirculartransformation) {
super.ondraw(canvas);
return;
}
if (mbitmap == null) {
return;
}
if (mfillcolor != color.transparent) {
canvas.drawcircle(mdrawablerect.centerx(), mdrawablerect.centery(), mdrawableradius, mfillpaint);
}
canvas.drawcircle(mdrawablerect.centerx(), mdrawablerect.centery(), mdrawableradius, mbitmappaint);
if (mborderwidth > 0) {
canvas.drawcircle(mborderrect.centerx(), mborderrect.centery(), mborderradius, mborderpaint);
}
}
@override
protected void onsizechanged(int w, int h, int oldw, int oldh) {
super.onsizechanged(w, h, oldw, oldh);
setup();
}
@override
public void setpadding(int left, int top, int right, int bottom) {
super.setpadding(left, top, right, bottom);
setup();
}
@override
public void setpaddingrelative(int start, int top, int end, int bottom) {
super.setpaddingrelative(start, top, end, bottom);
setup();
}
public int getbordercolor() {
return mbordercolor;
}
public void setbordercolor(@colorint int bordercolor) {
if (bordercolor == mbordercolor) {
return;
}
mbordercolor = bordercolor;
mborderpaint.setcolor(mbordercolor);
invalidate();
}
/**
* @deprecated use {@link #setbordercolor(int)} instead
*/
@deprecated
public void setbordercolorresource(@colorres int bordercolorres) {
setbordercolor(getcontext().getresources().getcolor(bordercolorres));
}
/**
* return the color drawn behind the circle-shaped drawable.
*
* @return the color drawn behind the drawable
* @deprecated fill color support is going to be removed in the future
*/
@deprecated
public int getfillcolor() {
return mfillcolor;
}
/**
* set a color to be drawn behind the circle-shaped drawable. note that
* this has no effect if the drawable is opaque or no drawable is set.
*
* @param fillcolor the color to be drawn behind the drawable
* @deprecated fill color support is going to be removed in the future
*/
@deprecated
public void setfillcolor(@colorint int fillcolor) {
if (fillcolor == mfillcolor) {
return;
}
mfillcolor = fillcolor;
mfillpaint.setcolor(fillcolor);
invalidate();
}
/**
* set a color to be drawn behind the circle-shaped drawable. note that
* this has no effect if the drawable is opaque or no drawable is set.
*
* @param fillcolorres the color resource to be resolved to a color and
* drawn behind the drawable
* @deprecated fill color support is going to be removed in the future
*/
@deprecated
public void setfillcolorresource(@colorres int fillcolorres) {
setfillcolor(getcontext().getresources().getcolor(fillcolorres));
}
public int getborderwidth() {
return mborderwidth;
}
public void setborderwidth(int borderwidth) {
if (borderwidth == mborderwidth) {
return;
}
mborderwidth = borderwidth;
setup();
}
public boolean isborderoverlay() {
return mborderoverlay;
}
public void setborderoverlay(boolean borderoverlay) {
if (borderoverlay == mborderoverlay) {
return;
}
mborderoverlay = borderoverlay;
setup();
}
public boolean isdisablecirculartransformation() {
return mdisablecirculartransformation;
}
public void setdisablecirculartransformation(boolean disablecirculartransformation) {
if (mdisablecirculartransformation == disablecirculartransformation) {
return;
}
mdisablecirculartransformation = disablecirculartransformation;
initializebitmap();
}
@override
public void setimagebitmap(bitmap bm) {
super.setimagebitmap(bm);
initializebitmap();
}
@override
public void setimagedrawable(drawable drawable) {
super.setimagedrawable(drawable);
initializebitmap();
}
@override
public void setimageresource(@drawableres int resid) {
super.setimageresource(resid);
initializebitmap();
}
@override
public void setimageuri(uri uri) {
super.setimageuri(uri);
initializebitmap();
}
@override
public void setcolorfilter(colorfilter cf) {
if (cf == mcolorfilter) {
return;
}
mcolorfilter = cf;
applycolorfilter();
invalidate();
}
@override
public colorfilter getcolorfilter() {
return mcolorfilter;
}
private void applycolorfilter() {
if (mbitmappaint != null) {
mbitmappaint.setcolorfilter(mcolorfilter);
}
}
private bitmap getbitmapfromdrawable(drawable drawable) {
if (drawable == null) {
return null;
}
if (drawable instanceof bitmapdrawable) {
return ((bitmapdrawable) drawable).getbitmap();
}
try {
bitmap bitmap;
if (drawable instanceof colordrawable) {
bitmap = bitmap.createbitmap(colordrawable_dimension, colordrawable_dimension, bitmap_config);
} else {
bitmap = bitmap.createbitmap(drawable.getintrinsicwidth(), drawable.getintrinsicheight(), bitmap_config);
}
canvas canvas = new canvas(bitmap);
drawable.setbounds(0, 0, canvas.getwidth(), canvas.getheight());
drawable.draw(canvas);
return bitmap;
} catch (exception e) {
e.printstacktrace();
return null;
}
}
private void initializebitmap() {
if (mdisablecirculartransformation) {
mbitmap = null;
} else {
mbitmap = getbitmapfromdrawable(getdrawable());
}
setup();
}
private void setup() {
if (!mready) {
msetuppending = true;
return;
}
if (getwidth() == 0 && getheight() == 0) {
return;
}
if (mbitmap == null) {
invalidate();
return;
}
mbitmapshader = new bitmapshader(mbitmap, shader.tilemode.clamp, shader.tilemode.clamp);
mbitmappaint.setantialias(true);
mbitmappaint.setshader(mbitmapshader);
mborderpaint.setstyle(paint.style.stroke);
mborderpaint.setantialias(true);
mborderpaint.setcolor(mbordercolor);
mborderpaint.setstrokewidth(mborderwidth);
mfillpaint.setstyle(paint.style.fill);
mfillpaint.setantialias(true);
mfillpaint.setcolor(mfillcolor);
mbitmapheight = mbitmap.getheight();
mbitmapwidth = mbitmap.getwidth();
mborderrect.set(calculatebounds());
mborderradius = math.min((mborderrect.height() - mborderwidth) / 2.0f, (mborderrect.width() - mborderwidth) / 2.0f);
mdrawablerect.set(mborderrect);
if (!mborderoverlay && mborderwidth > 0) {
mdrawablerect.inset(mborderwidth - 1.0f, mborderwidth - 1.0f);
}
mdrawableradius = math.min(mdrawablerect.height() / 2.0f, mdrawablerect.width() / 2.0f);
applycolorfilter();
updateshadermatrix();
invalidate();
}
private rectf calculatebounds() {
int availablewidth = getwidth() - getpaddingleft() - getpaddingright();
int availableheight = getheight() - getpaddingtop() - getpaddingbottom();
int sidelength = math.min(availablewidth, availableheight);
float left = getpaddingleft() + (availablewidth - sidelength) / 2f;
float top = getpaddingtop() + (availableheight - sidelength) / 2f;
return new rectf(left, top, left + sidelength, top + sidelength);
}
private void updateshadermatrix() {
float scale;
float dx = 0;
float dy = 0;
mshadermatrix.set(null);
if (mbitmapwidth * mdrawablerect.height() > mdrawablerect.width() * mbitmapheight) {
scale = mdrawablerect.height() / (float) mbitmapheight;
dx = (mdrawablerect.width() - mbitmapwidth * scale) * 0.5f;
} else {
scale = mdrawablerect.width() / (float) mbitmapwidth;
dy = (mdrawablerect.height() - mbitmapheight * scale) * 0.5f;
}
mshadermatrix.setscale(scale, scale);
mshadermatrix.posttranslate((int) (dx + 0.5f) + mdrawablerect.left, (int) (dy + 0.5f) + mdrawablerect.top);
mbitmapshader.setlocalmatrix(mshadermatrix);
}
}

六、在attrs中

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="circleimageview">
<attr name="civ_border_width" format="dimension" />
<attr name="civ_border_color" format="color" />
<attr name="civ_border_overlay" format="boolean" />
<attr name="civ_fill_color" format="color" />
</declare-styleable>
<declare-styleable name="rangeseekbar">
<attr name="seekbarheight" format="dimension" />
<attr name="textsize" format="dimension" />
<attr name="spacebetween" format="dimension" />
<attr name="leftcursorbackground" format="reference" />
<attr name="rightcursorbackground" format="reference" />
<attr name="marktextarray" format="reference" />
<attr name="textcolornormal" format="color" />
<attr name="textcolorselected" format="color" />
<attr name="seekbarcolornormal" format="color" />
<attr name="seekbarcolorselected" format="color" />
<attr name="automoveduration" format="integer" />
</declare-styleable>
</resources>

以上所述是小编给大家介绍的android实现水波纹效果,希望对大家有所帮助