android 一分钟倒计时动画,Android自定义view倒计时60秒

public class TimerTextView extends LinearLayout{

// 时间变量

private long second;

private TextView tv_Time;

private TextView tv_Unit;

RefreshCallBack refreshCallBack;

public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initView(context);

}

public TimerTextView(Context context, AttributeSet attrs) {

super(context, attrs);

initView(context);

}

public TimerTextView(Context context) {

super(context);

initView(context);

}

private void initView(Context context) {

// 加载布局

LayoutInflater.from(context).inflate(R.layout.timer_text_view, this);

tv_Time = (TextView) findViewById(R.id.countdown_time);

tv_Unit = (TextView) findViewById(R.id.countdown_unit);

}

@Override

protected void onDetachedFromWindow() {

super.onDetachedFromWindow();

// 在控件被销毁时移除消息

handler.removeMessages(0);

}

private boolean isRun = true; // 是否启动了

private Handler handler = new Handler(Looper.getMainLooper()) {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case 0:

if (isRun) {

if (second > 0) {

second = second - 1;

handler.sendEmptyMessageDelayed(0, 1000);

}else{

if(null != refreshCallBack){

refreshCallBack.refreshCallBack(true);

isRun = false;

}

}

}

break;

}

}

};

public boolean isRun() {

return isRun;

}

/**

* 开始计时

*/

public void start() {

isRun = true;

handler.removeMessages(0);

handler.sendEmptyMessage(0);

}

/**

* 结束计时

*/

public void stop() {

isRun = false;

}

public void diffTime(String endTime) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

String startTime = sdf.format(new Date());

String format = "yyyy-MM-dd hh:mm:ss";

//按照传入的格式生成一个simpledateformate对象

SimpleDateFormat sd = new SimpleDateFormat(format);

long nd = 1000 * 24 * 60 * 60;//一天的毫秒数

long nh = 1000 * 60 * 60;//一小时的毫秒数

long nm = 1000 * 60;//一分钟的毫秒数

long ns = 1000;//一秒钟的毫秒数long diff;try {

//获得两个时间的毫秒时间差异

long diff = 0;

try {

diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();

} catch (ParseException e) {

e.printStackTrace();

}

if (diff < 0) {

if(null != refreshCallBack){

refreshCallBack.showCallBack(false);

}

return ;

} else {

if(null != refreshCallBack){

refreshCallBack.showCallBack(true);

}

long day = diff / nd;//计算差多少天

if (day > 0) {

tv_Time.setText(String.valueOf(day));

tv_Unit.setText("天");

} else {

long hour = diff % nd / nh;//计算差多少小时

if (hour > 0) {

tv_Time.setText(String.valueOf(hour));

tv_Unit.setText("小时");

} else {

long min = diff % nd % nh / nm;//计算差多少分钟

if (min > 0) {

tv_Time.setText(String.valueOf(min));

tv_Unit.setText("分钟");

} else {

second = diff%nd%nh%nm/ns;//计算差多少秒//输出结果

// if(min > 0){

// stringBuffer.append(sec+"秒");

// }

handler.removeMessages(0);

handler.sendEmptyMessage(0);

tv_Unit.setText("即将开始");

tv_Time.setVisibility(GONE);

}

}

}

}

}

public void setTextViewSize(int size){

if(null != tv_Time){

tv_Time.setTextSize(size);

}

if(null != tv_Unit){

tv_Unit.setTextSize(size);

}

}

public void setTextViewSpace(String type){

if("Big".equals(type)){

LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();

lp2.setMargins(0, 0, DensityUtil.dip2px(tv_Time.getContext(), 12), 0);

tv_Time.setLayoutParams(lp2);

tv_Time.setBackground(getResources().getDrawable(R.drawable.bg_video_count_down));

}else if("Middle".equals(type)){

tv_Time.setPadding(12, 0, 12, 0);

LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();

lp2.setMargins(0, 0,12, 0);

tv_Time.setLayoutParams(lp2);

}else {

tv_Time.setPadding(8, 0, 8, 0);

LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();

lp2.setMargins(0, 0, 8, 0);

tv_Time.setLayoutParams(lp2);

}

}

public void setRefreshCallBack(RefreshCallBack refreshCallBack){

this.refreshCallBack = refreshCallBack;

}

public interface RefreshCallBack {

public void refreshCallBack(boolean flag);

public void showCallBack(boolean flag);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现倒计时圆形进度,你可以创建一个自定义View,使用 Canvas 和 Paint 来绘制圆形和进度条。以下是一个示例代码: ```java public class CountdownCircleView extends View { private int maxProgress = 100; // 总进度 private int progress = 100; // 当前进度 private int circleColor = Color.GRAY; // 圆形颜色 private int progressColor = Color.BLUE; // 进度条颜色 private Paint circlePaint; private Paint progressPaint; public CountdownCircleView(Context context) { super(context); init(); } public CountdownCircleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public CountdownCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { circlePaint = new Paint(); circlePaint.setAntiAlias(true); circlePaint.setColor(circleColor); progressPaint = new Paint(); progressPaint.setAntiAlias(true); progressPaint.setColor(progressColor); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int viewWidth = getWidth(); int viewHeight = getHeight(); // 绘制圆形 int diameter = Math.min(viewWidth, viewHeight); int radius = diameter / 2; int centerX = viewWidth / 2; int centerY = viewHeight / 2; canvas.drawCircle(centerX, centerY, radius, circlePaint); // 绘制进度条 RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius); float sweepAngle = 360f * progress / maxProgress; canvas.drawArc(rectF, -90, sweepAngle, true, progressPaint); } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } public void setProgress(int progress) { this.progress = progress; invalidate(); // 重新绘制 } } ``` 你可以将上述代码放入你的 Android 项目中,并在布局文件中使用 `CountdownCircleView`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值