小部件中的自定义字体

时间:2012-03-25 15:15:48

标签: android time fonts android-widget

我正在尝试创建一个简单的Android小部件,显示时间但使用自定义字体。谁知道怎么做? 我试过这个: Problem showing time using custom font

但它不起作用。首先getAssets()将无法工作,因为它需要上下文,如果我在几秒后删除设置自定义字体的行,我将得到失败的活页夹事务错误。

任何人都可以帮助或给我一个教程的链接? 使用计时器更新时间也是一个好习惯吗?

我的代码 的 LWidget.java

public class LWidget extends AppWidgetProvider {

//=============================================================================================
//On Update
//=============================================================================================


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);



        //views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));





    }
//=============================================================================================
//Timer class
//=============================================================================================

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat timeformat = new SimpleDateFormat("kk:mm");
        public MyTime(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, LWidget.class);
    }

    @Override
    public void run() {
        Calendar ci = Calendar.getInstance();
        String time = timeformat.format(ci.getTime());
        remoteViews.setTextViewText(R.id.widget_textview,"Time = " + time);
        remoteViews.setImageViewBitmap(R.id.widget_imageview, buildUpdate(time));
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }

//=============================================================================================
//bitmap font
//=============================================================================================
    public Bitmap buildUpdate(String time) 
{
        Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
        Canvas myCanvas = new Canvas(myBitmap);
        Paint paint = new Paint();

        //if I remove the comment here, getAssets() will not work

        //Typeface clock = Typeface.createFromAsset(this.getAssets(),"fonts/Crysta.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;
 }




//=============================================================================================
//On receive
//=============================================================================================
@Override
    public void onReceive(Context context, Intent intent) {
            // v1.5 fix that doesn't call onDelete Action
            final String action = intent.getAction();
            if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            final int appWidgetId = intent.getExtras().getInt(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.onDeleted(context, new int[] { appWidgetId });
            }
            } else {
                super.onReceive(context, intent);
            }
    }

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_height="wrap_content">

<TextView android:id="@+id/widget_textview"
android:text="@string/widget_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
android:padding="10dip"
android:textColor="@android:color/black"/>

<ImageView 
   android:id="@+id/widget_imageview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

</LinearLayout>

ltime_widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/main"
/>

2 个答案:

答案 0 :(得分:2)

您必须使用AlarmManager代替计时器。 您无法使用计时器because

  

AppWidgetProviderBroadcastReceiver的延伸,您的   在回调方法之后,不保证进程继续运行   返回。

使用代码here作为参考。

  

getAssets()不起作用,因为它需要上下文

onUpdate()提供了Context,没有?

答案 1 :(得分:0)

好的尝试这样:

static Context con;  

Typeface clock = Typeface.createFromAsset(con.getAssets(),"fonts/KacstLetter.ttf");