Example usage for android.content.res Resources getDrawable

List of usage examples for android.content.res Resources getDrawable

Introduction

In this page you can find the example usage for android.content.res Resources getDrawable.

Prototype

@Deprecated
public Drawable getDrawable(@DrawableRes int id) throws NotFoundException 

Source Link

Document

Return a drawable object associated with a particular resource ID.

Usage

From source file:Main.java

public static Drawable showUninstallAPKIcon(Context context, String apkPath) {
    String PATH_PackageParser = "android.content.pm.PackageParser";
    String PATH_AssetManager = "android.content.res.AssetManager";
    try {//from  w  w  w. j  av a  2 s.co m
        Class<?> pkgParserCls = Class.forName(PATH_PackageParser);
        Class<?>[] typeArgs = new Class[1];
        typeArgs[0] = String.class;
        Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
        Object[] valueArgs = new Object[1];
        valueArgs[0] = apkPath;
        Object pkgParser = pkgParserCt.newInstance(valueArgs);
        DisplayMetrics metrics = new DisplayMetrics();
        metrics.setToDefaults();
        typeArgs = new Class[4];
        typeArgs[0] = File.class;
        typeArgs[1] = String.class;
        typeArgs[2] = DisplayMetrics.class;
        typeArgs[3] = Integer.TYPE;
        Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);
        valueArgs = new Object[4];
        valueArgs[0] = new File(apkPath);
        valueArgs[1] = apkPath;
        valueArgs[2] = metrics;
        valueArgs[3] = 0;
        Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
        Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");
        ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);
        Class<?> assetMagCls = Class.forName(PATH_AssetManager);
        Constructor<?> assetMagCt = assetMagCls.getConstructor((Class[]) null);
        Object assetMag = assetMagCt.newInstance((Object[]) null);
        typeArgs = new Class[1];
        typeArgs[0] = String.class;
        Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath", typeArgs);
        valueArgs = new Object[1];
        valueArgs[0] = apkPath;
        assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
        Resources res = context.getResources();
        typeArgs = new Class[3];
        typeArgs[0] = assetMag.getClass();
        typeArgs[1] = res.getDisplayMetrics().getClass();
        typeArgs[2] = res.getConfiguration().getClass();
        Constructor<?> resCt = Resources.class.getConstructor(typeArgs);
        valueArgs = new Object[3];
        valueArgs[0] = assetMag;
        valueArgs[1] = res.getDisplayMetrics();
        valueArgs[2] = res.getConfiguration();
        res = (Resources) resCt.newInstance(valueArgs);
        if (info.icon != 0) {
            Drawable icon = res.getDrawable(info.icon);
            return icon;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.gdgdevfest.android.apps.devfestbcn.ui.PlusStreamRowViewBinder.java

public static ImageLoader getPlusStreamImageLoader(FragmentActivity activity, Resources resources) {
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int largestWidth = metrics.widthPixels > metrics.heightPixels ? metrics.widthPixels : metrics.heightPixels;

    // Create list of placeholder drawables (this ImageLoader requires two different
    // placeholder images).
    ArrayList<Drawable> placeHolderDrawables = new ArrayList<Drawable>(2);
    placeHolderDrawables.add(PLACEHOLDER_USER_IMAGE, resources.getDrawable(drawable.person_image_empty));
    placeHolderDrawables.add(PLACEHOLDER_MEDIA_IMAGE,
            new ColorDrawable(resources.getColor(R.color.plus_empty_image_background_color)));

    // Create ImageLoader instance
    return new ImageLoader(activity, placeHolderDrawables).setMaxImageSize(largestWidth);
}

From source file:com.tencent.wetest.common.util.ReportUtil.java

public static List<HashMap<String, Object>> readReportList() {
    path = WTApplication.getContext().getFilesDir().getPath();
    int i = 0;//from   ww  w  .  j  av  a2s .com
    File indexfile = new File(path + "/wtIndex");
    if (indexfile.exists()) {
        String temp = null;

        PackageManager pm = (PackageManager) WTApplication.getContext().getPackageManager();

        List<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();

        try {
            BufferedReader indexreader = new BufferedReader(new FileReader(indexfile));
            // indexreader.readLine();
            while ((temp = indexreader.readLine()) != null) {
                String[] content = temp.split("/");

                PackageInfo pi = ApkManager.getPackageInfoByPackageName(content[3]);

                HashMap<String, Object> map = new HashMap<String, Object>();

                if (pi != null)
                    map.put("icon", pi.applicationInfo.loadIcon(pm));
                else {
                    Resources resources = WTApplication.getContext().getResources();
                    Drawable drawable = resources.getDrawable(R.drawable.logo);
                    map.put("icon", drawable);
                }

                CharSequence appname = content[2];

                CharSequence time = content[1];
                map.put("appName", time);
                map.put("packageName", appname);
                map.put("filename", content[0]);
                items.add(map);
            }

            indexreader.close();
            return items;
        } catch (Exception e) {

            Logger.error("readReportList ");
            e.printStackTrace();
        }
    }
    return null;
}

From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java

static Drawable getDrawableByName(Context context, String name) {
    final Resources resources = context.getResources();
    final int id = getResourceIdByName(context, "drawable", name);
    if (id == 0) {
        return null;
    }/*w  w w. j a va  2s  .c o m*/
    return resources.getDrawable(id);
}

From source file:Main.java

public static Object getResource(Context context, Field field, int value) {
    Resources resources = context.getResources();
    Class type = field.getType();

    if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return resources.getBoolean(value);
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
        return resources.getInteger(value);
    } else if (type.isAssignableFrom(ColorStateList.class))
        return resources.getColorStateList(value);
    else if (type.isAssignableFrom(XmlResourceParser.class))
        return resources.getXml(value);
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return resources.getDimension(value);
    else if (type.isAssignableFrom(Drawable.class))
        return resources.getDrawable(value);
    else if (type.isAssignableFrom(Animation.class))
        return AnimationUtils.loadAnimation(context, value);
    else if (type.isAssignableFrom(Movie.class))
        return resources.getMovie(value);
    else if (type.isAssignableFrom(String.class))
        return resources.getString(value);
    else if (type.isArray()) {
        if (type.getName().equals("[I")) {
            return resources.getIntArray(value);
        } else if (type.isAssignableFrom(String[].class)) {
            return resources.getStringArray(value);
        }/*from ww  w  .  j av a2  s. c o  m*/
    }

    return null;
}

