我正在尝试制作一个简单的小部件,在小部件中以文本和图形方式显示电池百分比。文本部分没有问题,但是我很难让窗口小部件以图形方式更新。
从图形上看,我有一个电池图像,我根据电池百分比剪辑。我正在尝试使用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()
)。这两种尝试都没有成功。
我觉得我在做一些根本错误的事情。我非常感谢任何帮助/建议! :)
答案 0 :(得分:4)
你是正确的,因为你需要在Drawable实例上设置关卡,但不幸的是你无法进入Drawable,因为它在远程视图中。
但是,ImageView确实有一个名为setImageLevel
的方法,您可以远程调用它。我的建议是将Drawable放在ImageView而不是容器的背景中,然后使用setInt(R.id.image_view, "setImageLevel", level)
。
以下是布局的样子:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_padding" >
<ImageView
android:id="@+id/battery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/battery_clip_layer" />
<TextView
android:id="@+id/batteryPercentageWidgetTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/battery_percentage_widget_default"
android:gravity="center" />
</FrameLayout>
updateWidget()
方法:
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 + "%");
remoteViews.setInt(R.id.battery, "setImageLevel", percentage * 100);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(new ComponentName(this, BatteryAppWidgetProvider.class), remoteViews);
}
答案 1 :(得分:0)
您在窗口小部件中使用ClipDrawable
的两次尝试都不正确。在第一次尝试中,您错误地使用了setInt()
方法,因为该方法希望第一个int
参数是小工具布局中id
的{{1}}(而不是可绘制的)像你尝试的那样参考),Android SDK的任何View
子类上都没有setLevel()
方法,该方法存在于View
类中。你的第二种方法也是不正确的,因为你所做的就是加载一个Drawable
,设置它的级别,就是这一切,新创建的Drawable
与小部件的Drawable
根无关。
要使LinearLayout
工作,您需要将其级别设置为所需的值,同时drawable已设置为ClipDrawable
的背景,或者您可以将其设置为的背景。不幸的是,当在窗口小部件中使用布局时,我不知道如何做到这一点。
您需要使用解决方法(Jschools提供一个)。