一、效果
点击开始:
点击停止:
二、在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;@OverrIDeprotected 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);}@OverrIDepublic 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"><buttonandroID:ID="@+ID/start"androID:layout_margintop="10dp"androID:layout_marginleft="10dp"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:text="开始" /><buttonandroID: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.WaveVIEwandroID: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.circleimageVIEwandroID: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() {@OverrIDepublic 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);}@OverrIDeprotected 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,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;}}@OverrIDepublic ScaleType getScaleType() {return SCALE_TYPE;}@OverrIDepublic voID setScaleType(ScaleType scaleType) {if (scaleType != SCALE_TYPE) {throw new IllegalArgumentException(String.format("ScaleType %s not supported.",scaleType));}}@OverrIDepublic voID setAdjustVIEwBounds(boolean adjustVIEwBounds) {if (adjustVIEwBounds) {throw new IllegalArgumentException("adjustVIEwBounds not supported.");}}@OverrIDeprotected 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(),mBitmapPaint);if (mborderWIDth > 0) {canvas.drawCircle(mborderRect.centerX(),mborderRect.centerY(),mborderRadius,mborderPaint);}}@OverrIDeprotected voID onSizeChanged(int w,int oldh) {super.onSizeChanged(w,h,olDW,oldh);setup();}@OverrIDepublic voID setpadding(int left,int top,int right,int bottom) {super.setpadding(left,top,right,bottom);setup();}@OverrIDepublic voID setpaddingrelative(int start,int end,int bottom) {super.setpaddingrelative(start,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*/@Deprecatedpublic 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*/@Deprecatedpublic 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*/@Deprecatedpublic 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*/@Deprecatedpublic 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();}@OverrIDepublic voID setimageBitmap(Bitmap bm) {super.setimageBitmap(bm);initializeBitmap();}@OverrIDepublic voID setimageDrawable(Drawable drawable) {super.setimageDrawable(drawable);initializeBitmap();}@OverrIDepublic voID setimageResource(@DrawableRes int resID) {super.setimageResource(resID);initializeBitmap();}@OverrIDepublic voID setimageURI(Uri uri) {super.setimageURI(uri);initializeBitmap();}@OverrIDepublic voID setcolorFilter(colorFilter cf) {if (cf == mcolorFilter) {return;}mcolorFilter = cf;applycolorFilter();invalIDate();}@OverrIDepublic 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,canvas.getWIDth(),canvas.getHeight());drawable.draw(canvas);return bitmap;} catch (Exception e) {e.@R_403_1715@();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,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实现水波纹效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android实现水波纹效果全部内容,希望文章能够帮你解决Android实现水波纹效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)