问题显示使用自定义字体时间 [英] Problem showing time using custom font

查看:121
本文介绍了问题显示使用自定义字体时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字时钟小工具,我想用一个自定义的字体来显示时间。我知道,它不能在remoteviews做的,所以我取得了一些code呈现的自定义字体为位图,然后在remoteviews将其推到一个ImageView的。但是,我无法得到它的工作。这是我的code到目前为止:

I have a digital clock widget, and I want to use a custom font to display the time. I know that it can't be done in remoteviews, so I have obtained some code that renders the custom font to a bitmap and then pushes it across remoteviews to an imageview. However, I am unable to get it to work. This is my code so far:

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

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

我现在已经发现了(有经验的开发人员的帮助下)如何得到它的工作。 最后code现在是:

I have now found out (with the help of an experienced developer) how to get it to work. The final code now is:

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

这篇关于问题显示使用自定义字体时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