Android自定义视图,将图像定位在模拟时钟的中心
我正在创建一个带有小时、秒、分针图像的自定义模拟时钟。时钟工作正常,但唯一的问题是指针的固定。下面是所附时钟的图像。时针、分针被固定在中心,这使时钟看起来很糟糕。所有的手都应该固定在边缘,这样看起来更真实和可读。有人能提出一些建议吗。我的自定义视图附在这里Android自定义视图,将图像定位在模拟时钟的中心,android,android-custom-view,clock,Android,Android Custom View,Clock,我正在创建一个带有小时、秒、分针图像的自定义模拟时钟。时钟工作正常,但唯一的问题是指针的固定。下面是所附时钟的图像。时针、分针被固定在中心,这使时钟看起来很糟糕。所有的手都应该固定在边缘,这样看起来更真实和可读。有人能提出一些建议吗。我的自定义视图附在这里 package com.example.submission_customclock; import android.annotation.TargetApi; import android.content.Context; import
package com.example.submission_customclock;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews.RemoteView;
import java.util.TimeZone;
/**
* This widget display an analogic clock with two hands for hours and
* minutes.
*
* @attr ref android.R.styleable#AnalogClock_dial
* @attr ref android.R.styleable#AnalogClock_hand_hour
* @attr ref android.R.styleable#AnalogClock_hand_minute
*/
@RemoteView
public class AnalogClock extends View {
private Time mCalendar;
private static final String DEBUGTAG = "FA";
private Drawable mHourHand;
private Drawable mMinuteHand;
private Drawable mSecondHand;
private Drawable mDial;
private Drawable mDial_frame;
private int mDialWidth;
private int mDialHeight;
private boolean mAttached;
private final Handler mHandler = new 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);
}
Context mContext;
public AnalogClock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = context.getResources();
mContext=context;
Log.d(AnalogClock.DEBUGTAG,"Analog clock started");
mDial = r.getDrawable(R.drawable.clock4);
mDial_frame = r.getDrawable(R.drawable.clock_frame);
mHourHand = r.getDrawable(R.drawable.hour_hand);
mMinuteHand = r.getDrawable(R.drawable.minute_hand);
mSecondHand = r.getDrawable(R.drawable.second_hand);
mCalendar = new Time();
mDialWidth = mDial.getIntrinsicWidth();
mDialHeight = mDial.getIntrinsicHeight();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
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();
counter.start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAttached) {
counter.cancel();
getContext().unregisterReceiver(mIntentReceiver);
mAttached = false;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@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);
Log.d(AnalogClock.DEBUGTAG,"onMeasure params: " + widthSize + " "
+ heightSize + " " + hScale + " " + vScale + " " + scale);
try {
setMeasuredDimension(resolveSizeAndState((int) (mDialWidth * scale), widthMeasureSpec, 0),
resolveSizeAndState((int) (mDialHeight * scale), heightMeasureSpec, 0));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
}
boolean mSeconds=false;
float mSecond=0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
boolean changed = mChanged;
if (changed) {
mChanged = false;
}
boolean seconds = mSeconds;
if (seconds ) {
mSeconds = false;
}
int availableWidth = this.getMeasuredWidth();
int availableHeight = this.getMeasuredHeight();
int x = availableWidth / 2;
int y = availableHeight / 2;
final Drawable dial = mDial;
final Drawable dial_frame = mDial_frame;
int w = dial.getIntrinsicWidth();
int h = dial.getIntrinsicHeight();
boolean scaled = false;
// Log.d(AnalogClock.DEBUGTAG,"onDraw params: " + availableWidth +" "+ availableHeight + " " +
// x + " " + y + " " + w + " "+ h + " " + changed);
if (availableWidth < w || availableHeight < h) {
scaled = true;
//float scale = Math.min((float) availableWidth / (float) w,
// (float) availableHeight / (float) h);
canvas.save();
float scale1 = (float) 0.6;
float scale2 = (float) 0.8;
// Log.d(AnalogClock.DEBUGTAG,"scale params: " + scale1 + " " + scale2);
canvas.scale(scale1, scale2, x, y);
}
if (changed) {
//Log.d(AnalogClock.DEBUGTAG,"Bounds params: " + (x - (w / 2)) + " " + (y - (h / 2)) + " " + ( x + (w / 2)) + " " + (y + (h / 2)));
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
//dial_frame.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
//Log.d(AnalogClock.DEBUGTAG,"Bounds params: " + (x - (w / 2 + w/10)) + " " + (y - (h / 2 + h/10)) + " " + ( x + (w / 2 + w/10)) + " " +
// (y + (h / 2 + h/10)));
dial_frame.setBounds(x - (w/2 + w/10), y - (h/2 + h/10), x + (w/2 + w/10), y + (h/2 + h/10));
}
dial.draw(canvas);
dial_frame.draw(canvas);
canvas.save();
canvas.rotate(mHour / 12.0f * 180.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 * 180.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, x, y);
if (seconds) {
w = mSecondHand.getIntrinsicWidth();
h = mSecondHand.getIntrinsicHeight();
mSecondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
mSecondHand.draw(canvas);
canvas.restore();
if (scaled) {
canvas.restore();
}
}
MyCount counter = new MyCount(10000, 1000);
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter.start();
}
@Override
public void onTick(long millisUntilFinished) {
mCalendar.setToNow();
int second = mCalendar.second;
mSecond=6.0f*second;
mSeconds=true;
//mChanged = true;
AnalogClock.this.invalidate();
}
}
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() {
@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();
}
};
}
package com.example.submission\u customclock;
导入android.annotation.TargetApi;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.content.BroadcastReceiver;
导入android.content.res.Resources;
导入android.graphics.Canvas;
导入android.graphics.drawable.drawable;
导入android.os.Build;
导入android.os.CountDownTimer;
导入android.os.Handler;
导入android.text.format.Time;
导入android.util.AttributeSet;
导入android.util.Log;
导入android.view.view;
导入android.widget.RemoteView.RemoteView;
导入java.util.TimeZone;
/**
*这个小部件显示一个模拟时钟,两手分别显示小时和小时
*分钟。
*
*@attr ref android.R.styleable#模拟时钟
*@attr ref android.R.styleable#模拟时钟_指针_小时
*@attr ref android.R.styleable#模拟时钟(手动)分钟
*/
@远程视图
公共类模拟时钟扩展视图{
私人时间麦卡伦达;
私有静态最终字符串DEBUGTAG=“FA”;
私人可拉手;
私人可抽出式机头;
私人提款权;
私人可提取mDial;
专用可拉伸mDial_框架;
私有int-mDialWidth;
私人室内高度;
私有布尔mAttached;
私有最终处理程序mHandler=新处理程序();
私人浮点数;
私家车;
私有布尔值改变;
公共模拟时钟(上下文){
这个(上下文,空);
}
公共模拟时钟(上下文、属性集属性){
这(上下文,属性,0);
}
语境;
公共模拟时钟(上下文、属性集属性、,
int defStyle){
超级(上下文、属性、定义样式);
Resources r=context.getResources();
mContext=上下文;
Log.d(AnalogClock.DEBUGTAG,“模拟时钟启动”);
mDial=r.getDrawable(r.drawable.clock4);
mDial_帧=r.getDrawable(r.drawable.clock_帧);
mHourHand=r.getDrawable(r.drawable.hour\u-hand);
mMinuteHand=r.getDrawable(r.drawable.minutehand);
mSecondHand=r.getDrawable(r.drawable.second_hand);
mCalendar=新时间();
mDialWidth=mDial.getIntrinsicWidth();
mDialHeight=mDial.getIntrinsicHeight();
}
@凌驾
受保护的无效数据附加到DOWINDOW(){
super.onAttachedToWindow();
如果(!mAttached){
mAttached=真;
IntentFilter=newintentfilter();
filter.addAction(Intent.ACTION\u TIME\u TICK);
filter.addAction(Intent.ACTION\u TIME\u已更改);
filter.addAction(Intent.ACTION\u时区\u已更改);
getContext().registerReceiver(minentReceiver,filter,null,mHandler);
}
//注意:在注册接收器后执行这些操作是安全的,因为接收器始终运行
//在主线程中,因此在该方法返回之前,接收器不能运行。
//当接收器未注册时,时区可能已更改,因此请更新时间
mCalendar=新时间();
//确保我们更新到当前时间
onTimeChanged();
counter.start();
}
@凌驾
受保护的无效onDetachedFromWindow(){
super.onDetachedFromWindow();
如果(mAttached){
counter.cancel();
getContext().unregisterReceiver(MinentReceiver);
mAttached=假;
}
}
@TargetApi(构建版本代码蜂窝)
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
int widthMode=MeasureSpec.getMode(widthmasurespec);
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
浮动hs刻度=1.0f;
浮动vScale=1.0f;
if(widthMode!=MeasureSpec.UNSPECIFIED&&widthSize