Example usage for android.graphics Rect width

List of usage examples for android.graphics Rect width

Introduction

In this page you can find the example usage for android.graphics Rect width.

Prototype

public final int width() 

Source Link

Usage

From source file:Main.java

public static int[] sizeWithTitle(Activity activity) {
    Rect outRect = new Rect();
    int[] size = new int[2];
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
    size[0] = outRect.width();
    size[1] = outRect.height();/* w w w  . ja  v a  2 s.  com*/
    return size;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context context, int resId, String text) {

    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null)
        bitmapConfig = Bitmap.Config.ARGB_8888;
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(context.getResources().getColor(android.R.color.white));
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize((int) (12 * scale));

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 4;
    int y = (bitmap.getHeight() + bounds.height()) / 5;

    canvas.drawText(text, x * scale, y * scale, paint);

    return bitmap;
}

From source file:Main.java

public static float setTextSizeForWidth(Paint paint, float desiredWidth, String text, float max, float min) {
    final float textSize = 12.0f;

    paint.setTextSize(textSize);//from   w  ww .ja v  a2s  . c  o m
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float desiredTextSize = textSize * desiredWidth / bounds.width();
    if (desiredTextSize > max) {
        desiredTextSize = max;
    } else if (desiredTextSize < min) {
        desiredTextSize = min;
    }
    paint.setTextSize(desiredTextSize);
    return desiredTextSize;
}

From source file:Main.java

public static void log(String key, Rect rect) {
    debugOutput.put(key, rect.left + ", " + rect.top + ", " + rect.right + ", " + rect.bottom + " : "
            + rect.width() + ", " + rect.height());
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, String gText, int frontColor, int backColor) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    int w = 1536, h = 1280;
    Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
    Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
    android.graphics.Bitmap.Config bitmapConfig = bmp.getConfig();
    Canvas canvas = new Canvas(bmp);

    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(frontColor);//from  ww w. jav a  2 s  .  c  o m
    // text size in pixels
    paint.setTextSize((int) (400 * scale));
    // text shadow
    //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    if (backColor != -1) {
        canvas.drawColor(backColor);
    }
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bmp.getWidth() - bounds.width()) / 2;
    int y = (bmp.getHeight() + bounds.height()) / 2;
    canvas.drawText(gText, x, y, paint);
    return bmp;
}

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);//from   www  .  j  a va  2s.  c o m
    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);
    return new BitmapDrawable(res, buffer);
}

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);//from ww w .  ja  v  a  2s  .  c om
    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 drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {//w ww .j  a  v a  2s . c  om
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(Color.rgb(77, 77, 77));
        // text size in pixels
        paint.setTextSize((int) (13 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (int) ((bitmap.getWidth() - bounds.width()) / 4);
        int y = (int) ((bitmap.getHeight() + bounds.height()) / 4);

        canvas.drawText(mText, x * scale, y * scale, paint);

        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
        ScaleType scalingLogic) {//  w  ww . j a  va2 s.c om
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
    return scaledBitmap;
}

From source file:Main.java

public static int[] getApplicationWidthAndHeight(Activity activity) {
    Rect outRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
    System.out.println("top:" + outRect.top + " ; left: " + outRect.left);
    int info[] = new int[2];
    info[0] = outRect.width();
    info[1] = outRect.height();/*from   w w w  . j  a  va 2 s . c  om*/
    return info;
}