来自Android Widget的getAssets

时间:2011-12-03 02:27:57

标签: android

我遇到的问题是我正在尝试将自定义字体绘制到窗口小部件(AppWidgetProvider)上。现在的问题是你无法访问ges tAssets()和AppWidgetProvider的扩展。有没有解决的办法?谢谢!

*更新* 以下是我反馈后的情况。 Widget不显示任何内容。我的xml应该是什么样的呢?也许这就是我现在出错的地方......

import java.text.DateFormat;
import java.util.Date;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.graphics.Typeface;
import android.appwidget.AppWidgetManager;
import android.widget.RemoteViews;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.widget.ImageView;

public class ClockWidgetXProvider extends AppWidgetProvider {

    public Bitmap buildUpdate(String string, Context context) {
        Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
        Canvas myCanvas = new Canvas(myBitmap);
        Paint paint = new Paint();
        Typeface clock = Typeface.createFromAsset(context.getAssets(),"Carre.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(string, 80, 60, paint);
        return myBitmap;
    }
    @Override
    public void onUpdate(Context context,
        AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.clockwidget);
        String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
        view.setImageViewBitmap(R.id.time1, buildUpdate(currentDateTimeString, context));
        appWidgetManager.updateAppWidget(appWidgetIds, view);   
    }   
}

1 个答案:

答案 0 :(得分:1)

将方法更改为

public Bitmap buildUpdate(String time, Context context) {
...
Typeface clock = Typeface.createFromAsset(context.getAssets(),"Carre.ttf");