我想用自定义字体制作一个数字时钟小部件。我想要的代码我知道应该使用AlarmManager和服务的概念,但是由于我是新手,所以我无法实现它。
我尝试使用位图,绘画和画布概念,但没有像时钟那样每分钟更新一次ui的方法,我尝试了许多教程并尝试解决先前提出的问题,但没有一个对我有用
public static Bitmap BuildUpdate(String txttime, int size, Context context) {
Paint paint = new Paint();
paint.setTextSize(size);
Typeface ourCustomType = Typeface.createFromAsset(context.getAssets(), "fonts/Lato-Light.ttf");
paint.setTypeface(ourCustomType);
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setSubpixelText(true);
paint.setAntiAlias(true);
float baseline = -paint.ascent();
int width = (int) (paint.measureText(txttime) + 0.5f);
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(txttime, 0, baseline, paint);
return image;
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId) {
CharSequence widgetText = FiscelClockConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.fiscel_clock);
views.setImageViewBitmap(R.id.imgClock, BuildUpdate("20:25", 40, context));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
我想要一个时钟小部件,它是一种数字时钟,可以具有自定义字体,并且每分钟更新一次,以使其外观和行为类似于时钟。