From source file:com.fastbootmobile.encore.utils.Utils.java

public static void showCurrentSongOverflow(final Context context, final View parent, final Song song,
        final boolean showArtist) {
    PopupMenu popupMenu = new PopupMenu(context, parent);
    popupMenu.inflate(R.menu.queue_overflow);
    if (song.getAlbum() == null) {
        Log.d(TAG, "No album information, removing album options");

        // This song has no album information, hide the entries
        Menu menu = popupMenu.getMenu();
        menu.removeItem(R.id.menu_add_album_to_queue);
        menu.removeItem(R.id.menu_open_album);
    }/*from   ww  w.  j a  v a 2 s. co  m*/

    if (!showArtist) {
        Menu menu = popupMenu.getMenu();
        menu.removeItem(R.id.menu_open_artist);
    }

    popupMenu.show();

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            final ProviderAggregator aggregator = ProviderAggregator.getDefault();

            switch (menuItem.getItemId()) {
            case R.id.menu_open_album:
                final Resources res = context.getResources();
                Intent intent = AlbumActivity.craftIntent(context,
                        ((BitmapDrawable) res.getDrawable(R.drawable.album_placeholder)).getBitmap(),
                        song.getAlbum(), song.getProvider(),
                        res.getColor(R.color.default_album_art_background));
                context.startActivity(intent);
                break;

            case R.id.menu_open_artist:
                intent = ArtistActivity.craftIntent(context, null, song.getArtist(), song.getProvider(),
                        context.getResources().getColor(R.color.default_album_art_background));
                context.startActivity(intent);
                break;

            case R.id.menu_add_album_to_queue:
                PlaybackProxy.queueAlbum(aggregator.retrieveAlbum(song.getAlbum(), song.getProvider()), false);
                Toast.makeText(context, R.string.toast_album_added_to_queue, Toast.LENGTH_SHORT).show();
                break;

            case R.id.menu_add_to_playlist:
                PlaylistChooserFragment fragment = PlaylistChooserFragment.newInstance(song);
                if (context instanceof FragmentActivity) {
                    FragmentActivity act = (FragmentActivity) context;
                    fragment.show(act.getSupportFragmentManager(), song.getRef());
                } else {
                    throw new IllegalArgumentException("Context must be an instance of FragmentActivity");
                }
                break;

            default:
                return false;
            }
            return true;
        }
    });
}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

public static Drawable getDrawableByName(View view, String name) {
    Resources resources = view.getResources();
    return resources.getDrawable(resources.getIdentifier(name, "drawable", view.getContext().getPackageName()));
}

From source file:com.android.dialer.contactinfo.ContactPhotoLoaderTest.java

private Uri getResourceUri(int resId) {
    Context testContext = getInstrumentation().getContext();
    Resources resources = testContext.getResources();

    assertNotNull(resources.getDrawable(resId));
    return Uri.parse(
            ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + testContext.getPackageName() + '/' + resId);
}

From source file:io.github.importre.animatedicons.AnimatedButton.java

@SuppressLint("NewApi")
private void init(Context context) {
    if (isLollipop()) {
        offDrawable = context.getDrawable(getOffDrawable());
        onDrawable = context.getDrawable(getOnDrawable());
    } else {//from  www.j  av a 2s.c  o  m
        Resources r = context.getResources();

        offDrawable = r.getDrawable(getOffDrawable());
        onDrawable = r.getDrawable(getOnDrawable());
        offDrawable = DrawableCompat.wrap(offDrawable);
        onDrawable = DrawableCompat.wrap(onDrawable);

        int color = r.getColor(R.color.ai_primary);
        DrawableCompat.setTint(offDrawable, color);
        DrawableCompat.setTint(onDrawable, color);
    }

    setScaleType(ScaleType.CENTER_INSIDE);

    if (isLollipop()) {
        setImageDrawable(!checked ? onDrawable : offDrawable);
    } else {
        setImageDrawable(checked ? onDrawable : offDrawable);
    }
}

From source file:com.kth.baasio.baassample.BaseActivity.java

/**
 * Sets the icon color using some fancy blending mode trickery.
 *///from   w  ww . j av a2  s .  c  om
protected void setActionBarColor(int color) {
    if (color == 0) {
        color = 0xffffffff;
    }

    final Resources res = getResources();
    Drawable maskDrawable = res.getDrawable(R.drawable.actionbar_icon_mask);
    if (!(maskDrawable instanceof BitmapDrawable)) {
        return;
    }

    Bitmap maskBitmap = ((BitmapDrawable) maskDrawable).getBitmap();
    final int width = maskBitmap.getWidth();
    final int height = maskBitmap.getHeight();

    Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outBitmap);
    canvas.drawBitmap(maskBitmap, 0, 0, null);

    Paint maskedPaint = new Paint();
    maskedPaint.setColor(color);
    maskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

    canvas.drawRect(0, 0, width, height, maskedPaint);

    BitmapDrawable outDrawable = new BitmapDrawable(res, outBitmap);
    getSupportActionBar().setIcon(outDrawable);
}