Android 分享一个流量显示界面
16lz
2021-01-23
http://blog.csdn.net/wangjinyu501/article/details/39527021
http://blog.csdn.net/wangjinyu501/article/details/39527021
http://blog.csdn.net/wangjinyu501/article/details/39527021
http://blog.csdn.net/wangjinyu501/article/details/39527021
http://blog.csdn.net/wangjinyu501/article/details/39527021
http://blog.csdn.net/wangjinyu501/article/details/39527021
Android 分享一个流量显示界面
分类:Android 2014-09-24 19:32 1204人阅读 评论(7) 收藏 举报 版本:1.0 日期:2014.8.9 2014.9.24 版权:© 2014 kince 转载注明出处波形效果有几种不同的呈现形式,比如从中间向四周散开的波形,也就是熟知的水涟漪;还有上下波动的曲线,像五线谱等。英文中可以称作Wave或者Ripple,所以暂且叫它们WaveView、WaveLayout、RippleView、RippleLayout,接下来开始实现这些效果。 首先看一下Solo 火爆足球动态壁纸,
下面中间的按钮就是一个波形按钮,它会不断地向四周辐射,由于是静态图,如果想体验真实效果可以另行下载。这种效果的实现思路是不断绘制圆形,当然半径也要不断变化,透明度也是一样。代码如下: [html] view plain copy
- /**
- *
- */
- packagecom.kince.rippleview;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.graphics.Canvas;
- importandroid.graphics.Color;
- importandroid.graphics.Paint;
- importandroid.graphics.RectF;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.util.AttributeSet;
- importandroid.view.View;
- /**
- *@authorkince
- *@category波纹
- *@since2014.8.9
- *@versionv1.0.0
- *
- */
- publicclassRippleViewextendsView{
- privateintmScreenWidth;
- privateintmScreenHeight;
- privateBitmapmRippleBitmap;
- privatePaintmRipplePaint=newPaint();
- privateintmBitmapWidth;
- privateintmBitmapHeight;
- privatebooleanisStartRipple;
- privateintheightPaddingTop;
- privateintheightPaddingBottom;
- privateintwidthPaddingLeft;
- privateintwidthPaddingRight;
- privateRectFmRect=newRectF();
- privateintrippleFirstRadius=0;
- privateintrippleSecendRadius=-33;
- privateintrippleThirdRadius=-66;
- privatePainttextPaint=newPaint();
- privateStringmText="点击我吧";
- privateHandlerhandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- //TODOAuto-generatedmethodstub
- super.handleMessage(msg);
- invalidate();
- if(isStartRipple){
- rippleFirstRadius++;
- if(rippleFirstRadius>100){
- rippleFirstRadius=0;
- }
- rippleSecendRadius++;
- if(rippleSecendRadius>100){
- rippleSecendRadius=0;
- }
- rippleThirdRadius++;
- if(rippleThirdRadius>100){
- rippleThirdRadius=0;
- }
- sendEmptyMessageDelayed(0,20);
- }
- }
- };
- /**
- *@paramcontext
- */
- publicRippleView(Contextcontext){
- super(context);
- //TODOAuto-generatedconstructorstub
- init();
- }
- /**
- *@paramcontext
- *@paramattrs
- */
- publicRippleView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- //TODOAuto-generatedconstructorstub
- init();
- }
- /**
- *@paramcontext
- *@paramattrs
- *@paramdefStyleAttr
- */
- publicRippleView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
- super(context,attrs,defStyleAttr);
- //TODOAuto-generatedconstructorstub
- init();
- }
- privatevoidinit(){
- mRipplePaint.setColor(4961729);
- mRipplePaint.setAntiAlias(true);
- mRipplePaint.setStyle(Paint.Style.FILL);
- textPaint.setTextSize(26);
- textPaint.setAntiAlias(true);
- textPaint.setStyle(Paint.Style.FILL);
- textPaint.setColor(Color.WHITE);
- mRippleBitmap=BitmapFactory.decodeStream(getResources()
- .openRawResource(R.drawable.easy3d_ic_apply));
- mBitmapWidth=mRippleBitmap.getWidth();
- mBitmapHeight=mRippleBitmap.getHeight();
- }
- @Override
- protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
- //TODOAuto-generatedmethodstub
- super.onMeasure(widthMeasureSpec,heightMeasureSpec);
- intmh=getMeasuredHeight()-getPaddingTop()-getPaddingBottom();
- intmw=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
- if(mBitmapWidth<2*mBitmapHeight){
- mBitmapWidth=(2*mBitmapHeight);
- }
- setMeasuredDimension(mBitmapWidth,mBitmapHeight);
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- //TODOAuto-generatedmethodstub
- super.onDraw(canvas);
- if(isStartRipple){
- floatf1=3*mBitmapHeight/10;
- mRipplePaint.setAlpha(255);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,
- 7*mBitmapHeight/10,mRipplePaint);
- inti1=(int)(220.0F-(220.0F-0.0F)/100.0F
- *rippleFirstRadius);
- mRipplePaint.setAlpha(i1);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,7
- *mBitmapHeight/10+f1*rippleFirstRadius/100.0F,
- mRipplePaint);
- if(rippleSecendRadius>=0){
- inti3=(int)(220.0F-(220.0F-0.0F)/100.0F
- *rippleSecendRadius);
- mRipplePaint.setAlpha(i3);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,
- 7*mBitmapHeight/10+f1*rippleSecendRadius
- /100.0F,mRipplePaint);
- }
- if(rippleThirdRadius>=0){
- inti2=(int)(220.0F-(220.0F-0.0F)/100.0F
- *rippleThirdRadius);
- mRipplePaint.setAlpha(i2);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,7
- *mBitmapHeight/10+f1*rippleThirdRadius/100.0F,
- mRipplePaint);
- }
- }
- mRipplePaint.setAlpha(30);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,mBitmapHeight,
- mRipplePaint);
- mRipplePaint.setAlpha(120);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,
- 9*mBitmapHeight/10,mRipplePaint);
- mRipplePaint.setAlpha(180);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,
- 8*mBitmapHeight/10,mRipplePaint);
- mRipplePaint.setAlpha(255);
- canvas.drawCircle(mBitmapWidth/2,mBitmapHeight,
- 7*mBitmapHeight/10,mRipplePaint);
- floatlength=textPaint.measureText(mText);
- canvas.drawText(mText,(mBitmapWidth-length)/2,
- mBitmapHeight*3/4,textPaint);
- }
- @Override
- protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
- //TODOAuto-generatedmethodstub
- super.onSizeChanged(w,h,oldw,oldh);
- mScreenWidth=w;
- mScreenHeight=h;
- confirmSize();
- invalidate();
- }
- privatevoidconfirmSize(){
- intminScreenSize=Math.min(mScreenWidth,mScreenHeight);
- intwidthOverSize=mScreenWidth-minScreenSize;
- intheightOverSize=mScreenHeight-minScreenSize;
- heightPaddingTop=(getPaddingTop()+heightOverSize/2);
- heightPaddingBottom=(getPaddingBottom()+heightOverSize/2);
- widthPaddingLeft=(getPaddingLeft()+widthOverSize/2);
- widthPaddingRight=(getPaddingRight()+widthOverSize/2);
- intwidth=getWidth();
- intheight=getHeight();
- mRect=newRectF(widthPaddingLeft,heightPaddingTop,width
- -widthPaddingRight,height*2-heightPaddingBottom);
- }
- publicvoidstratRipple(){
- isStartRipple=true;
- handler.sendEmptyMessage(0);
- }
- }
- /**
- *
- */
- packagecom.kince.waveview;
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.graphics.Color;
- importandroid.graphics.Paint;
- importandroid.graphics.Path;
- importandroid.graphics.RectF;
- importandroid.os.Handler;
- importandroid.os.Parcel;
- importandroid.os.Parcelable;
- importandroid.util.AttributeSet;
- importandroid.view.View;
- importandroid.widget.ProgressBar;
- /**
- *@authorkince
- *@categoryView必须是正方形
- *
- */
- publicclassWaterWaveViewextendsView{
- privateContextmContext;
- privateintmScreenWidth;
- privateintmScreenHeight;
- privatePaintmRingPaint;
- privatePaintmCirclePaint;
- privatePaintmWavePaint;
- privatePaintlinePaint;
- privatePaintflowPaint;
- privatePaintleftPaint;
- privateintmRingSTROKEWidth=15;
- privateintmCircleSTROKEWidth=2;
- privateintmLineSTROKEWidth=1;
- privateintmCircleColor=Color.WHITE;
- privateintmRingColor=Color.WHITE;
- privateintmWaveColor=Color.WHITE;
- privateHandlermHandler;
- privatelongc=0L;
- privatebooleanmStarted=false;
- privatefinalfloatf=0.033F;
- privateintmAlpha=50;//透明度
- privatefloatmAmplitude=10.0F;//振幅
- privatefloatmWateLevel=0.5F;//水高(0~1)
- privatePathmPath;
- privateStringflowNum="1024M";
- privateStringflowLeft="还剩余";
- /**
- *@paramcontext
- */
- publicWaterWaveView(Contextcontext){
- super(context);
- //TODOAuto-generatedconstructorstub
- mContext=context;
- init(mContext);
- }
- /**
- *@paramcontext
- *@paramattrs
- */
- publicWaterWaveView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- //TODOAuto-generatedconstructorstub
- mContext=context;
- init(mContext);
- }
- /**
- *@paramcontext
- *@paramattrs
- *@paramdefStyleAttr
- */
- publicWaterWaveView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
- super(context,attrs,defStyleAttr);
- //TODOAuto-generatedconstructorstub
- mContext=context;
- init(mContext);
- }
- privatevoidinit(Contextcontext){
- mRingPaint=newPaint();
- mRingPaint.setColor(mRingColor);
- mRingPaint.setAlpha(50);
- mRingPaint.setStyle(Paint.Style.STROKE);
- mRingPaint.setAntiAlias(true);
- mRingPaint.setStrokeWidth(mRingSTROKEWidth);
- mCirclePaint=newPaint();
- mCirclePaint.setColor(mCircleColor);
- mCirclePaint.setStyle(Paint.Style.STROKE);
- mCirclePaint.setAntiAlias(true);
- mCirclePaint.setStrokeWidth(mCircleSTROKEWidth);
- linePaint=newPaint();
- linePaint.setColor(mCircleColor);
- linePaint.setStyle(Paint.Style.STROKE);
- linePaint.setAntiAlias(true);
- linePaint.setStrokeWidth(mLineSTROKEWidth);
- flowPaint=newPaint();
- flowPaint.setColor(mCircleColor);
- flowPaint.setStyle(Paint.Style.FILL);
- flowPaint.setAntiAlias(true);
- flowPaint.setTextSize(36);
- leftPaint=newPaint();
- leftPaint.setColor(mCircleColor);
- leftPaint.setStyle(Paint.Style.FILL);
- leftPaint.setAntiAlias(true);
- leftPaint.setTextSize(18);
- mWavePaint=newPaint();
- mWavePaint.setStrokeWidth(1.0F);
- mWavePaint.setColor(mWaveColor);
- mWavePaint.setAlpha(mAlpha);
- mPath=newPath();
- mHandler=newHandler(){
- @Override
- publicvoidhandleMessage(android.os.Messagemsg){
- if(msg.what==0){
- invalidate();
- if(mStarted){
- //不断发消息给自己,使自己不断被重绘
- mHandler.sendEmptyMessageDelayed(0,60L);
- }
- }
- }
- };
- }
- @Override
- protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
- intwidth=measure(widthMeasureSpec,true);
- intheight=measure(heightMeasureSpec,false);
- if(width<height){
- setMeasuredDimension(width,width);
- }else{
- setMeasuredDimension(height,height);
- }
- }
- /**
- *@category测量
- *@parammeasureSpec
- *@paramisWidth
- *@return
- */
- privateintmeasure(intmeasureSpec,booleanisWidth){
- intresult;
- intmode=MeasureSpec.getMode(measureSpec);
- intsize=MeasureSpec.getSize(measureSpec);
- intpadding=isWidth?getPaddingLeft()+getPaddingRight()
- :getPaddingTop()+getPaddingBottom();
- if(mode==MeasureSpec.EXACTLY){
- result=size;
- }else{
- result=isWidth?getSuggestedMinimumWidth()
- :getSuggestedMinimumHeight();
- result+=padding;
- if(mode==MeasureSpec.AT_MOST){
- if(isWidth){
- result=Math.max(result,size);
- }else{
- result=Math.min(result,size);
- }
- }
- }
- returnresult;
- }
- @Override
- protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
- //TODOAuto-generatedmethodstub
- super.onSizeChanged(w,h,oldw,oldh);
- mScreenWidth=w;
- mScreenHeight=h;
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- //TODOAuto-generatedmethodstub
- super.onDraw(canvas);
- //得到控件的宽高
- intwidth=getWidth();
- intheight=getHeight();
- setBackgroundColor(mContext.getResources().getColor(
- R.color.holo_purple2));
- canvas.drawCircle(mScreenWidth/2,mScreenHeight/2,
- mScreenWidth/4,mRingPaint);
- canvas.drawCircle(mScreenWidth/2,mScreenHeight/2,mScreenWidth/4
- -mRingSTROKEWidth/2,mCirclePaint);
- canvas.drawLine(mScreenWidth*3/8,mScreenHeight*5/8,
- mScreenWidth*5/8,mScreenHeight*5/8,linePaint);
- floatnum=flowPaint.measureText(flowNum);
- canvas.drawText(flowNum,mScreenWidth*4/8-num/2,
- mScreenHeight*4/8,flowPaint);
- floatleft=leftPaint.measureText(flowLeft);
- canvas.drawText(flowLeft,mScreenWidth*4/8-left/2,
- mScreenHeight*3/8,leftPaint);
- //如果未开始(未调用startWave方法),绘制一个扇形
- if((!mStarted)||(mScreenWidth==0)||(mScreenHeight==0)){
- RectFoval=newRectF(mScreenWidth/4+mRingSTROKEWidth/2,
- mScreenHeight/4+mRingSTROKEWidth/2,mScreenWidth*3
- /4-mRingSTROKEWidth/2,mScreenHeight*3/4
- -mRingSTROKEWidth/2);//设置个新的长方形,扫描测量
- canvas.drawArc(oval,0,180,true,mWavePaint);
- return;
- }
- //绘制,即水面静止时的高度
- RectFoval=newRectF(mScreenWidth/4+mRingSTROKEWidth/2,
- mScreenHeight/4+mRingSTROKEWidth/2+mAmplitude*2,
- mScreenWidth*3/4-mRingSTROKEWidth/2,mScreenHeight*3
- /4-mRingSTROKEWidth/2);//设置个新的长方形,扫描测量
- canvas.drawArc(oval,0,180,true,mWavePaint);
- if(this.c>=8388607L){
- this.c=0L;
- }
- //每次onDraw时c都会自增
- c=(1L+c);
- floatf1=mScreenHeight*(1.0F-mWateLevel);
- inttop=(int)(f1+mAmplitude);
- mPath.reset();
- intstartX=mScreenWidth/2-mScreenWidth/4+mRingSTROKEWidth/2;
- //波浪效果
- while(startX<mScreenWidth/2+mScreenWidth/4-mRingSTROKEWidth
- /2){
- intstartY=(int)(f1-mAmplitude
- *Math.sin(Math.PI
- *(2.0F*(startX+this.c*width*this.f))
- /width));
- canvas.drawLine(startX,startY,startX,top,mWavePaint);
- startX++;
- }
- canvas.restore();
- }
- @Override
- publicParcelableonSaveInstanceState(){
- //Forceourancestorclasstosaveitsstate
- ParcelablesuperState=super.onSaveInstanceState();
- SavedStatess=newSavedState(superState);
- ss.progress=(int)c;
- returnss;
- }
- @Override
- publicvoidonRestoreInstanceState(Parcelablestate){
- SavedStatess=(SavedState)state;
- super.onRestoreInstanceState(ss.getSuperState());
- c=ss.progress;
- }
- @Override
- protectedvoidonAttachedToWindow(){
- super.onAttachedToWindow();
- //关闭硬件加速,防止异常unsupportedoperationexception
- this.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
- }
- @Override
- protectedvoidonDetachedFromWindow(){
- super.onDetachedFromWindow();
- }
- /**
- *@category开始波动
- */
- publicvoidstartWave(){
- if(!mStarted){
- this.c=0L;
- mStarted=true;
- this.mHandler.sendEmptyMessage(0);
- }
- }
- /**
- *@category停止波动
- */
- publicvoidstopWave(){
- if(mStarted){
- this.c=0L;
- mStarted=false;
- this.mHandler.removeMessages(0);
- }
- }
- /**
- *@category保存状态
- */
- staticclassSavedStateextendsBaseSavedState{
- intprogress;
- /**
- *Constructorcalledfrom{@linkProgressBar#onSaveInstanceState()}
- */
- SavedState(ParcelablesuperState){
- super(superState);
- }
- /**
- *Constructorcalledfrom{@link#CREATOR}
- */
- privateSavedState(Parcelin){
- super(in);
- progress=in.readInt();
- }
- @Override
- publicvoidwriteToParcel(Parcelout,intflags){
- super.writeToParcel(out,flags);
- out.writeInt(progress);
- }
- publicstaticfinalParcelable.Creator<SavedState>CREATOR=newParcelable.Creator<SavedState>(){
- publicSavedStatecreateFromParcel(Parcelin){
- returnnewSavedState(in);
- }
- publicSavedState[]newArray(intsize){
- returnnewSavedState[size];
- }
- };
- }
- }
github下载地址: https://github.com/wangjinyu501/RippleView https://github.com/wangjinyu501/WaterWaveView/
更多相关文章
- Android 实现图片闪烁效果
- Android 震动示例--心跳效果
- Android动画效果学习
- JavaGG android实现uc 和墨迹天气那样的左右拖动效果
- Android切换Activity的动画效果
- android 控件 3d 旋转效果
- Android中开关按钮IOS效果的实现
- android ListView滑动到顶部带弹性效果