与大致相同大小的启动器图标文本相比,绘制窗口小部件文本看起来非常模糊。在Stackoverflow找到了一些技巧,但他们没有帮助。
如何使用与启动器相同的尖锐字体?
用于绘制的代码:
Bitmap drawWidget(JSONArray result) {
int pixelsize = (int)Math.round(72.0 * SettingsActivity.density);
if (pixelsize > 0) {
Log.i(TAG, "pixel size = " + pixelsize);
Bitmap b = Bitmap.createBitmap(pixelsize, pixelsize, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint p = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.FILL);
p.setColor(Color.WHITE);
p.setTextSize(10f);
int y = 10;
for (TemperatureItem t : temperatureItems) {
double temp = getTemperature(result, t.getName());
c.drawText(t.getName() + ": " + temp, 0, y, p);
y += 10;
}
c.drawText(new SimpleDateFormat("HH:mm:ss").format(new Date()), 0, y, p);
return b;
} else {
return null;
}
窗口小部件提供程序:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widgetlayout"
android:minHeight="72dp"
android:minWidth="72dp"
android:updatePeriodMillis="1800000" >
</appwidget-provider>
小部件布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dip"
android:background="#000000" >
<ImageView
android:id="@+id/update"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:layout_margin="0dip"
android:text="Static Text" >
</ImageView>
</LinearLayout>