我正在尝试制作一个简单的小部件,在小部件中以文本和图形方式显示电池百分比。文本部分没有问题,但我很难让小部件以图形方式更新。
从图形上看,我有一个电池图像,我根据电池百分比进行剪辑。我正在尝试使用ClipDrawable
它。
battery_widget_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="@dimen/widget_padding"
android:background="@drawable/battery_clip_layer" >
<TextView
android:id="@+id/batteryPercentageWidgetTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/battery_percentage_widget_default"
android:gravity="center" />
</LinearLayout>
battery_clip_layer.xml(即 ClipDrawable)
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:gravity="bottom"
android:drawable="@drawable/battery_shape" />
BatteryService.java - 接收电池事件并通过 RemoteViews 更新小部件的服务
public class BatteryService extends Service {
private static final String LOG = BatteryService.class.getName();
private final AtomicInteger batteryPercentage = new AtomicInteger(100);
private final BroadcastReceiver batteryUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
batteryPercentage.set(level);
updateWidget();
}
};
@Override
public void onCreate() {
super.onCreate();
registerReceiver(batteryUpdateReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Log.i(LOG, "Created...");
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryUpdateReceiver);
Log.i(LOG, "Destroyed...");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG, "Started...");
updateWidget();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
private void updateWidget() {
final int percentage = batteryPercentage.get();
log.i(LOG, "Updated... " + percentage);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.battery_widget_layout);
remoteViews.setTextViewText(R.id.batteryPercentageWidgetTextView, percentage + "%");
//ATTEMPT 1 - no cigar
remoteViews.setInt(R.drawable.battery_clip_layer, "setLevel", 5000);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(new ComponentName(this, BatteryAppWidgetProvider.class), remoteViews);
}
private void updateWidgetAttempt2() {
final int percentage = batteryPercentage.get();
Log.i(LOG, "Updated... " + percentage);
//ATTEMPT 2 - still no cigar
Drawable clipLayer = getApplicationContext().getResources().getDrawable(R.drawable.battery_clip_layer);
if (clipLayer instanceof ClipDrawable) {
ClipDrawable clipDrawable = (ClipDrawable) clipLayer;
int level = clipDrawable.getLevel();
if (clipDrawable.setLevel(10000)) {
clipDrawable.invalidateSelf();
}
Log.i(LOG, "Updated clip amount..." + level + " -> " + ((ClipDrawable) clipLayer).getLevel());
}
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.battery_widget_layout);
remoteViews.setTextViewText(R.id.batteryPercentageWidgetTextView, percentage + "%");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(new ComponentName(this, BatteryAppWidgetProvider.class), remoteViews);
}
}
请查看更新小部件(和)BatteryService
的两种不同尝试。两种尝试都不成功。updateWidget()
updateWidgetAttempt2()
我觉得我在做一些根本错误的事情。我非常感谢任何帮助/建议!:)