android自定義倒計時控件示例

這篇文章主要介紹了Android秒殺倒計時自定義TextView示例,大家參考使用吧

自定義TextView控件TimeTextView代碼:

復制代碼 代碼如下:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;

import com.new0315.R;
/**
?* 自定義倒計時文本控件
?* @author Administrator
?*
?*/
public class TimeTextView extends TextView implements Runnable{

??? Paint mPaint; //畫筆,包含了畫幾何圖形、文本等的樣式和顏色信息

??? private long[] times;

??? private long mday, mhour, mmin, msecond;//天,小時,分鐘,秒

??? private boolean run=false; //是否啟動了

??? public TimeTextView(Context context, AttributeSet attrs) {
??????? super(context, attrs);
??????? mPaint=new Paint();
??????? TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);

??????? array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響
??? }

??? public TimeTextView(Context context, AttributeSet attrs, int defStyle) {
??????? super(context, attrs, defStyle);
??????? mPaint=new Paint();
??????? TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);

??????? array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響
??? }

??? public TimeTextView(Context context) {
??????? super(context);
??? }

??? public long[] getTimes() {
??????? return times;
??? }

??? public void setTimes(long[] times) {
??????? this.times = times;
??????? mday = times[0];
??????? mhour = times[1];
??????? mmin = times[2];
??????? msecond = times[3];

??? }

??? /**
???? * 倒計時計算
???? */
??? private void ComputeTime() {
??????? msecond--;
??????? if (msecond < 0) {
??????????? mmin--;
??????????? msecond = 59;
??????????? if (mmin < 0) {
??????????????? mmin = 59;
??????????????? mhour--;
??????????????? if (mhour < 0) {
??????????????????? // 倒計時結束
??????????????????? mhour = 59;
??????????????????? mday--;

??????????????? }
??????????? }

??????? }

??? }

??? public boolean isRun() {
??????? return run;
??? }

??? public void setRun(boolean run) {
??????? this.run = run;
??? }

??? @Override
??? public void run() {
??????? //標示已經啟動
??????? run=true;

??????? ComputeTime();

??????? String strTime="還剩</pre>
<span style="color: red;">"+mday+"</span>
<pre>"+"天</pre>
<span style="color: red;">"+mhour+"</span>
<pre>小時</pre>
<span style="color: red;">"+
?mmin+"</span>
<pre>分鐘</pre>
<span style="color: red;">"+msecond+"</span>
<pre>秒";
??????? this.setText(Html.fromHtml(strTime));

??????? postDelayed(this, 1000);

??? }

}

屬性atts.xml

復制代碼 代碼如下:

<declare-styleable name="TimeTextView">
</declare-styleable>

Adapter調用代碼:

