Example usage for android.view Gravity RIGHT

List of usage examples for android.view Gravity RIGHT

Introduction

In this page you can find the example usage for android.view Gravity RIGHT.

Prototype

int RIGHT

To view the source code for android.view Gravity RIGHT.

Click Source Link

Document

Push object to the right of its container, not changing its size.

Usage

From source file:Main.java

public static void toast(Activity activity, String txt) {
    Toast toast = Toast.makeText(activity, txt, Toast.LENGTH_SHORT);
    int xoffset = 30, yoffset = 30;
    toast.setGravity(Gravity.RIGHT, xoffset, yoffset);
    toast.show();//from w ww.jav a  2s  . c  o m
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@SuppressLint("RtlHardcoded")
public static int getGravityEnd() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        return Gravity.END;
    }//from   w  w w  .j a  v  a2 s.  c  om
    return Gravity.RIGHT;
}

From source file:Main.java

public final static int[] getPopupPosition(View anchor, int gravity) {
    int[] position = new int[2];

    int windowWidth = anchor.getRootView().getMeasuredWidth();
    int windowHeight = anchor.getRootView().getMeasuredHeight();

    int anchorWidth = anchor.getMeasuredWidth();
    int anchorHeight = anchor.getMeasuredHeight();

    int[] location = new int[2];
    anchor.getLocationInWindow(location);

    if (Gravity.LEFT == (gravity & Gravity.LEFT)) {
        position[0] = location[0];/*from w  w w  .j  a v a2s. co m*/
    } else if (Gravity.RIGHT == (gravity & Gravity.RIGHT)) {
        position[0] = windowWidth - location[0] - anchorWidth;
    }

    if (Gravity.TOP == (gravity & Gravity.TOP)) {
        position[1] = location[1] + anchorHeight;
    } else if (Gravity.BOTTOM == (gravity & Gravity.BOTTOM)) {
        position[1] = windowHeight - location[1];
    }

    return position;
}

From source file:Main.java

public static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables != null) {
        if (drawables[0] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            onCenterDraw(view, canvas, drawables[0], Gravity.LEFT);
        } else if (drawables[1] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
            onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
        } else if (drawables[2] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
            onCenterDraw(view, canvas, drawables[2], Gravity.RIGHT);
        } else if (drawables[3] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
        }//  ww  w.j av a 2  s . c o m
    }
}

From source file:Main.java

protected static WindowManager.LayoutParams updateLayoutParams(boolean left, int y,
        WindowManager.LayoutParams params) {
    if (left) {//from   www.j a v  a 2s  . c  o m
        params.gravity = Gravity.LEFT | Gravity.TOP;
    } else {
        params.gravity = Gravity.RIGHT | Gravity.TOP;
    }
    params.y = y;
    return params;
}

From source file:com.jakewharton.behavior.drawer.DrawerBehavior.java

private static void validateGravity(int gravity) {
    if (gravity != Gravity.LEFT && gravity != Gravity.RIGHT && gravity != GravityCompat.START
            && gravity != GravityCompat.END) {
        throw new IllegalArgumentException("Only START, END, LEFT, or RIGHT gravity is supported.");
    }/*from  w w w .j a  v  a  2  s  . c  om*/
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Layout.Alignment getLayoutAlignment(TextView textView) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Layout.Alignment.ALIGN_NORMAL;
    }// w  w w.  ja  va2  s.c  o m

    Layout.Alignment alignment;
    switch (textView.getTextAlignment()) {
    case TextView.TEXT_ALIGNMENT_GRAVITY:
        switch (textView.getGravity() & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
        case Gravity.START:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
        case Gravity.END:
            alignment = Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.LEFT:
            alignment = (textView.getLayoutDirection() == TextView.LAYOUT_DIRECTION_RTL)
                    ? Layout.Alignment.ALIGN_OPPOSITE
                    : Layout.Alignment.ALIGN_NORMAL;
            break;
        case Gravity.RIGHT:
            alignment = (textView.getLayoutDirection() == TextView.LAYOUT_DIRECTION_RTL)
                    ? Layout.Alignment.ALIGN_NORMAL
                    : Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.CENTER_HORIZONTAL:
            alignment = Layout.Alignment.ALIGN_CENTER;
            break;
        default:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
        }
        break;
    case TextView.TEXT_ALIGNMENT_TEXT_START:
        alignment = Layout.Alignment.ALIGN_NORMAL;
        break;
    case TextView.TEXT_ALIGNMENT_TEXT_END:
        alignment = Layout.Alignment.ALIGN_OPPOSITE;
        break;
    case TextView.TEXT_ALIGNMENT_CENTER:
        alignment = Layout.Alignment.ALIGN_CENTER;
        break;
    case TextView.TEXT_ALIGNMENT_VIEW_START:
        alignment = Layout.Alignment.ALIGN_NORMAL;
        break;
    case TextView.TEXT_ALIGNMENT_VIEW_END:
        alignment = Layout.Alignment.ALIGN_OPPOSITE;
        break;
    case TextView.TEXT_ALIGNMENT_INHERIT:
        //
    default:
        alignment = Layout.Alignment.ALIGN_NORMAL;
        break;
    }
    return alignment;
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;//  w w  w  .j  a  v  a2  s.c  om

    switch (gravity) {
    case Gravity.RIGHT:
        ratio = -1;
    case Gravity.LEFT:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
        total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:Main.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*from ww  w.  j a va2  s  .  c  o m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        /* float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
         stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);*/
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:Main.java

/**
 * This helper method creates a 'nice' scrim or background protection for layering text over
 * an image. This non-linear scrim is less noticable than a linear or constant one.
 *
 * Borrowed from github.com/romannurik/muzei
 *
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*from  w ww.ja v a  2 s.c o m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        double x = i * 1f / (numStops - 1);
        double opacity = Math.max(0, Math.min(1, Math.pow(x, 3)));
        stopColors[i] = (baseColor & 0x00ffffff) | ((int) (alpha * opacity) << 24);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

    return paintDrawable;
}