当用作窗口小部件的布局背景时,ClipDrawable不起作用 - java

我正在尝试制作一个简单的小部件,以小部件形式以文本和图形方式显示电池电量百分比。文本部分可以正常工作,但是让小部件以图形方式进行更新非常困难。

在图形上,我有一个根据电池百分比裁剪的电池图像。我正在尝试为此使用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())。两次尝试均未成功。

我觉得我做的事情根本上是错误的。我非常感谢您的帮助/建议! 🙂

参考方案

正确的是,您需要在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);
}

Android:如何为wrap_content设置高度动画? - java

我需要使用ValueAnimator来使用户拖动特定视图时出现自定义“放置字段”。 (我想将字段从gone, height = 0更改为visible, height = wrap_content)。我已经尝试过以下问题的解决方案:How to animate to wrap_content?当我在单个TextView上使用它时,答案就起作用了,但是当我尝试…

从服务器加载XML布局(android) - java

我对此问题进行了大量研究,但没有得到想要的答案。所以我确实有一个应用程序从服务器获取字符串。字符串系列采用XML格式。这是我将从服务器获得的示例(如您所见,它是一个布局):<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to…

为什么我的应用在启动时总是崩溃-Android - java

刚刚遵循了有关如何制作按钮和活动的youtube指南。我按照他的代码减小字体大小,并且在启动时一直崩溃。有人知道为什么吗?public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { su…

如何在ScrollView和LinearLayout中居中 - java

请看下面的代码<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match…

纵向模式下带有图标和文字的ActionBar - java

无论尝试如何,我似乎都无法在操作栏中的菜单项上以肖像模式工作时获得图像和文本。我什至试图强迫android纵向我的XML: <item android:id="@+id/menu_save" android:icon="@drawable/content" android:orderInCategory=…