復制代碼 代碼如下:

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.new0315.R;
import com.new0315.entity.SpecialGoods;
import com.new0315.utils.CorrectSpecialDataFormHttp;
import com.new0315.utils.DateTools;
import com.new0315.widgets.TimeTextView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SpecialGoodsAdapter extends BaseAdapter {

??? private Context context;
??? private List list;
??? private long sumTime;

??? public SpecialGoodsAdapter(Context context) {

??????? this.context = context;
??? }

??? public void setList(List list) {
??????? this.list = list;
??? }

??? @Override
??? public int getCount() {
??????? // TODO Auto-generated method stub
??????? return list.size();
??? }

??? @Override
??? public Object getItem(int arg0) {
??????? // TODO Auto-generated method stub
??????? return null;
??? }

??? @Override
??? public long getItemId(int arg0) {
??????? // TODO Auto-generated method stub
??????? return 0;
??? }

??? @Override
??? public View getView(int arg0, View convertView, ViewGroup arg2) {
??????? //開始計時,性能測試用nanoTime會更精確,因為它是納秒級的
??????? long startTime = System.nanoTime();
??????? Log.d("position","getView " + arg0 + " " + convertView);
??????? ViewHolder viewHolder;
??????? if(convertView == null)
??????? {
??????????? convertView = LayoutInflater.from(context).inflate(
??????????????????? R.layout.item_temai_list, null);
??????????? viewHolder = new ViewHolder();
??????????? viewHolder.goodName = (TextView) convertView
??????????????????? .findViewById(R.id.temai_Name);
??????????? viewHolder.price = (TextView) convertView
??????????????????? .findViewById(R.id.temai_yuanjia_text);

??????????? viewHolder.specialPrice = (TextView) convertView
??????????????????? .findViewById(R.id.temai_xiajia_text);
??????????? //特賣倒計時控件
??????????? viewHolder.mTimeText = (TimeTextView) convertView
??????????????????? .findViewById(R.id.temai_timeTextView);

??????????? viewHolder.showDate = (TextView) convertView
??????????????????? .findViewById(R.id.index_temai_day);
??????????? viewHolder.showDate_l = (LinearLayout) convertView
??????????????????? .findViewById(R.id.temai_weikaishi);
??????????? viewHolder.showTime = (LinearLayout) convertView
??????????????????? .findViewById(R.id.temai_yikaishi);
??????????? viewHolder.koukou = (TextView) convertView
??????????????????? .findViewById(R.id.temai_zhekou_text);
??????????? viewHolder.image = (ImageView) convertView
??????????????????? .findViewById(R.id.index_temai_image);
??????????? Log.d("GoogleIO","new position:"+viewHolder.goodName.getText());

??????????? convertView.setTag(viewHolder);

??????? }else {
??????????? viewHolder = (ViewHolder) convertView.getTag();
??????????? resetViewHolder(viewHolder);
??????? }
??????? //setData
??????? String off = getOff(list.get(arg0).getGoods_Price(), list.get(arg0)
??????????????? .getGoods_SpecialPrice());
??????? viewHolder.goodName.setText(list.get(arg0).getGoods_Name());
??????? viewHolder.price.setText(list.get(arg0).getGoods_Price());
??????? viewHolder.price.getPaint().setFlags(
??????????????? Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
??????? viewHolder.specialPrice.setText(list.get(arg0).getGoods_SpecialPrice());
??????? viewHolder.koukou.setText(off + "折");

??????? if (DateTools.isStart(list.get(arg0).getSpecialFrom())) {
??????????? //特賣倒計時開始
??????????? viewHolder.mTimeText.setTimes(DateTools.getDate(CorrectSpecialDataFormHttp
??????????????????? .correctData((list.get(arg0).getSpecialEnd()))));
??????????? //已經在倒計時的時候不再開啟計時
??????????? if(!viewHolder.mTimeText.isRun())
??????????? {
??????????????? viewHolder.mTimeText.run();
??????????? }
??????????? viewHolder.showDate_l.setVisibility(View.GONE);
??????????? viewHolder.showTime.setVisibility(View.VISIBLE);
??????? } else {
??????????? viewHolder.showTime.setVisibility(View.GONE);
??????????? viewHolder.showDate_l.setVisibility(View.VISIBLE);
??????????? viewHolder.showDate.setText(DateTools.getDay(list.get(arg0).getSpecialFrom())
??????????????????? + "");
??????? }

??????? ImageLoader.getInstance().displayImage(list.get(arg0).getGoods_Pic(),viewHolder.image);

??????? //停止計時
??????? long endTime = System.nanoTime();
??????? //耗時
??????? long spendTime = (endTime - startTime);

??????? sumTime += spendTime;
//??????? Log.d("GoogleIO", "position at:"+arg0+"--sumTime:"+String.valueOf(sumTime));
??????? return convertView;
??? }

??? public String getOff(String price, String specialPrice) {

??????? double off = Double.parseDouble(specialPrice)
??????????????? / Double.parseDouble(price) * 10;

??????? DecimalFormat df = new DecimalFormat("0.0");
??????? String off_String = df.format(off);

??????? if (off_String.equals("NaN") || off_String.equals("1")) {
??????????? off_String = "10";
??????? }
??????? return off_String;
??? }

??? static class ViewHolder {
??????? ImageView image;
??????? TextView goodName;
??????? TextView price;
??????? TextView specialPrice;
??????? TextView koukou;
??????? TimeTextView mTimeText;
??????? TextView showDate;
??????? LinearLayout showDate_l;
??????? LinearLayout showTime;

??? }

??? protected void resetViewHolder(ViewHolder viewHolder) {
??????? viewHolder.image.setImageBitmap(null);
??????? viewHolder.goodName.setText("");
??????? viewHolder.price.setText("");
??????? viewHolder.specialPrice.setText("");
??????? viewHolder.koukou.setText("");
??????? viewHolder.mTimeText.setText("");
??????? viewHolder.showDate.setText("");

??? }
}

layout使用代碼

復制代碼 代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/home_panicbuying_background"
android:orientation="vertical" >

<!-- 免單 -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >

<FrameLayout
android:id="@+id/index_temai_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp" >

<ImageView
android:id="@+id/index_temai_image"
android:layout_width="80dp"
android:layout_height="80dp" />

<ImageView
android:id="@+id/index_temai_discount_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:background="@drawable/app_limit_buy_sale"
android:src="@drawable/app_limit_buy_begin" />
</FrameLayout>

<LinearLayout
android:id="@+id/temai_date_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/index_temai_image_layout"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/temai_weikaishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="距離開始還有"
android:textColor="@color/black"
android:textSize="@dimen/small_text_size"
android:textStyle="bold" />

<TextView
android:id="@+id/index_temai_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="99"
android:textColor="@color/red"
android:textSize="@dimen/small_text_size"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天"
android:textColor="@color/black"
android:textSize="@dimen/small_text_size"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:id="@+id/temai_yikaishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal" >

<com.new0315.widgets.TimeTextView
android:id="@+id/temai_timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="@dimen/small_text_size"
/>

</LinearLayout>
</RelativeLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/temai_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:text="大眾甲殼蟲,豪華款,曾全套汽車配件,十年加油卡,車庫補貼,十年車險,五年以舊換新服務,比提供五年免費待架服務"
android:textColor="@color/black"
android:textSize="12sp" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/index_raw_price"
android:textColor="@color/darkgray"
android:textSize="@dimen/small_text_size" />

<TextView
android:id="@+id/temai_yuanjia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="@color/darkgray"
android:textSize="@dimen/small_text_size" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:background="@drawable/app_limit_buy_sale_bg"
android:gravity="center_vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="3dp"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="特賣價:"
android:textColor="#919263"
android:textSize="13sp" />

<TextView
android:id="@+id/temai_xiajia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5sp"
android:text="¥400"
android:textColor="@color/red"
android:textSize="13sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="折扣:"
android:textColor="#919263"
android:textSize="13sp" />

<TextView
android:id="@+id/temai_zhekou_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5sp"
android:text="5.0折"
android:textColor="@color/green"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

?

轉載于:https://www.cnblogs.com/Free-Thinker/p/3938451.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258253.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258253.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258253.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Spring Cloud構建微服務架構:服務消費(Ribbon)【Dalston版】

通過上一篇《Spring Cloud構建微服務架構&#xff1a;服務消費&#xff08;基礎&#xff09;》&#xff0c;我們已經學會如何通過LoadBalancerClient接口來獲取某個服務的具體實例&#xff0c;并根據實例信息來發起服務接口消費請求。但是這樣的做法需要我們手工的去編寫服務選…

檢測是否點擊到精靈

需要給每個精靈設置tag.可以用枚舉 bool GE::GamePass::ccTouchBegan( cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent ) { const int iButtonCount 2; const int iButtonTags[iButtonCount] { GamePass_btn_share, GamePass_btn_return }; for(int i 0; i < iButt…

從gitlab上拉代碼_從gitlab上拉取代碼并一鍵部署

一、gitlab安裝GitLab是一個利用Ruby on Rails開發的開源應用程序&#xff0c;實現一個自托管的Git項目倉庫&#xff0c;可通過Web界面進行訪問公開的或者私人項目。GitLab擁有與Github類似的功能&#xff0c;能夠瀏覽源代碼&#xff0c;管理缺陷和注釋。可以管理團隊對倉庫的訪…

LPWA技術:發展物聯網的最佳選擇

物聯網時代的物物相連將會使百億以上物體連入網絡&#xff0c;這對傳統上的兩種通信技術&#xff0c;即近距離無線接入和移動蜂窩網提出了更高的要求。事實上&#xff0c;目前&#xff0c;用于物聯網發展的通信技術正在全球范圍內開發&#xff0c;低功耗廣域網通信技術(Low Pow…

上傳文件大小限制,webconfig和IIS配置大文件上傳

IIS6下上傳大文件沒有問題&#xff0c;但是遷移到IIS7下面&#xff0c;上傳大文件時&#xff0c;出現HTTP 404錯誤。 IIS配置上傳大小&#xff0c;webconfig <!-- 配置允許上傳大小 --><httpRuntime maxRequestLength"1997151" useFullyQualifiedRedirectU…

產品管理流程

轉載于:https://www.cnblogs.com/candle806/p/4860841.html

如何根據灰度直方圖計算標準差_如何根據電器功率計算電線的粗細?

一般來說&#xff0c;測算電線的粗細&#xff0c;需要根據功率計算電流&#xff0c;根據電流選擇導線截面&#xff0c;根據導線的截面&#xff0c;導線或電纜的型號查廠家的該型號的導線電纜的直徑。這里就涉及了&#xff1a;電線粗細與功率之間的關系計算&#xff1b;導線截面…

解惑煙草行業工控系統如何風險評估

上周五下午&#xff0c;威努特工控安全聯合創始人 趙宇 先生&#xff0c;帶來了一場關于“工控系統的風險評估”的技術講座。此次近200注冊報名的朋友&#xff0c;來自各大高校、國企、外企、測評中心、安全廠商、大型集成商以及大型IT科技企業、安全實驗室等。 煙草企業調研參…

ORACLE union order by

select * from ( select a.id,a.oacode,a.custid,a.custname,a.xsz,a.salename,a.communicationtheme,a.communicationproperty,a.communicationtime,a.productmanager,a.creator,a.createdate from technology_flow a where a.oastate正常結束 union select b.id,b.oacode,b…

UVa 11806 Cheerleaders

題意&#xff1a;m行n列的矩形網格放k個相同的石子&#xff0c;要求第一行最后一行第一列最后一列都必須有石子&#xff0c;問有多少種放法 A為第一行沒有石子的方案數&#xff0c;BCD依此類推&#xff0c;全集為S 如果沒有任何要求的話&#xff0c;放法數應該是C(rc, k) 解法中…

為什么說一站式移動辦公SaaS平臺一定是未來!

摘要&#xff1a;移動辦公SaaS之間的核心競爭不在于比拼技術&#xff0c;而在于誰更好地與企業管理和文化相互融合&#xff0c;給企業帶來更加年輕、更加高效的工作方式&#xff0c;實現了企業組織的互聯網化。 沒有哪個企業愿意當諾基亞&#xff0c;“并沒有做錯什么&#xff…

server sql 將出生日期轉為年齡_在sql server表中有一個出生日期字段我怎么才能在當前年份改變時自動更新年齡字段...

先說明下identity(1,1)&#xff1a;自動1foreign key 外鍵語法create database ztxuse ztxCreate Table QAUser--baidu用戶資料(Id int Primary Key not null identity(1,1),--自動編號,也同時用于對用戶的標示符QA_name varchar(20),--用戶名Sex char(2),--或者使用bit類型,但…

MySQL關聯left join 條件on與where不同

以下的文章主要講述的是MySQL關聯left join 條件on與where 條件的不同之處&#xff0c;我們現在有兩個表&#xff0c;即商品表(products)與sales_detail(銷售記錄表)。我們主要是通過這兩個表來對MySQL關聯left join 條件on與where 條件的不同之處進行講述。 products: pid pna…

自動裁剪圖片

自動裁剪商品圖片View Code執行裁剪指定目錄商品圖片動作///<summary> ///執行指定目錄商品圖片動作 ///</summary> public static void FindPictureDoCutIt(object o) {string filePatho.ToString();try{DirectioryInfo fatherFolder new DirectioryInfo(filePat…

32位oracle_oracle 性能調優

pool&#xff0c;sga&#xff0c;pga的配置 物理內存16G在調整SGA前&#xff0c;先看下服務器操作系統是32位還是64位的&#xff0c;如果是32位的&#xff0c;則SGA最大不能超過1.7G&#xff0c;如果是64位的&#xff0c;則不能超過4G。基本分配原則&#xff0c;db_block_buffe…

看網絡電子圍欄如何做好周界安防

圍欄是為了保護一定范圍內的任何物遭到侵害而設立的一個屏障&#xff0c;在一定程度上有保護的作用&#xff0c;但是也不能完全阻止。傳統的圍欄以加高或者添加危險觸碰物來增加安全性&#xff0c;但是會影響美觀&#xff0c;不能進行主動擊退&#xff0c;也給圍欄內人物帶來不…

Objective-C語法之代碼塊(block)的使用

代碼塊本質上是和其它變量相似。不同的是&#xff0c;代碼塊存儲的數據是一個函數體。使用代碼塊是&#xff0c;你能夠像調用其它標準函數一樣&#xff0c;傳入參數數&#xff0c;并得到返回值。脫字符&#xff08;^&#xff09;是塊的語法標記。依照我們熟悉的參數語法規約所定…

C#委托和事件

http://www.cnblogs.com/leslies2/archive/2012/03/22/2389318.html 講解比較好 轉載于:https://www.cnblogs.com/sun-shadow/p/4872768.html

asp.net mvc使用mysql_ASP.NET開發實戰——(八)ASP.NET MVC 與數據庫之MySQL

之前介紹了My Blog如何使用http://ADO.NET來訪問SQL Server獲取數據。本章將介紹如何使用My SQL來完成數據管理。在使用My SQL之前需確保開發環境中安裝了My SQL數據庫和Connector/Net&#xff0c;后者是一個用C#編寫的http://ADO.NET數據提供器&#xff0c;換句話說無論使用SQ…

多元時代個人信息更需強有力保護

有網友反映&#xff0c;用多個搜索引擎搜索“手持身份證照片”&#xff0c;皆出現大量相關圖片&#xff0c;人臉清晰&#xff0c;身份證號碼等關鍵信息明白無誤。不少網友擔心“這么重要的信息就這么暴露&#xff0c;太危險”。記者發現&#xff0c;其中有弱勢群體求助信息&…