Example usage for android.view View getWidth

List of usage examples for android.view View getWidth

Introduction

In this page you can find the example usage for android.view View getWidth.

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() 

Source Link

Document

Return the width of your view.

Usage

From source file:Main.java

public static Bitmap scaleBitmapToFitInView(View view, String imagePath) {
    // Get the dimensions of the View
    int targetW = view.getWidth();
    int targetH = view.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;/*w w  w .  j  av a2s.  c o  m*/

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
    return bitmap;
}

From source file:Main.java

/**
 * Gets the proper scale value that scales down the content and keeps its aspect ratio to
 * display inside the view./*w w w.  jav  a  2  s .c o m*/
 */
public static float getDisplayScale(RectF contentBounds, View view) {
    if (contentBounds.isEmpty()) {
        return 1;
    }

    float scale = Math.min(view.getWidth() / contentBounds.width(), view.getHeight() / contentBounds.height());
    // Avoid scaling up the content.
    return Math.min(scale, 1);
}

From source file:Main.java

public static Bitmap convertViewToBitmap(View v) {
    if (v == null) {
        return null;
    }/* w w  w . j  a  v a 2  s  .  c om*/
    Bitmap screenshot;
    screenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(screenshot);
    v.draw(c);
    return screenshot;
}

From source file:Main.java

public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);//w  w w .  j a va2  s  .c  om
    else
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

From source file:Main.java

public static Bitmap getBitmapFromView(View v) {
    if (v == null) {
        return null;
    }/*from www  .  ja  v  a 2s  . c o  m*/
    Bitmap screenShot;
    screenShot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas mCanvas = new Canvas(screenShot);
    mCanvas.translate(-v.getScrollX(), -v.getScrollY());
    v.draw(mCanvas);
    return screenShot;
}

From source file:Main.java

public static float[] convertViewXYintoBitmapXY(View view, Bitmap bitmap, float x, float y) {
    float imgHeight = view.getHeight();
    float imgWidth = view.getWidth();
    float bmHeight = bitmap.getHeight();
    float bmWidth = bitmap.getWidth();

    float WidthRatio = bmWidth / imgWidth;
    float HeightRatio = bmHeight / imgHeight;

    float BitmapPixelX = x * WidthRatio;
    float BitmapPixelY = y * HeightRatio;

    float[] bitmapXY = new float[] { BitmapPixelX, BitmapPixelY };
    return bitmapXY;
}

From source file:Main.java

/**
 * Tries to get the available width from the (previously measured) view of the activity.
 * If if fails then calls getDisplayWidth().
 *///from   ww w  .  j a  va  2  s . c om
private static int getWindowWidth(Activity activity) {
    int width = 0;

    //Doesn't work on create method, should call it from another place?
    View content = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
    if (content != null)
        width = content.getWidth();

    if (width == 0)
        width = getDisplayWidth(activity);

    return width;
}

From source file:Main.java

/**
 * Convert the specified view to a drawable, if possible
 *
 * @param view the view to convert/* w w w .  j a v a 2 s. c  o  m*/
 * @return the bitmap or {@code null} if the {@code view} is null
 */
@Nullable
public static Bitmap toBitmap(@Nullable final View view) {
    if (view == null) {
        return null;
    }
    final int width = view.getWidth();
    final int height = view.getHeight();
    final Bitmap bitmapToExport = Bitmap.createBitmap(width > 0 ? width : DEFAULT_BITMAP_WIDTH,
            height > 0 ? height : DEFAULT_BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmapToExport);
    view.draw(canvas);
    return bitmapToExport;
}

From source file:Main.java

public static boolean isEventInView(MotionEvent ev, View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);/*ww w  .  ja  v a2 s .  c  o m*/
    if (ev.getX() > location[0] && ev.getX() < location[0] + view.getWidth() && ev.getY() > location[1]
            && ev.getY() < location[1] + view.getHeight()) {

        return true;
    }
    return false;
}

From source file:Main.java

public static Bitmap createBitmapFromView(View view) {
    // Clear any focus from the view first to remove any cursor
    view.clearFocus();/*from   w  w  w.  java 2  s .  c  o m*/
    Bitmap drawingBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    drawingBitmap.eraseColor(Color.TRANSPARENT);
    Canvas canvas = new Canvas(drawingBitmap);
    view.draw(canvas);
    return drawingBitmap;
}