本文主要是介绍【Android 应用】广告倒计时该怎么做,来看看。。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天讲讲广告倒计时怎么做。这边讲的很明白。
先看下最终效果图:
看下这是不是你最终想要的,如果是请往下看。
一、参考
https://www.jianshu.com/p/8cd55b695676
二、实现步骤
上一篇中(https://blog.csdn.net/twk121109281/article/details/107236690)我们实现了smartimageview和Androidvideocache去加载和显示视频和图片。在其基础上我们看下倒计时显示是如何实现。
原理很简单,在整体布局中添加一个textview,并使用CountDownTimer去更新textview的文字显示。
1、整体布局修改
2、CountDownTimer更新
三、整体布局修改
1、将整体布局框架修改成帧布局(FrameLayout)
2、添加textview布局
3、textview背景布局
在res/anim目录(anim目录没有也创建一个)创建文件animation_text.xml文件
补充知识点:Android shape属性大全
在Android开发中,我们可以使用shape定义各种各样的形状,也可以定义一些图片资源。相对于传统图片来说,使用shape可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="#80000000"/><paddingandroid:bottom="3dp"android:left="8dp"android:right="8dp"android:top="3dp"/><cornersandroid:bottomLeftRadius="45dp"android:bottomRightRadius="45dp"android:topLeftRadius="45dp"android:topRightRadius="45dp"/></shape>
4、布局整体代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"><com.loopj.android.image.SmartImageViewandroid:id="@+id/imageView"android:layout_width="1280dp"android:layout_height="720dp"android:scaleType="fitXY"/><TextViewandroid:id="@+id/picTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:layout_margin="50dp"android:text="@string/ad_timeout"android:gravity="center"android:background="@anim/animation_text"android:textColor="@android:color/white"android:textSize="50sp"/>
</FrameLayout >
四、CountDownTimer更新
1、初始化textview
private TextView mPicTextView;mPicTextView = (TextView) mImageLayout.findViewById(R.id.picTextView);
2、实现CountDownTimer
class AdCountDownTimer extends CountDownTimer {/*** @param millisInFuture* 表示以「 毫秒 」为单位倒计时的总数* 例如 millisInFuture = 1000 表示1秒** @param countDownInterval* 表示 间隔 多少微秒 调用一次 onTick()* 例如: countDownInterval = 1000 ; 表示每 1000 毫秒调用一次 onTick()**/public AdCountDownTimer(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}public void onFinish() {mPicTextView.setText("0s");}public void onTick(long millisUntilFinished) {mPicTextView.setText( millisUntilFinished / 1000 + "s");}}
3、调用更新
private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {if (msg.what == 0) {mPicTextView.setText(mAdTime+"s");downTimerCancel();mCountDownTimer = new AdCountDownTimer(mAdTime*1000+300, 1000);mCountDownTimer.start();//tmpHandler.postDelayed(runnable, 1000);}};
};handler.sendEmptyMessage(0);
4、销毁
一定要注意销毁,防止内存泄漏。
private synchronized void downTimerCancel(){if (mCountDownTimer != null) {mCountDownTimer.cancel();mCountDownTimer = null;}
}
五、碰到的问题
1、时间开始时间不对
我们看CountDownTimer的源码可以看到,在执行onTick的方法时,google源码里面减去了程序执行到这里的时候所消耗的时间,这里可以看出google代码的严谨。
解决方案:设置时间的时间多加300ms即可
mCountDownTimer = new AdCountDownTimer(mAdTime*1000+300, 1000);
结束语
到这里一个简单的广告功能就开发完了。
这篇关于【Android 应用】广告倒计时该怎么做,来看看。。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!