Example usage for android.graphics.drawable BitmapDrawable setTargetDensity

List of usage examples for android.graphics.drawable BitmapDrawable setTargetDensity

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable setTargetDensity.

Prototype

public void setTargetDensity(int density) 

Source Link

Document

Set the density at which this drawable will be rendered.

Usage

From source file:Main.java

public static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from w w  w  .j a  va 2s  .c  o m
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}

From source file:Main.java

@SuppressWarnings("deprecation")
private static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }/*w ww .j av  a2s.  co  m*/
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }//  w  w w. ja v a 2s. co m
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);//  w  w  w .  j  a  v a 2 s  .com
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;//  w  ww .ja va2 s. c  o m

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        bitmapDrawable.setAntiAlias(true);
        bitmapDrawable.setDither(true);
        bitmapDrawable.setTargetDensity(Integer.MAX_VALUE);
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:com.linkbubble.util.Util.java

/**
 * Returns a bitmap suitable for the all apps view.
 *///from w  ww.  j a v a  2s.  co m
static Bitmap createIconBitmap(Drawable icon, Context context) {
    synchronized (sCanvas) { // we share the statics :-(
        if (sIconWidth == -1) {
            initStatics(context);
        }

        int width = sIconWidth;
        int height = sIconHeight;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes.
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down.
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            } else if (sourceWidth < width && sourceHeight < height) {
                // Don't scale up the icon
                width = sourceWidth;
                height = sourceHeight;
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = sIconTextureWidth;
        int textureHeight = sIconTextureHeight;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        icon.draw(canvas);
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

From source file:com.uzmap.pkg.uzmodules.UIListView.UIListView.java

@SuppressWarnings("deprecation")
public void jsmethod_setRefreshHeader(UZModuleContext uzContext) {

    pullDownContext = uzContext;/*from w w  w.  ja  va  2 s.  c o  m*/

    if (mRefreshableList != null) {
        mRefreshableList.setMode(mRefreshableList.getMode() != Mode.DISABLED
                ? mRefreshableList.getMode() == Mode.PULL_FROM_START ? Mode.PULL_FROM_START : Mode.BOTH
                : Mode.PULL_FROM_START);
        String textDown = uzContext.optString("textDown");
        if (TextUtils.isEmpty(textDown)) {
            textDown = TEXT_DOWN;
        }
        String textUp = uzContext.optString("textUp");
        if (TextUtils.isEmpty(textUp)) {
            textUp = TEXT_UP;
        }
        String loadingImg = uzContext.optString("loadingImg");

        if (!uzContext.isNull("showTime")) {
            showHeaderTime = uzContext.optBoolean("showTime");
        }

        String bgColor = "#f5f5f5";

        if (!uzContext.isNull("bgColor")) {
            bgColor = uzContext.optString("bgColor");
        }

        String textColor = "#8e8e8e";

        if (!uzContext.isNull("textColor")) {
            textColor = uzContext.optString("textColor");
        }
        LoadingLayout loadingLayout = mRefreshableList.getHeaderLayout();
        loadingLayout.setPullLabel(textDown);
        loadingLayout.setReleaseLabel(textUp);
        loadingLayout.setRefreshingLabel(REFRESH_LABEL);

        BitmapDrawable bitmapDrawable = new BitmapDrawable(getBitmap(loadingImg));
        bitmapDrawable.setTargetDensity(mContext.getResources().getDisplayMetrics().densityDpi);
        loadingLayout.setLoadingDrawable(bitmapDrawable);

        loadingLayout.setBackgroundColor(UZUtility.parseCssColor(bgColor));
        loadingLayout.getHeaderText().setTextColor(UZUtility.parseCssColor(textColor));
        loadingLayout.getSubHeaderText().setTextColor(UZUtility.parseCssColor(textColor));

        if (refreshListener == null) {
            refreshListener = new OnRefreshListener();
            mRefreshableList.setOnRefreshListener(refreshListener);
        }
    }
}

From source file:com.uzmap.pkg.uzmodules.UIListView.UIListView.java

@SuppressWarnings("deprecation")
public void jsmethod_setRefreshFooter(UZModuleContext uzContext) {

    pullUpContext = uzContext;//from  w  w  w. j a v  a  2 s.com

    if (mRefreshableList != null) {
        mRefreshableList.setMode(mRefreshableList.getMode() != Mode.DISABLED
                ? mRefreshableList.getMode() == Mode.PULL_FROM_END ? Mode.PULL_FROM_END : Mode.BOTH
                : Mode.PULL_FROM_END);
        String textDown = uzContext.optString("textDown");
        if (TextUtils.isEmpty(textDown)) {
            textDown = TEXT_DOWN;
        }
        String textUp = uzContext.optString("textUp");
        if (TextUtils.isEmpty(textUp)) {
            textUp = TEXT_UP;
        }
        String loadingImg = uzContext.optString("loadingImg");

        if (!uzContext.isNull("showTime")) {
            showFooterTime = uzContext.optBoolean("showTime");
        }

        String bgColor = "#f5f5f5";

        if (!uzContext.isNull("bgColor")) {
            bgColor = uzContext.optString("bgColor");
        }

        String textColor = "#8e8e8e";

        if (!uzContext.isNull("textColor")) {
            textColor = uzContext.optString("textColor");
        }

        LoadingLayout loadingLayout = mRefreshableList.getFooterLayout();
        loadingLayout.setPullLabel(textUp);
        loadingLayout.setReleaseLabel(textDown);
        loadingLayout.setRefreshingLabel(REFRESH_LABEL);

        BitmapDrawable bitmapDrawable = new BitmapDrawable(getBitmap(loadingImg));
        bitmapDrawable.setTargetDensity(mContext.getResources().getDisplayMetrics().densityDpi);
        loadingLayout.setLoadingDrawable(bitmapDrawable);

        loadingLayout.setBackgroundColor(UZUtility.parseCssColor(bgColor));
        loadingLayout.getHeaderText().setTextColor(UZUtility.parseCssColor(textColor));
        loadingLayout.getSubHeaderText().setTextColor(UZUtility.parseCssColor(textColor));

        if (refreshListener == null) {
            refreshListener = new OnRefreshListener();
            mRefreshableList.setOnRefreshListener(refreshListener);
        }
        // callback(uzContext ,true);
    }
}

From source file:com.android.launcher3.Utilities.java

/**
 * @param scale the scale to apply before drawing {@param icon} on the canvas
 *///from w  w  w  .j  ava2 s .co  m
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
    synchronized (sCanvas) {
        final int iconBitmapSize = getIconBitmapSize();

        int width = iconBitmapSize;
        int height = iconBitmapSize;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // Scale the icon proportionally to the icon dimensions
            final float ratio = (float) sourceWidth / sourceHeight;
            if (sourceWidth > sourceHeight) {
                height = (int) (width / ratio);
            } else if (sourceHeight > sourceWidth) {
                width = (int) (height * ratio);
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = iconBitmapSize;
        int textureHeight = iconBitmapSize;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
        icon.draw(canvas);
        canvas.restore();
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}