Проблема, показывающая время с использованием пользовательских шрифтов

StackOverflow https://stackoverflow.com/questions/4377668

  •  09-10-2019
  •  | 
  •  

Вопрос

У меня есть цифровой виджет часов, и я хочу использовать пользовательский шрифт для отображения времени. Я знаю, что это не может быть сделано в 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;
 }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top