Android 闪屏广告页面

Android 启动页的倒计时封装

比较简单,直接上代码
广告页view

package com.tkda.advert.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.tkda.advert.R;
import com.tkda.advert.http.DownLoadResource;
import com.tkda.advert.interf.FinishInterfece;
import com.tkda.advert.interf.HttpCallBack;
import com.tkda.advert.tools.DensityUtils;

public class AdvertView extends FrameLayout {
    private ImageView imageView;
    private CountDownView countDownView;

    private final int defCircleBg = 0x583d3d3d;
    private final int defTextColor = 0xffefefef;
    private final int defTextSize = 12;//sp
    private final int defProgressColor = 0xffff0000;
    private final int defProgressWidth = 3;// dp
    private final int defTime = 3; //单位 s
    private final int defRadius = 25; //单位 dp

    private boolean show;
    private int circleBg;
    private int textColor;
    private int textSize;
    private int progressColor;
    private int progressWidth;
    private int radius;
    private int time;

    private FinishInterfece finishInterfece;

    public void setFinishInterfece(FinishInterfece finishInterfece) {
        this.finishInterfece = finishInterfece;
    }

    public AdvertView(Context context) {
        this(context, null);
    }

    public AdvertView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AdvertView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AdvertView, defStyleAttr, 0);
        circleBg = typedArray.getColor(R.styleable.AdvertView_time_bg, defCircleBg);

        progressColor = typedArray.getColor(R.styleable.AdvertView_time_pro_color, defProgressColor);
        progressWidth = typedArray.getDimensionPixelSize(R.styleable.AdvertView_time_pro_width, DensityUtils.dp2px(context, defProgressWidth));

        textColor = typedArray.getColor(R.styleable.AdvertView_time_text_color, defTextColor);
        textSize = typedArray.getDimensionPixelSize(R.styleable.AdvertView_time_text_size, DensityUtils.sp2px(context, defTextSize));

        time = typedArray.getInteger(R.styleable.AdvertView_time_num, defTime);

        radius = typedArray.getDimensionPixelSize(R.styleable.AdvertView_time_radius, DensityUtils.dp2px(context, defRadius));

        show = typedArray.getBoolean(R.styleable.AdvertView_show, true);

        typedArray.recycle();

        View view = View.inflate(context, R.layout.advert_layout, this);

        imageView = view.findViewById(R.id.image);
        countDownView = view.findViewById(R.id.cd_view);
        countDownView.setFinishInterfece(new FinishInterfece() {
            @Override
            public void onFinish() {
                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();
        }
    }

    public void setImage(int resourceId) {
        if (imageView != null) {
            imageView.setImageResource(resourceId);
        }
    }

    public void setImage(Bitmap bitmap) {
        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
        }
    }

    public void setImage(String url) {
        if (url != null) {
            if (url.contains("http")) {
                DownLoadResource downLoadResource = new DownLoadResource();
                downLoadResource.setHttpCallBack(new HttpCallBack() {
                    @Override
                    public void onSuccess(Bitmap bitmap) {
                        setImage(bitmap);
                    }
                });
                downLoadResource.execute(url);
            }
        }
    }
}

倒计时view

package com.tkda.advert.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.tkda.advert.tools.DensityUtils;
import com.tkda.advert.interf.FinishInterfece;
import com.tkda.advert.R;

public class CountDownView extends View {

    private final int defCircleBg = 0x583d3d3d;
    private final int defTextColor = 0xffefefef;
    private final int defTextSize = 12;//sp
    private final int defProgressColor = 0xffff0000;
    private final int defProgressWidth = 3;// dp
    private final int defTime = 3; //单位 s
    private final int defRadius = 25; //单位 dp

    private int circleBg;
    private int textColor;
    private int textSize;
    private int progressColor;
    private int progressWidth;
    private int radius;
    private int time;

    private float totalTime;
    private float curTime;

    private Paint bgPaint;
    private Paint textPaint;
    private Paint proPaint;

    private Rect mBounds;//绘制文字的范围
    private RectF mRectF;//圆弧的外接矩形

    private FinishInterfece finishInterfece;

    private Handler handler = new Handler();

    public CountDownView(Context context) {
        this(context, null);
    }

