时间:2021-05-20
自定义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>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能。首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:activ
本文实例为大家分享了android自定义圆形倒计时显示控件的具体代码,供大家参考,具体内容如下先上效果图-倒计时结束代码块attr.xml控件需要用到的属性:C
自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:两个TextView是用来显示提示信息和倒计时的秒数的相关教程:Android开发
WSDrawCircleProgress,根据UIBezierPath和CAShapeLayer自定义倒计时进度条,适用于app启动的时候设置一个倒计时关闭启动
本文实例为大家分享了DigitalClock实现商品倒计时的具体代码,供大家参考,具体内容如下自定义DigitalClock控件:packagecom.veal