• 手机站
  • 收藏
聚培教育网合作机构 > 福州达内教育
福州达内教育
400-998-6158
福州达内教育以打造符合企业需求的实战型人才为目标,结合多种就业渠道,将学员就业视为己任。
福州达内教育

Android自定义View实例AnalogClock源码

软件测试学习网

更新时间:2021-10-14 浏览:171
核心提示:对于Android**层View的立即结构许多网民沒有实践经验,此次Android开发设计网融合现阶段服务平台开放源码一起根据AnalogClock类 来了解View的立即承继

对于Android**层View的立即结构许多网民沒有实践经验,此次Android开发设计网融合现阶段服务平台开放源码一起根据AnalogClock类 来了解View的立即承继。AnalogClock便是Home Screen上的那一个含有二根表针的表壳类。它的完成大家立即从开放源码能够掌握到:

public class AnalogClock extends View {

private Time mCalendar;

private Drawable mHourHand; //秒针

private Drawable mMinuteHand; //分针

private Drawable mDial; //表壳情况

private int mDialWidth; //表壳总宽

private int mDialHeight; //表壳高宽比

private boolean mAttached; //粘附情况

private final Handler mHandler = new Handler(); //定一个Handler类完成更新

private float mMinutes;

private float mHour;

private boolean mChanged; //時间是不是更改

public AnalogClock(Context context) {

this(context, null);

}

public AnalogClock(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public AnalogClock(Context context, AttributeSet attrs,

int defStyle) {

super(context, attrs, defStyle);

Resources r = mContext.getResources();

TypedArray a =

context.obtainStyledAttributes(

attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);

mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial); //载入表壳資源

if (mDial == null) {

mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);

}

mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour); //载入时针图片資源

if (mHourHand == null) {

mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);

}

mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute); //载入分针照片

if (mMinuteHand == null) {

mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);

}

mCalendar = new Time(); //获得当今时间格式

mDialWidth = mDial.getIntrinsicWidth(); //获得表盘图片的总宽

mDialHeight = mDial.getIntrinsicHeight(); //高宽比,跟上面一样

}

@Override

protected void onAttachedToWindow() {

super.onAttachedToWindow();

if (!mAttached) {

mAttached = true;

IntentFilter filter = new IntentFilter(); //申请注册一个信息过滤装置,获得時间更改、时区时间更改的action

filter.addAction(Intent.ACTION_TIME_TICK);

filter.addAction(Intent.ACTION_TIME_CHANGED);

filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);

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

}

mCalendar = new Time();

onTimeChanged();

}

@Override

protected void onDetachedFromWindow() {

super.onDetachedFromWindow();

if (mAttached) {

getContext().unregisterReceiver(mIntentReceiver); //反申请注册信息过滤装置

mAttached = false;

}

}

@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;

}

关键的制图调用View的onDraw方式 ,我们可以见到根据canvas案例立即显示屏

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

boolean changed = mChanged;

if (changed) {

mChanged = false;

}

int availableWidth = mRight - mLeft;

int availableHeight = mBottom - mTop;

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); //测算秒针转动的视角,android123提醒就是那个时针图片的转动视角,立即反映的便是表壳上那一个针的時间

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();

if (scaled) {

canvas.restore();

}

}

private void onTimeChanged() { //获得時间更改,测算当今的分秒

mCalendar.setToNow();

int hour = mCalendar.hour;

int minute = mCalendar.minute;

int second = mCalendar.second;

mMinutes = minute second / 60.0f;

mHour = hour mMinutes / 60.0f;

mChanged = true;

}

private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { //监视获得時间更改action

@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(); //更新显示屏,强制性类启用onDraw方式 完成分针秒针的行走

}

};

看过本例依据,Android开发设计非常简单吧,有兴趣的网民能够为本程序流程添加一个表针,但是Android123提示网民的是很有可能针对充电电池,及其系统软件运作效 率造成一定的危害,但是做为训练大伙儿能够试一试。

更多>同类资讯
更多>相关课程
顶部