    public CountDownView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CountDownView, defStyleAttr, 0);
        circleBg = typedArray.getColor(R.styleable.CountDownView_circleBg, defCircleBg);

        progressColor = typedArray.getColor(R.styleable.CountDownView_progressColor, defProgressColor);
        progressWidth = typedArray.getDimensionPixelSize(R.styleable.CountDownView_progressWidth, DensityUtils.dp2px(context, defProgressWidth));

        textColor = typedArray.getColor(R.styleable.CountDownView_textColor, defTextColor);
        textSize = typedArray.getDimensionPixelSize(R.styleable.CountDownView_textSize, DensityUtils.sp2px(context, defTextSize));

        time = typedArray.getInteger(R.styleable.CountDownView_time, defTime);

        radius = typedArray.getDimensionPixelSize(R.styleable.CountDownView_radius, DensityUtils.dp2px(context, defRadius));

        typedArray.recycle();

        // 初始化 画笔
        initPaint();
    }

    private void initPaint() {
        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setColor(circleBg);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);

        proPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        proPaint.setColor(progressColor);
        proPaint.setStyle(Paint.Style.STROKE);
        proPaint.setStrokeWidth(progressWidth);

        mBounds = new Rect();
        mRectF = new RectF();

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finishInterfece != null) {
                    finishInterfece.onFinish();
                    if (handler != null)
                        handler.removeCallbacks(showTimeRunnable);
                }
            }
        });

        //扩大10倍
        totalTime = time * 100;
        curTime = totalTime;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));
    }

    private int measureDimension(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {//指定宽高了
            result = specSize;
            if (result / 2 < radius) {
                radius = result / 2;
            }
        } else {// 一般为WARP_CONTENT
            result = 2 * radius;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画背景圆
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, bgPaint);
        //画进度条
        mRectF.set(getWidth() / 2 - radius + progressWidth / 2, getHeight() / 2 - radius + progressWidth / 2, getWidth() / 2 + radius - progressWidth / 2, getHeight() / 2 + radius - progressWidth / 2);
        canvas.drawArc(mRectF, 270, 360 * curTime / totalTime, false, proPaint);
        //画文本
        String text = "跳过";
        textPaint.getTextBounds(text, 0, text.length(), mBounds);
        canvas.drawText(text, getWidth() / 2 - mBounds.width() / 2, getHeight() / 2 + mBounds.height() * 2 / 5, textPaint);

    }

    public void setCircleBg(int circleBg) {
        this.circleBg = circleBg;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public void setProgressWidth(int progressWidth) {
        this.progressWidth = progressWidth;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public void setFinishInterfece(FinishInterfece finishInterfece) {
        this.finishInterfece = finishInterfece;
    }

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

    /**
     * 不显示view  开始
     */
    public void unShowStart() {
        if (handler != null) {
            handler.postDelayed(unShowTimeRunnable, 100);
        }
    }

    private Runnable unShowTimeRunnable = new Runnable() {
        @Override
        public void run() {
            if (time == 0) {
                if (finishInterfece != null)
                    finishInterfece.onFinish();
                handler = null;
                return;
            }
            time--;
            handler.postDelayed(unShowTimeRunnable, 1000);
        }
    };
    private Runnable showTimeRunnable = new Runnable() {
        @Override
        public void run() {
            if (curTime == 0) {
                if (finishInterfece != null)
                    finishInterfece.onFinish();
                handler = null;
                return;
            }
            curTime--;
            invalidate();
            handler.postDelayed(showTimeRunnable, 10);//10毫秒刷新一次
        }
    };
}

使用

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.tkda.advert.view.AdvertView
        android:id="@+id/advert"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:show="true"
        app:time_bg="#993d3d3d"
        app:time_num="5"
        app:time_pro_color="#ff0000"
        app:time_pro_width="3dp"
        app:time_radius="30dp"
        app:time_text_size="14sp"
        app:time_text_color="#ff0000" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity

 AdvertView advert = findViewById(R.id.advert);
        advert.setImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582184513188&di=9756049deedd826da11c0664a10d4c3e&imgtype=0&src=http%3A%2F%2Fwww.17qq.com%2Fimg_qqtouxiang%2F16932598.jpeg");
//        advert.setImage(R.drawable.ic_launcher_background);
        advert.setFinishInterfece(new FinishInterfece() {
            @Override
            public void onFinish() {
                Toast.makeText(MainActivity.this, "结束", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
                finish();
            }
        });

GitHub

Android 闪屏广告页面

封装了ImageView+倒计时功能

功能特点

  • 倒计时时间设定
  • 倒计时进度条颜色设定
  • 倒计时进度条宽度设定
  • 倒计时“跳过”文字颜色设定
  • 倒计时“跳过”文字大小设定
  • 倒计时view背景颜色设定
  • 倒计时view半径大小设定
  • 倒计时按钮是否显示
  • 广告Image支持本地资源(drawable,bitmap)、网络资源

简单用例

在需要使用的Activity里面使用

        AdvertView advert = findViewById(R.id.advert);
        advert.setImage(R.drawable.ic_launcher_background);
        advert.setFinishInterfece(new FinishInterfece() {
            @Override
            public void onFinish() {
                // 计时器 结束回调
                Toast.makeText(MainActivity.this, "结束", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
                finish();
            }
        });

对应的Activity的xml 布局文件里面引入,根据自己需要设置参数

    <com.tkda.advert.view.AdvertView
        android:id="@+id/advert"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:show="true"
        app:time_bg="#993d3d3d"
        app:time_num="5"
        app:time_pro_color="#ff0000"
        app:time_pro_width="3dp"
        app:time_radius="30dp"
        app:time_text_size="14sp"
        app:time_text_color="#ff0000" />

Attributes

nameformatdescription
showboolean是否显示倒计时
time_bgcolor倒计时背景颜色
time_numinteger倒计时秒数
time_pro_colorcolor倒计时进度条颜色
time_pro_widthdimension倒计时进度条宽度
time_text_colorcolor倒计时按钮中间“跳过”文字颜色
time_text_sizedimension倒计时按钮中间“跳过”文字大小

Method

nameformatdescription
setImageint资源图片的id
setImageBitmap资源图片
setImageString资源图片的url
setFinishInterfeceFinishInterfece倒计时结束回调

使用


Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

dependencies {
            implementation 'com.github.HeyMouser:AdvertPage:0.1'
    }


直接下载advert,并依赖
在这里插入图片描述

implementation project(':advert')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值