我想在位图上添加文本并在App-Widget上的ImageView中设置它,这是我的代码:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(20);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
//int xPos = (canvas.getWidth() / 2);
//int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
int xPos = (canvas.getWidth() / 2);
int yPos = (canvas.getHeight() / 2);
canvas.drawText("My Text", xPos, yPos, paint);
views.setImageViewBitmap(R.id.widgetImg, bitmap);
appWidgetManager.updateAppWidget(appWidgetId, views);
但是在AppWidget中,它只是简单的图像(R.drawable.img
),并且没有文字。
有人知道这个问题吗?
答案 0 :(得分:0)
根据this回答刚改变了这句话:
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));
为:
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);