当前位置:网站首页 > 图文广告 > 正文

android启动时的广告页面

作者:admin发布时间:2021-09-25分类:图文广告浏览:评论:3


导读:闪屏广告页面启动页的倒计时封装比较简单直接上代码广告页view;;;;;;;importandroid.widget.ImageView;importcom.tkda.advert...

android启动时的广告页面

闪屏广告页面

启动页的倒计时封装

比较简单直接上代码

广告页view

;;;;;;;importandroid.widget.ImageView;importcom.tkda.advert.R;importcom.tkda.advert.http.DownLoadResource;importcom.tkda.advert.interf.FinishInterfece;importcom.tkda.advert.interf.HttpCallBack;importcom.tkda.advert.tools.DensityUtils;publicclassAdvertViewextendsFrameLayout{privateImageViewimageView;privateCountDownViewcountDownView;privatefinalintdefCircleBg0x583d3d3d;privatefinalintdefTextColor0xffefefef;privatefinalintdefTextSize12;//spprivatefinalintdefProgressColor0xffff0000;privatefinalintdefProgressWidth3;//dpprivatefinalintdefTime3;//单位sprivatefinalintdefRadius25;//单位dpprivatebooleanshow;privateintcircleBg;privateinttextColor;privateinttextSize;privateintprogressColor;privateintprogressWidth;privateintradius;privateinttime;privateFinishInterfecefinishInterfece;publicvoidsetFinishInterfece(FinishInterfecefinishInterfece){this.finishInterfecefinishInterfece;}publicAdvertView(Contextcontext){this(context,null);}publicAdvertView(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicAdvertView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);TypedArraytypedArraycontext.obtainStyledAttributes(attrs,R.styleable.AdvertView,defStyleAttr,0);circleBgtypedArray.getColor(R.styleable.AdvertView_time_bg,defCircleBg);progressColortypedArray.getColor(R.styleable.AdvertView_time_pro_color,defProgressColor);progressWidthtypedArray.getDimensionPixelSize(R.styleable.AdvertView_time_pro_width,DensityUtils.dp2px(context,defProgressWidth));textColortypedArray.getColor(R.styleable.AdvertView_time_text_color,defTextColor);textSizetypedArray.getDimensionPixelSize(R.styleable.AdvertView_time_text_size,DensityUtils.sp2px(context,defTextSize));timetypedArray.getInteger(R.styleable.AdvertView_time_num,defTime);radiustypedArray.getDimensionPixelSize(R.styleable.AdvertView_time_radius,DensityUtils.dp2px(context,defRadius));showtypedArray.getBoolean(R.styleable.AdvertView_show,true);typedArray.recycle();ViewviewView.inflate(context,R.layout.advert_layout,this);imageViewview.findViewById(R.id.image);countDownViewview.findViewById(R.id.cd_view);countDownView.setFinishInterfece(newFinishInterfece(){OverridepublicvoidonFinish(){if(finishInterfece!null){finishInterfece.onFinish();}}});if(show){countDownView.setVisibility(View.VISIBLE);countDownView.setCircleBg(circleBg);countDownView.setProgressColor(progressColor);countDownView.setProgressWidth(progressWidth);countDownView.setTextColor(textColor);countDownView.setTextSize(textSize);countDownView.setTime(time);countDownView.setRadius(radius);countDownView.showStartTime();}else{countDownView.setVisibility(View.GONE);countDownView.unShowStart();}}publicvoidsetImage(intresourceId){if(imageView!null){imageView.setImageResource(resourceId);}}publicvoidsetImage(Bitmapbitmap){if(imageView!null){imageView.setImageBitmap(bitmap);}}publicvoidsetImage(Stringurl){if(url!null){if(url.contains(http)){DownLoadResourcedownLoadResourcenewDownLoadResource();downLoadResource.setHttpCallBack(newHttpCallBack(){OverridepublicvoidonSuccess(Bitmapbitmap){setImage(bitmap);}});downLoadResource.execute(url);}}}}

倒计时view

packagecom.tkda.advert.view;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.graphics.RectF;importandroid.os.Handler;importandroid.util.AttributeSet;importandroid.view.View;importandroidx.annotation.Nullable;importcom.tkda.advert.tools.DensityUtils;importcom.tkda.advert.interf.FinishInterfece;importcom.tkda.advert.R;publicclassCountDownViewextendsView{privatefinalintdefCircleBg0x583d3d3d;privatefinalintdefTextColor0xffefefef;privatefinalintdefTextSize12;//spprivatefinalintdefProgressColor0xffff0000;privatefinalintdefProgressWidth3;//dpprivatefinalintdefTime3;//单位sprivatefinalintdefRadius25;//单位dpprivateintcircleBg;privateinttextColor;privateinttextSize;privateintprogressColor;privateintprogressWidth;privateintradius;privateinttime;privatefloattotalTime;privatefloatcurTime;privatePaintbgPaint;privatePainttextPaint;privatePaintproPaint;privateRectmBounds;//绘制文字的范围privateRectFmRectF;//圆弧的外接矩形privateFinishInterfecefinishInterfece;privateHandlerhandlernewHandler();publicCountDownView(Contextcontext){this(context,null);}publicCountDownView(Contextcontext,NullableAttributeSetattrs){this(context,attrs,0);}publicCountDownView(Contextcontext,NullableAttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);TypedArraytypedArraycontext.obtainStyledAttributes(attrs,R.styleable.CountDownView,defStyleAttr,0);circleBgtypedArray.getColor(R.styleable.CountDownView_circleBg,defCircleBg);progressColortypedArray.getColor(R.styleable.CountDownView_progressColor,defProgressColor);progressWidthtypedArray.getDimensionPixelSize(R.styleable.CountDownView_progressWidth,DensityUtils.dp2px(context,defProgressWidth));textColortypedArray.getColor(R.styleable.CountDownView_textColor,defTextColor);textSizetypedArray.getDimensionPixelSize(R.styleable.CountDownView_textSize,DensityUtils.sp2px(context,defTextSize));timetypedArray.getInteger(R.styleable.CountDownView_time,defTime);radiustypedArray.getDimensionPixelSize(R.styleable.CountDownView_radius,DensityUtils.dp2px(context,defRadius));typedArray.recycle();//初始化画笔initPaint();}privatevoidinitPaint(){bgPaintnewPaint(Paint.ANTI_ALIAS_FLAG);bgPaint.setColor(circleBg);textPaintnewPaint(Paint.ANTI_ALIAS_FLAG);textPaint.setColor(textColor);textPaint.setTextSize(textSize);proPaintnewPaint(Paint.ANTI_ALIAS_FLAG);proPaint.setColor(progressColor);proPaint.setStyle(Paint.Style.STROKE);proPaint.setStrokeWidth(progressWidth);mBoundsnewRect();mRectFnewRectF();setOnClickListener(newOnClickListener(){OverridepublicvoidonClick(Viewv){if(finishInterfece!null){finishInterfece.onFinish();if(handler!null)handler.removeCallbacks(showTimeRunnable);}}});//扩大10倍totalTimetime*100;curTimetotalTime;}OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){setMeasuredDimension(measureDimension(widthMeasureSpec),measureDimension(heightMeasureSpec));}privateintmeasureDimension(intmeasureSpec){intresult;intspecModeMeasureSpec.getMode(measureSpec);intspecSizeMeasureSpec.getSize(measureSpec);if(specModeMeasureSpec.EXACTLY){//指定宽高了resultspecSize;if(result/2radius){radiusresult/2;}}else{//一般为WARP_CONTENTresult2*radius;if(specModeMeasureSpec.AT_MOST){resultMath.min(result,specSize);}}returnresult;}OverrideprotectedvoidonDraw(Canvascanvas){//画背景圆canvas.drawCircle(getWidth()/2,getHeight()/2,radius,bgPaint);//画进度条mRectF.set(getWidth()/2-radiusprogressWidth/2,getHeight()/2-radiusprogressWidth/2,getWidth()/2radius-progressWidth/2,getHeight()/2radius-progressWidth/2);canvas.drawArc(mRectF,270,360*curTime/totalTime,false,proPaint);//画文本Stringtext跳过textPaint.getTextBounds(text,0,text.length(),mBounds);canvas.drawText(text,getWidth()/2-mBounds.width()/2,getHeight()/2mBounds.height()*2/5,textPaint);}publicvoidsetCircleBg(intcircleBg){this.circleBgcircleBg;}publicvoidsetTextColor(inttextColor){this.textColortextColor;}publicvoidsetTextSize(inttextSize){this.textSizetextSize;}publicvoidsetProgressColor(intprogressColor){this.progressColorprogressColor;}publicvoidsetProgressWidth(intprogressWidth){this.progressWidthprogressWidth;}publicvoidsetRadius(intradius){this.radiusradius;}publicvoidsetTime(inttime){this.timetime;}publicvoidsetFinishInterfece(FinishInterfecefinishInterfece){this.finishInterfecefinishInterfece;}/**

*显示view开始

*/publicvoidshowStartTime(){//开始之前若有外部方法设置参数就重新初始化一下画笔参数initPaint();if(handler!null){handler.postDelayed(showTimeRunnable,100);}}/**

*不显示view开始

*/publicvoidunShowStart(){if(handler!null){handler.postDelayed(unShowTimeRunnable,100);}}privateRunnableunShowTimeRunnablenewRunnable(){Overridepublicvoidrun(){if(time0){if(finishInterfece!null)finishInterfece.onFinish();handlernull;return;}time--;handler.postDelayed(unShowTimeRunnable,1000);}};privateRunnableshowTimeRunnablenewRunnable(){Overridepublicvoidrun(){if(curTime0){if(finishInterfece!null)finishInterfece.onFinish();handlernull;return;}curTime--;invalidate();handler.postDelayed(showTimeRunnable,10);//10毫秒刷新一次}};}

使用

?xmlversion1.0encodingutf-8?androidx.constraintlayout.widget.ConstraintLayoutxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivitycom.tkda.advert.view.AdvertViewandroid:idid/advertandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:showtrueapp:time_bg#993d3d3dapp:time_num5app:time_pro_color#ff0000app:time_pro_width3dpapp:time_radius30dpapp:time_text_size14spapp:time_text_color#ff0000//androidx.constraintlayout.widget.ConstraintLayout

Activity

AdvertViewadvertfindViewById(R.id.advert);advert.setImage(https://timgsa.baidu.com/timg?imagequality80sizeb9999_10000sec****88di9756049deedd826da11c0664a10d4c3eimgtype0srchttp%3A%2F%2Fwww.17qq.com%2Fimg_qqtouxiang%2F16932598.jpeg//advert.setImage(R.drawable.ic_launcher_background);advert.setFinishInterfece(newFinishInterfece(){OverridepublicvoidonFinish(){Toast.makeText(MainActivity.this,结束,Toast.LENGTH_SHORT).show();startActivity(newIntent(MainActivity.this,Main2Activity.class));finish();}});

GitHub


已有3位网友发表了看法:

欢迎 发表评论: