Android second kill countdown custom TextView

Source: Internet
Author: User

Custom TextView control TimeTextView code: copy the code 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;/*** custom countdown text control * @ author Administrator **/public class TimeTextView extends TextView implements Runnable {Paint mPaint; // Paint brush, it contains the style and color information of the geometric image and text. private l Ong [] times; private long mday, mhour, mmin, msecond; // day, hour, minute, Second private boolean run = false; // whether public TimeTextView (Context context, AttributeSet attrs) {super (context, attrs); mPaint = new Paint (); TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. timeTextView); array. recycle (); // It must be called. Otherwise, this setting will affect the next use.} public TimeTextView (Context context, AttributeSet attrs, int defStyl E) {super (context, attrs, defStyle); mPaint = new Paint (); TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. timeTextView); array. recycle (); // It must be called. Otherwise, this setting will affect the next use.} 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]; mse Cond = times [3];}/*** countdown calculation */private void ComputeTime () {msecond --; if (msecond <0) {mmin --; msecond = 59; if (mmin <0) {mmin = 59; mhour --; if (mhour <0) {// The countdown ends mhour = 59; mday --;}}}} public boolean isRun () {return run;} public void setRun (boolean run) {this. run = run ;}@ Override public void run () {// indicates that run has been started = true; ComputeTime (); string strTime = "Left </pre> <span style =" col Or: red; ">" + mday + "</span> <pre>" + "day </pre> <span style =" color: red; ">" + mhour + "</span> <pre> hour </pre> <span style =" color: red; ">" + mmin + "</span> <pre> minutes </pre> <span style =" color: red; ">" + msecond + "</span> <pre> seconds"; this. setText (Html. fromHtml (strTime); postDelayed (this, 1000) ;}} copy the code attribute atts. xml <declare-styleable name = "TimeTextView"> </declare-styleable> Adapter call code: copy the code import java. text. decimalFormat; import j Ava. 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. new031 5. utils. correctSpecialDataFormHttp; import com. new0315.utils. datemedils; 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) {// start timing, performance testing with nanoTime will be more accurate, because it is a nanosecond-level 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. tem Ai_yuanjia_text); viewHolder. specialPrice = (TextView) convertView. findViewById (R. id. temai_xiajia_text); // sale countdown control 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 = (L InearLayout) 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 (viewHo Lder);} // 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 (); v IewHolder. koukou. setText (off + ""); if (datatils. isStart (list. get (arg0 ). getSpecialFrom () {// The sale countdown begins with viewHolder. mTimeText. setTimes (datatils. 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 (datemedils. getDay (list. get (arg0 ). getSpecialFrom () + "");} ImageLoader. getInstance (). displayImage (list. get (Rg0 ). getGoods_Pic (), viewHolder. image); // stop timing long endTime = System. nanoTime (); // time consumed 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 values; TimeTextView mTimeText; TextView showDate; LinearLayout showDate_l; LinearLayout showTime;} protected void resetVi EwHolder (ViewHolder viewHolder) {viewHolder. image. setImageBitmap (null); viewHolder. goodName. setText (""); viewHolder. price. setText (""); viewHolder. specialPrice. setText (""); viewHolder. koukou. setText (""); viewHolder. mTimeText. setText (""); viewHolder. showDate. setText ("") ;}} copy code layout use code to copy Code <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" Ndroid: layout_height = "wrap_content" android: background = "@ drawable/home_panicbuying_background" android: orientation = "vertical"> <! -- Free --> <RelativeLayoutandroid: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp"> <FrameLayoutandroid: id = "@ + id/index_temai_image_layout" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: layout_margin = "5dp"> <ImageViewandroid: id = "@ + id/index_temai_image" android: layout_width = "80dp" android: layout_height = "80dp"/> <ImageViewandroid: id = "@ + id/index_temai_discount_icon" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "top | left" android: background = "@ drawable/resize" android: src = "@ drawable/app_limit_buy_begin"/> </FrameLayout> <LinearLayoutandroid: id = "@ + id/temai_date_show" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "@ id/fill" android: orientation = "vertical"> <RelativeLayoutandroid: layout_width = "wrap_content" android: layout_height = "wrap_content"> <LinearLayoutandroid: id = "@ + id/temai_weikaishi" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "2dp" android: orientation = "horizontal"> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "android: textColor = "@ color/black" android: textSize = "@ dimen/small_text_size" android: textStyle = "bold"/> <TextViewandroid: 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"/> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "day" android: textColor = "@ color/black" android: textSize = "@ dimen/small_text_size" android: textStyle = "bold"/> </LinearLayout> <LinearLayoutandroid: id = "@ + id/temai_yikaishi" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "2dp" android: orientation = "horizontal"> <com. new0315.widgets. timeTextViewandroid: 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> <LinearLayoutandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginBottom = "5dp" android: layout_marginRight = "20dp" android: layout_marginTop = "5dp" android: orientation = "horizontal"> <TextViewandroid: id = "@ + id/temai_Name" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: lines = "2" android: text = "Volkswagen Beetle, deluxe, zeng has a full set of auto accessories, ten years of fuel cards, garage subsidies, ten years of auto insurance, five years of old-for-new services, compared to the five-year free waiting service "android: textColor = "@ color/black" android: textSize = "12sp"/> </LinearLayout> <LinearLayoutandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal"> <TextViewandroid: 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"/> <TextViewandroid: 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/users"/> </LinearLayout> </RelativeLayout> <LinearLayoutandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_margin = "5sp" android: background = "@ drawable/resize" android: gravity = "weight"> <LinearLayoutandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "30dp" android: layout_marginTop = "3dp" android: orientation = "horizontal"> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "sale price:" android: textColor = "#919263" android: textSize = "13sp"/> <TextViewandroid: 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"/> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "discount: "android: textColor =" #919263 "android: textSize =" 13sp "/> <TextViewandroid: 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 off" android: textColor = "@ color/green" android: textSize = "13sp"/> </LinearLayout> copy the code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.