4

我正在尝试制作一个简单的小部件,在小部件中以文本和图形方式显示电池百分比。文本部分没有问题,但我很难让小部件以图形方式更新。

从图形上看,我有一个电池图像,我根据电池百分比进行剪辑。我正在尝试使用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()

我觉得我在做一些根本错误的事情。我非常感谢任何帮助/建议!:)

4

2 回答 2

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);
}
于 2013-04-15T19:23:29.430 回答
0

您对在小部件中使用 a 所做的两次尝试ClipDrawable都不正确。在第一次尝试中,您错误地使用了该方法,因为该方法setInt()期望第一个int参数是idView部件布局中的,该方法存在于类中。您的第二种方法也不正确,因为您所做的只是加载 a ,设置其级别,仅此而已,新创建的与小部件的根目录无关。setLevel()ViewDrawableDrawableDrawableLinearLayout

要完成这项ClipDrawable工作,您需要将其级别设置为所需的值,而可绘制对象已设置为LinearLayout的背景,或者您可以将其设置为背景。不幸的是,当在小部件中使用布局时,我看不到您如何做到这一点。

您需要使用一种解决方法(Jschools 提供了一种)。

于 2013-04-16T08:52:49.670 回答