我有一个数字时钟小部件,我想使用自定义字体来显示时间。我知道它无法在远程视图中完成,因此我获得了一些代码,可以将自定义字体呈现为位图,然后将其推送到远程视图到imageview。但是,我无法让它工作。到目前为止,这是我的代码:
public Bitmap buildUpdate(String time)
{
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.main);
Bitmap myBitmap = Bitmap.createBitmap(100, 50, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(this.getAssets(),"Clockopia.ttf");
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(clock);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(15);
myCanvas.drawText(time, 0, 20, paint);
views.setImageViewBitmap(R.id.TimeView, myBitmap);
return myBitmap;
}
private void update() {
mCalendar.setTimeInMillis(System.currentTimeMillis());
final CharSequence date = DateFormat.format(mDateFormat, mCalendar);
final CharSequence day = DateFormat.format(mDayFormat, mCalendar);
// final CharSequence time = DateFormat.format(mTimeFormat, mCalendar);
String time = (String) DateFormat.format(mTimeFormat, mCalendar);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
views.setTextViewText(R.id.Day, day);
views.setTextViewText(R.id.Date, date);
// views.setTextViewText(R.id.Time, time);
buildUpdate(time);
ComponentName widget = new ComponentName(this, DigiClock.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(widget, views);
}
感谢任何帮助。
答案 0 :(得分:3)
我现在已经找到了(在经验丰富的开发人员的帮助下)如何让它发挥作用。 现在的最终代码是:
private void update() {
mCalendar.setTimeInMillis(System.currentTimeMillis());
final CharSequence date = DateFormat.format(mDateFormat, mCalendar);
final CharSequence day = DateFormat.format(mDayFormat, mCalendar);
String time = (String) DateFormat.format(mTimeFormat, mCalendar);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
views.setTextViewText(R.id.Day, day);
views.setTextViewText(R.id.Date, date);
views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));
ComponentName widget = new ComponentName(this, DigiClock.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(widget, views);
}
public Bitmap buildUpdate(String time)
{
Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(this.getAssets(),"Clockopia.ttf");
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(clock);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(65);
paint.setTextAlign(Align.CENTER);
myCanvas.drawText(time, 80, 60, paint);
return myBitmap;
}