gpt4 book ai didi

android - 创建一个模拟时钟小部件根据不同的时区显示时间

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:56 25 4
gpt4 key购买 nike

实际上,我必须在选择特定时区后制作一个模拟时钟小部件来显示世界时间。我创建了一个模拟时钟 选择时区的选项。我希望将具有更新时区的同一个时钟显示为一个小部件。我不知道如何只将时钟 View 作为一个小部件。

我的代码是:

package nEx.Software.Tutorials.Widgets.AnalogClock;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
//import nEx.Software.Tutorials.Widgets.AnalogClock.main.CustomClock;
//import nEx.Software.Tutorials.Widgets.AnalogClock.main.MyTime;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Info extends Activity implements RadioGroup.OnCheckedChangeListener
{

Context context;
TextView tv;
RadioButton rb1, rb2, rb3;
RadioGroup r1;
FrameLayout fl1;
LinearLayout l1;
Timer timer;
Date result;
ViewGroup.LayoutParams lp1;
Drawable bck;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.main);

context = this;
tv = (TextView) findViewById(R.id.TextView01);
r1 = (RadioGroup) findViewById(R.id.RadioGroup01);
rb1 = (RadioButton) findViewById(R.id.RadioButton01);
rb2 = (RadioButton) findViewById(R.id.RadioButton02);
rb3 = (RadioButton) findViewById(R.id.RadioButton03);
fl1 = (FrameLayout) findViewById(R.id.FrameLayout01);
bck = this.getResources().getDrawable(R.drawable.clockface);
fl1.setBackgroundDrawable(bck);
lp1 = fl1.getLayoutParams();
r1.setOnCheckedChangeListener(this);
result = null;
}
private void updateTime(String str) {
// TODO Auto-generated method stub
if (timer != null) {
timer.cancel();
timer = null;
}
timer = new Timer(); //digital clock
MyTime mt = new MyTime(this, str);
timer.schedule(mt, 1, 1000);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (rb1.isChecked() == true) {
updateTime("GMT");

}
if (rb2.isChecked() == true) {
updateTime("EST");
}
if (rb3.isChecked() == true) {
updateTime("MST");
}
}
public class MyTime extends TimerTask {
String tz;
public MyTime(Context context, String str) {
tz = str;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Date date = new Date();
date = getDateInTimeZone(date, tz);
//System.out.println(date.toLocaleString());
result = date;
handler.sendEmptyMessage(0);
} catch (Exception e) {
}
}
private 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();
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
tv.setText(result.toLocaleString());
//System.out.println(result.getHours()+" "+result.getMinutes());
fl1.removeAllViews();
fl1.addView(new CustomClock(context, lp1.height / 2, lp1.width / 2, result));
}
};
public class CustomClock extends View {
private final float x;
private final float y;
private final int r = 70;
private final Date date;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CustomClock(Context context, float x, float y, Date date) {
super(context);
this.x = x;
this.y = y;
this.date = date;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawCircle(x, y, r, mPaint);
float sec = (float) date.getSeconds();
float min = (float) date.getMinutes();
float hour = (float) date.getHours() + min / 60.0f;
mPaint.setColor(0xFFFF0000);
canvas.drawLine(x, y, (float) (x + (r - 15) * Math.cos(Math.toRadians((hour / 12.0f * 360.0f) - 90f))), (float) (y + (r - 10) * Math.sin(Math.toRadians((hour / 12.0f * 360.0f) - 90f))), mPaint);
canvas.save();
mPaint.setColor(0xFF0000FF);
canvas.drawLine(x, y, (float) (x + r * Math.cos(Math.toRadians((min / 60.0f * 360.0f) - 90f))), (float) (y + r * Math.sin(Math.toRadians((min / 60.0f * 360.0f) - 90f))), mPaint);
canvas.save();
mPaint.setColor(0xFFA2BC13);
canvas.drawLine(x, y, (float) (x + (r + 10) * Math.cos(Math.toRadians((sec / 60.0f * 360.0f) - 90f))), (float) (y + (r + 15) * Math.sin(Math.toRadians((sec / 60.0f * 360.0f) - 90f))), mPaint);
}
}
//}
// }


}

最佳答案

应用程序小部件非常限于称为“远程 View ”的几个 View 。甚至他们也不那么灵活。这个决定背后的原因可能是安全。您必须找到解决此问题的方法,因为您无法将自己的自定义 View 创建为 appwidget。这是所有可用远程 View 的列表: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

如果您愿意,您可以使用他们的模拟时钟。

关于android - 创建一个模拟时钟小部件根据不同的时区显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com