自定义控件(选择模拟时钟进行重写)

[img]http://dl.iteye.com/upload/attachment/299720/54558aac-d649-3ee3-81ce-a6d426f5d2e2.png[/img]
主要是为了学习自定义控件,如何添加属性以及修改方法等等。
为模拟时钟添加了秒针走动。
效果如附件。
package com.able.widget;


import java.util.TimeZone;

import com.able.test.R;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.util.AttributeSet;

import android.view.View;


public class AbleAnalogClock extends View
{
private Time mCalendar;

private Drawable mHourHand;//时针
private Drawable mMinuteHand;//分针
private Drawable mDial;//表盘
private Drawable mSecondHand;//秒针

private int mDialWidth;//表盘宽度
private int mDialHeight;//表盘高度

private boolean mAttached;

private final Handler mHandler = new Handler();
private float mMinutes;
private float mHour;
private float mSecond;
private boolean mChanged;
private final int UPDATEBYMIN=111;
public AbleAnalogClock(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public AbleAnalogClock(Context context, AttributeSet attrs) {
super(context, attrs);
try
{
Resources r=context.getResources();//
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.AbleAnalogClock);
this.mSecondHand=a.getDrawable(R.styleable.AbleAnalogClock_second);//属性栏的值
if(mSecondHand==null)
{
mSecondHand=r.getDrawable(R.drawable.clock_hand_second);//无用户输入时的默认值
}


mDial = a.getDrawable(R.styleable.AbleAnalogClock_face);
if (mDial == null) {
mDial = r.getDrawable(R.drawable.clock_dial);
}

mHourHand = a.getDrawable(R.styleable.AbleAnalogClock_hour);
if (mHourHand == null) {
mHourHand = r.getDrawable(R.drawable.clock_hand_hour);
}

mMinuteHand = a.getDrawable(R.styleable.AbleAnalogClock_minute);
if (mMinuteHand == null) {
mMinuteHand = r.getDrawable(R.drawable.clock_hand_minute);
}

mCalendar = new Time();

mDialWidth = mDial.getIntrinsicWidth();
mDialHeight = mDial.getIntrinsicHeight();
a.recycle();
}
catch(Exception e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(UPDATEBYMIN);
}
public AbleAnalogClock(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();

filter.addAction(Intent.ACTION_TIME_TICK);//send every min
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);

getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
}

// NOTE: It's safe to do these after registering the receiver since the receiver always runs
// in the main thread, therefore the receiver can't run before this method returns.

// The time zone may have changed while the receiver wasn't registered, so update the Time
mCalendar = new Time();

// Make sure we update to the current time
onTimeChanged();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAttached) {
getContext().unregisterReceiver(mIntentReceiver);
mAttached = false;
handler.removeMessages(UPDATEBYMIN);
}
}



@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);

float hScale = 1.0f;
float vScale = 1.0f;

if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
hScale = (float) widthSize / (float) mDialWidth;
}

if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
vScale = (float )heightSize / (float) mDialHeight;
}

float scale = Math.min(hScale, vScale);

setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

boolean changed = mChanged;
if (changed) {
mChanged = false;
}

int availableWidth = mDial.getIntrinsicWidth();
int availableHeight = mDial.getIntrinsicHeight();


int x = availableWidth / 2;
int y = availableHeight / 2;

final Drawable dial = mDial;
int w = dial.getIntrinsicWidth();
int h = dial.getIntrinsicHeight();

boolean scaled = false;

if (availableWidth < w || availableHeight < h) {
scaled = true;
float scale = Math.min((float) availableWidth / (float) w,
(float) availableHeight / (float) h);
canvas.save();
canvas.scale(scale, scale, x, y);
}

if (changed) {
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
dial.draw(canvas);

canvas.save();
canvas.rotate(mHour / 12.0f * 360.0f, x, y);


final Drawable hourHand = mHourHand;
if (changed) {
w = hourHand.getIntrinsicWidth();
h = hourHand.getIntrinsicHeight();
hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
hourHand.draw(canvas);
canvas.restore();

canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);


final Drawable minuteHand = mMinuteHand;
if (changed) {
w = minuteHand.getIntrinsicWidth();
h = minuteHand.getIntrinsicHeight();
minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
minuteHand.draw(canvas);
canvas.restore();
canvas.save();
canvas.rotate(mSecond*6.0f, x, y);

final Drawable secondHand = mSecondHand;
if (changed) {
w = secondHand.getIntrinsicWidth();
h = secondHand.getIntrinsicHeight();
secondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
secondHand.draw(canvas);
canvas.restore();


if (scaled) {
canvas.restore();
}
}

private Context getApplicationContext() {

return null;
}
private void onTimeChanged() {
mCalendar.setToNow();

int hour = mCalendar.hour;
int minute = mCalendar.minute;
int second = mCalendar.second;
mSecond=second;
mMinutes = minute + second / 60.0f;
mHour = hour + mMinutes / 60.0f;
mChanged = true;
}

private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
String tz = intent.getStringExtra("time-zone");
mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
}

onTimeChanged();

invalidate();

}
};
Handler handler=new Handler()
{

@Override
public void handleMessage(Message msg) {

switch(msg.what)
{
case UPDATEBYMIN:
onTimeChanged();
invalidate();

sendEmptyMessageDelayed(UPDATEBYMIN,1000);
break;

}
super.handleMessage(msg);
}

};

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值