android - 使用自定义字体显示时间时出现问题

标签 android widget typeface

我有一个数字时钟小部件,我想使用自定义字体来显示时间。我知道它不能在 remoteviews 中完成,所以我获得了一些代码,将自定义字体呈现为位图,然后将其跨 remoteviews 推送到 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);
    }

感谢任何帮助。

最佳答案

我现在已经找到了(在一位经验丰富的开发人员的帮助下)如何让它工作。 现在的最终代码是:

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

关于android - 使用自定义字体显示时间时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377668/

相关文章:

java - Canvas 上的 SWT 可编辑文本字段

android - 如何将 MediaBrowserService 与小部件一起使用?

progressdialog - 如何从 Assets 文件夹中为 ProgressDialog 添加字体?

java - "Unfortunately (app name) hast stopped"的字体原因

android - 将 typeFace 设置为 NumberPicker

android - 如何获取 Google DeskClock 应用程序中设置的闹钟时间?

css - 加载所有内容后重新设置小部件样式

android - Android 项目中带有 Gradle 插件的模块

android - 如何在 Android 中不使用 Intent 裁剪图像

java - Android 读取文件时出现 ArrayIndexOutOfBounds