Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在模拟时钟中按不同时区显示时间_Android_Time_Clock - Fatal编程技术网

Android 在模拟时钟中按不同时区显示时间

Android 在模拟时钟中按不同时区显示时间,android,time,clock,Android,Time,Clock,我的应用程序中有两个模拟时钟。一个时钟将显示当前位置时间。另一个时钟将用于显示用户选择的不同时区的时间。 我使用以下代码将特定时区设置为第二个时钟。但它不能正常工作。谁能帮帮我吗 public class ClockWidgetDemo extends Activity { MyAnalogClock clock2; TimePicker timePicker; Time timeSetter; Button btn; String timezone = "America/Los_Angele

我的应用程序中有两个模拟时钟。一个时钟将显示当前位置时间。另一个时钟将用于显示用户选择的不同时区的时间。
我使用以下代码将特定时区设置为第二个时钟。但它不能正常工作。谁能帮帮我吗

public class ClockWidgetDemo extends Activity {
MyAnalogClock clock2;
TimePicker timePicker;
Time timeSetter;
Button btn;
 String timezone = "America/Los_Angeles";
 String time;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn=(Button) findViewById(R.id.btn1);
    Calendar calendar=Calendar.getInstance();
    SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss a");
    sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 //TimeZone time=TimeZone.getTimeZone(timezone);
    time=sdf.format(calendar.getTime());
    Log.i("Analog Clock", "Time"+sdf.format(calendar.getTime()));
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             //timeSetter=new Time(time);
             timeSetter=new Time(timezone);

            onTimeChanged();



        }
    });
clock2 = (MyAnalogClock) findViewById(R.id.clock2);
    public void onTimeChanged(){



    timeSetter.set(timeSetter.second,timeSetter.minute,timeSetter.HOUR,timeSetter.monthDay, timeSetter.month, timeSetter.year);

    Log.i("Analog Clock","Seconds"+timeSetter.second+"Minute"+timeSetter.minute+"Hour"+timeSetter.hour+"Month Day"
            +timeSetter.monthDay+"Month"+timeSetter.month+"Year"+timeSetter.year);

    clock2.setTime(timeSetter.toMillis(true));

}
}
我的模拟时钟代码是

public class MyAnalogClock extends View{
public MyAnalogClock(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

private Time mCalendar;

private Drawable mHourHand;
private Drawable mMinuteHand;
private Drawable mSecondHand;
private Drawable mDial;

private int mDialWidth;
private int mDialHeight;

private boolean mAttached;

private final Handler mHandler = new Handler();
private float mMinutes;
private float mHour;
private boolean mChanged;

Context mContext;

public MyAnalogClock(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MyAnalogClock(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    Resources r = context.getResources();

//      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnalogClock, defStyle, 0);
    mContext = context;
    // mDial =
    // a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
    // if (mDial == null) {
    mDial = r.getDrawable(R.drawable.clock_dial1);
    // }

    // mHourHand =
    // a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour);
    // if (mHourHand == null) {
    mHourHand = r.getDrawable(R.drawable.clock_hour);
    // }

    // mMinuteHand =
    // a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute);
    // if (mMinuteHand == null) {
    mMinuteHand = r.getDrawable(R.drawable.clock_minute);
    mSecondHand = r.getDrawable(R.drawable.clockgoog_minute);
    // }

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

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


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

    boolean changed = mChanged;
    if (changed) {
        mChanged = false;
    }
    boolean seconds = mSeconds;
    if (seconds) {
        mSeconds = false;
    }
    int availableWidth = 200;
    int availableHeight = 200;

    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);
    // canvas.rotate(mSecond, 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);

    // minuteHand = mMinuteHand;
    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.set(System.currentTimeMillis() - 60000);

          //            int hour = mCalendar.hour;
          //            int minute = mCalendar.minute;
        int second = mCalendar.second;

        mSecond = 6.0f * second;
        mSeconds = true;
        // mChanged = true;
        MyAnalogClock.this.invalidate();
        // Toast.makeText(mContext, "text", Toast.LENGTH_LONG).show();
    }
}

public void setOffset(long offset){
    this.offset = offset;
    onTimeChanged();
}

public void setTime(long time){
    long tmp = System.currentTimeMillis() - time;
    this.offset = tmp;
    onTimeChanged();
}

boolean mSeconds = false;
float mSecond = 0;
long offset = 0;

private void onTimeChanged() {
    mCalendar.set(System.currentTimeMillis() - offset);

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


  }
公共类MyAnalogClock扩展视图{
公共MyAnalogClock(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
私人时间麦卡伦达;
私人可拉手;
私人可抽出式机头;
私人提款权;
私人可提取mDial;
私有int-mDialWidth;
私人室内高度;
私有布尔mAttached;
私有最终处理程序mHandler=新处理程序();
私人浮点数;
私家车;
私有布尔值改变;
语境;
公共MyAnalogClock(上下文、属性集属性){
这(上下文,属性,0);
}
公共MyAnalogClock(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
Resources r=context.getResources();
//TypedArray a=上下文。获取样式属性(attrs,R.styleable.AnalogClock,defStyle,0);
mContext=上下文;
//mDial=
//a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
//如果(mDial==null){
mDial=r.getDrawable(r.drawable.clock_dial1);
// }
//手=
//a.getDrawable(com.android.internal.R.styleable.AnalogClock\u hand\u hour);
//if(mHourHand==null){
mHourHand=r.getDrawable(r.drawable.clock\u hour);
// }
//米努特汉德=
//a.getDrawable(com.android.internal.R.styleable.AnalogClock\u hand\u minute);
//if(mMinuteHand==null){
mMinuteHand=r.getDrawable(r.drawable.clock_minute);
mSecondHand=r.getDrawable(r.drawable.clockgoog_分钟);
// }
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,
姆汉德勒);
}
//注意:在注册接收者后进行这些操作是安全的,因为
//接受者总是跑
//在主线程中,因此接收器在此之前无法运行
//方法返回。
//当接收器未注册时,时区可能已更改,
//所以更新时间
mCalendar=新时间();
//确保我们更新到当前时间
onTimeChanged();
counter.start();
}
@凌驾
受保护的无效onDetachedFromWindow(){
super.onDetachedFromWindow();
如果(mAttached){
counter.cancel();
getContext().unregisterReceiver(MinentReceiver);
mAttached=假;
}
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
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&&widthSizeprivate Date getDateInTimeZone(Date currentDate, String timeZoneId) 
{
    TimeZone tz = TimeZone.getTimeZone(timeZoneId);
    Calendar mbCal = new GregorianCalendar(tz);
    mbCal.setTimeInMillis(currentDate.getTime());
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
    cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
    cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
    cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));
    return cal.getTime();
 }

String tz = "America/Los_Angeles";
Date date = new Date();
date = getDateInTimeZone(date, tz);