返回

福州达内教育

Android自定义View实例AnalogClock源码

软件测试学习网更新时间:2021-10-14 浏览:171

对于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提示网民的是很有可能针对充电电池,及其系统软件运作效 率造成一定的危害,但是做为训练大伙儿能够试一试。

相关资讯

iOS让键盘消失的技巧
2021-07-15 316
聚焦点在UITextField或UITextView中的情况下,电脑键盘会发生,想点一下情况就要电脑键盘消退。它是在手执机器设备上服合客户的实际操作习惯性。自然方式 有很多,在这儿我讲一个较为好用的方式...
Android开发之--脚本编程
2021-10-06 264
免费下载和安裝 Scripting Layer for Android (SL4A)Scripting Layer for Android (SL4A) 是一个开源软件,总体目标是为android系统软件给予开发语言的适用...
参加云计算培训就业前景怎么样
2021-10-20 246
云技术不但不容易变弱其发展趋势优点,反过来,在5G技术性的推动下,云计算技术发展前景也会变的愈来愈丰厚。下面,就要达内教育教师为我们深入分析一下,将来云计算技术的发展趋向...
软件测试工程师35岁以后怎么办
2021-06-17 243
35岁干不动,都只是借口而已。能干的,依然能够很好的在这个行业发展,干不了的,注定会被一浪比一浪高的后浪,拍死在沙滩上。我...
Android私有文件夹文件的写入与读取
2021-07-15 238
*给大伙儿详细介绍应用文档怎样对数据信息开展储存,Activity给予了openFileOutput()方式 能够用以把数据信息輸出到文档中...
软件测试工程师的职业发展方向有哪些
2021-06-15 236
在我们开始尝试为大家描述软件测试工作的多种可能性之前,先来看看在现在所知最近代的开发模式中,测试人员还会继续存在吗?很多...
浅谈软件测试流程
2021-10-12 229
一般而言,自动化测试从新项目建立时就开始了,前后左右要历经下列一些关键阶段:需求分析报告→测试流程→检测设计方案→接口测试构建→检测实行→检测纪录→缺陷管理→手机软件评定→RTM...
几种测试工作量的估算方法
2021-10-11 220
在检测项目风险管理中或撰写测试流程时,常常*对某一检测工作中开展劳动量的费用预算,许多情况下全是凭本人的工作经历开展估计的,如能融合一些基本的估计方式 ,有利于估计的精准度...

相关课程

栏目导航