简体   繁体   中英

How to set images in Cordova plugin

I'm converting android native code into Cordova plugin. In native i'm using following code to set the image in Floating Action Button

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.icon_record, getTheme()));
    } else {
        floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.icon_record));
    } 

My doubt is, In android we will use R.drawable.XXXXXX for setting the image. Like wise how we can set the image in cordova plugin and where we have to store the images (Like drawable folders in android native)

Example from InAppBrowser plugin

Resources activityRes = cordova.getActivity().getResources();
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
Drawable backIcon = activityRes.getDrawable(backResId);
if (Build.VERSION.SDK_INT >= 16)
    back.setBackground(null);
else
    back.setBackgroundDrawable(null);
back.setImageDrawable(backIcon);

The images are copied using the resource-file tag in plugin.xml

<resource-file src="src/android/res/drawable-hdpi/ic_action_previous_item.png" target="res/drawable-hdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-mdpi/ic_action_previous_item.png" target="res/drawable-mdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-xhdpi/ic_action_previous_item.png" target="res/drawable-xhdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-xxhdpi/ic_action_previous_item.png" target="res/drawable-xxhdpi/ic_action_previous_item.png" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM