gpt4 book ai didi

android - 从 Android Widget 获取资源

转载 作者:行者123 更新时间:2023-11-30 04:26:43 25 4
gpt4 key购买 nike

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

* 更新 * 这是我收到反馈后的内容。小部件不显示任何内容。为此,我的 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);
}
}

最佳答案

方法改为

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

关于android - 从 Android Widget 获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8365009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com