Android: access “system”-drawables

一世执手 提交于 2019-12-04 03:37:21

问题


I'm currently working on an app to display the battery status and I'd like to use Android-drawables instead of own images to reduce the app size.

I've found this page which lists available images and the availability for each SDK-version:
http://www.fixedd.com/projects/android_drawables_display

My question: How can I access the "system"-drawables? If you click on the link and choose the tab "Status", there are some battery-drawables like "stat_sys_battery_0", but I can't access it, Eclipse doesn't offer intellisense for it and won't compile the app if I use one of those drawables.

As those drawables are part of all SDK-versions, I'd think I should be able to use them, or are those "special" drawables protected in a way so they can only be used by system-functions (and not apps)?

Any idea is appreciated.

Select0r


回答1:


Hope this is what you were looking for:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {

        int level = intent.getIntExtra("level", 0);

        int batteryIconId = intent.getIntExtra("icon-small", 0);
        Button toolBarBattery = (Button) findViewById(R.id.toolBarButton);

        LevelListDrawable batteryLevel = (LevelListDrawable) getResources().getDrawable(batteryIconId);
        batteryLevel.setLevel(level);

        toolBarBattery.setBackgroundDrawable(batteryLevel);

    }
};



回答2:


I've found another link with information that not all drawables are public. It doesn't say why some drawables would be private, but I guess I'll have to live with the fact and copy the needed images to my app.
http://androiddrawableexplorer.appspot.com/

NOTE: Some of the images in the Android jar are not public and therefore cannot be directly used (you can copy them to you own application, but can't reference them via the "android" package namespace).




回答3:


There actually seems to be a way to access the system icons, but it's not really working as stated in the documentation, but I'll add it in case somebody is interested:

intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1)

Will get you the resource-ID of the icon that matches the current battery-status:
http://developer.android.com/reference/android/os/BatteryManager.html#EXTRA_ICON_SMALL

Extra for ACTION_BATTERY_CHANGED: integer containing the resource ID of a small status bar icon indicating the current battery state.

However, it always returns the same icon, no matter what the actual battery level is. Finding the icon by just trying random numbers may work, but I don't know if the IDs are consistent throughout the SKD-levels as well as different machines, so I'd rather not rely in that.



来源:https://stackoverflow.com/questions/4891598/android-access-system-drawables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!