Example usage for android.graphics Rect Rect

List of usage examples for android.graphics Rect Rect

Introduction

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

Prototype

public Rect(int left, int top, int right, int bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar./*from  w w  w  .j a  va2 s.  c om*/
 *
 * @param filePath file path to avatar image
 */
public static Bitmap publishAvatar(String filePath) {
    Bitmap bitmap = null;

    if (!TextUtils.isEmpty(filePath)) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filePath, opts);
            int size = 0;
            if (bitmap.getHeight() > bitmap.getWidth()) {
                size = bitmap.getWidth();
            } else {
                size = bitmap.getHeight();
            }
            Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, size, size);
            final RectF rectF = new RectF(rect);
            final float roundPx = 12;

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

            bitmap.recycle();

            return output;
        } catch (Exception e) {

        }
    }
    return null;
}

From source file:ca.psiphon.ploggy.Robohash.java

public static Bitmap getRobohash(Context context, boolean cacheCandidate, byte[] data)
        throws Utils.ApplicationError {
    try {//from w  w w . jav a  2  s  .c o m
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        byte[] digest = sha1.digest(data);

        String key = Utils.formatFingerprint(digest);
        Bitmap cachedBitmap = mCache.get(key);
        if (cachedBitmap != null) {
            return cachedBitmap;
        }

        ByteBuffer byteBuffer = ByteBuffer.wrap(digest);
        byteBuffer.order(ByteOrder.BIG_ENDIAN);
        // TODO: SecureRandom SHA1PRNG (but not LinuxSecureRandom)
        Random random = new Random(byteBuffer.getLong());

        AssetManager assetManager = context.getAssets();

        if (mConfig == null) {
            mConfig = new JSONObject(loadAssetToString(assetManager, CONFIG_FILENAME));
        }

        int width = mConfig.getInt("width");
        int height = mConfig.getInt("height");

        JSONArray colors = mConfig.getJSONArray("colors");
        JSONArray parts = colors.getJSONArray(random.nextInt(colors.length()));

        Bitmap robotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas robotCanvas = new Canvas(robotBitmap);

        for (int i = 0; i < parts.length(); i++) {
            JSONArray partChoices = parts.getJSONArray(i);
            String selection = partChoices.getString(random.nextInt(partChoices.length()));
            Bitmap partBitmap = loadAssetToBitmap(assetManager, selection);
            Rect rect = new Rect(0, 0, width, height);
            Paint paint = new Paint();
            paint.setAlpha(255);
            robotCanvas.drawBitmap(partBitmap, rect, rect, paint);
            partBitmap.recycle();
        }

        if (cacheCandidate) {
            mCache.set(key, robotBitmap);
        }

        return robotBitmap;

    } catch (IOException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    } catch (JSONException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    } catch (NoSuchAlgorithmException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    }
}

From source file:android.car.cluster.sample.NavigationFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.i(TAG, "onCreateView");
    mDisplayManager = getActivity().getSystemService(DisplayManager.class);
    mDisplayManager.registerDisplayListener(mDisplayListener, new Handler());

    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_navigation, container, false);

    mSurfaceView = root.findViewById(R.id.nav_surface);
    mSurfaceView.getHolder().addCallback(new Callback() {
        @Override//  www .j  a  v a 2 s  . c om
        public void surfaceCreated(SurfaceHolder holder) {
            Log.i(TAG, "surfaceCreated, holder: " + holder);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.i(TAG, "surfaceChanged, holder: " + holder + ", size:" + width + "x" + height + ", format:"
                    + format);

            //Create dummy unobscured area to report to navigation activity.
            mUnobscuredBounds = new Rect(40, 0, width - 80, height - 40);

            if (mVirtualDisplay == null) {
                mVirtualDisplay = createVirtualDisplay(holder.getSurface(), width, height);
            } else {
                mVirtualDisplay.setSurface(holder.getSurface());
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.i(TAG, "surfaceDestroyed, holder: " + holder + ", detaching surface from"
                    + " display, surface: " + holder.getSurface());
            // detaching surface is similar to turning off the display
            mVirtualDisplay.setSurface(null);
        }
    });

    return root;
}

From source file:com.nloko.android.Utils.java

public static Bitmap crop(Bitmap bitmapToCrop, int destHeight, int destWidth) {
    int width = bitmapToCrop.getWidth();
    int height = bitmapToCrop.getHeight();
    if (width <= destWidth && height <= destHeight) {
        return bitmapToCrop;
    }// w  w  w.j a  v a  2 s  .c  o m

    Bitmap b = Bitmap.createBitmap(destHeight, destWidth, Bitmap.Config.RGB_565);
    Canvas c1 = new Canvas(b);

    int midpointX = width / 2;
    int midpointY = height / 2;

    Rect r = new Rect(midpointX - destWidth / 2, midpointY - destHeight / 2, midpointX + destWidth / 2,
            midpointY + destHeight / 2);

    int left = 0; //(width / 2) - (bitmapToCrop.getWidth() / 2);
    int top = 0; //(height / 2) - (bitmapToCrop.getWidth() / 2);
    c1.drawBitmap(bitmapToCrop, r, new Rect(left, top, left + destWidth, top + destHeight), null);

    return b;
}

From source file:de.madvertise.android.sdk.MadView.java

/**
 * Constructor/*www . j  a  v  a 2s  .c  om*/
 * 
 * @param context
 * @param attrs
 */
public MadView(Context context, AttributeSet attrs) {
    super(context, attrs);

    MadUtil.logMessage(null, Log.DEBUG, "** Constructor for mad view called **");
    setVisibility(INVISIBLE);

    if (context.checkCallingOrSelfPermission(
            android.Manifest.permission.INTERNET) == PackageManager.PERMISSION_DENIED) {

        MadUtil.logMessage(null, Log.DEBUG, " *** ----------------------------- *** ");
        MadUtil.logMessage(null, Log.DEBUG, " *** Missing internet permissions! *** ");
        MadUtil.logMessage(null, Log.DEBUG, " *** ----------------------------- *** ");
        throw new IllegalArgumentException();
    }

    initParameters(attrs);

    Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    MadUtil.logMessage(null, Log.DEBUG,
            "Display values: Width = " + display.getWidth() + " ; Height = " + display.getHeight());

    setGravity(Gravity.CENTER);

    initialBackground = this.getBackground();
    Rect r = new Rect(0, 0, display.getWidth(), display.getHeight());
    textBannerBackground = generateBackgroundDrawable(r, backgroundColor, 0xffffff);

    setClickable(true);
    setFocusable(true);
    setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}

From source file:com.gmail.altakey.lucene.AsyncImageLoader.java

protected BitmapDrawable doInBackground(Void... args) {
    final Context context = this.view.getContext();
    final Resources res = context.getResources();

    try {/*from  www. java2 s. co  m*/
        InputStream in = this.read(new ProgressReportingInputStream.ProgressListener() {
            public void onAdvance(long at, long size) {
                publishProgress(at, size);
            }
        });
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inDither = true;
        bfo.inPreferredConfig = Bitmap.Config.RGB_565;
        try {
            bfo.inPreferQualityOverSpeed = true;
        } catch (NoSuchFieldError e) {
        }
        Bitmap bitmap = BitmapFactory.decodeStream(in, new Rect(-1, -1, -1, -1), bfo);
        return new BitmapDrawable(res, this.scale(bitmap));
    } catch (FileNotFoundException e) {
        return null;
    } catch (OutOfMemoryError e) {
        this.oomMessage.show();
        return null;
    }
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar./* w  ww  . j a v  a  2  s.  com*/
 *
 * @param rawBitmap bitmap to round corners
 */
public static Bitmap publishAvatar(Bitmap rawBitmap, int roundK) {
    if (rawBitmap == null)
        return null;

    try {
        int size = 0;
        if (rawBitmap.getHeight() > rawBitmap.getWidth()) {
            size = rawBitmap.getWidth();
        } else {
            size = rawBitmap.getHeight();
        }
        Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, size, size);
        final RectF rectF = new RectF(rect);
        final float roundPx = roundK;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(rawBitmap, rect, rect, paint);

        rawBitmap.recycle();

        return output;
    } catch (Exception e) {

    }

    return null;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float pixels) {
    if (bitmap == null) {
        Log.e(TAG, "getRoundedCornerBitmap - null bitmap");
        return null;
    }//from ww  w . j a  v a 2  s  .com

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, pixels, pixels, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:com.polyvi.xface.extension.capture.XCaptureScreenImpl.java

/**
 * ?xy??widthheight//from   ww w  .  ja  v a 2s.  c om
 */
private XCaptureScreenOptions getValidOptions(Bitmap bitmap) {
    int captureRectX = mOptions.getX();
    int captureRectY = mOptions.getY();
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    int width = mOptions.getWidth();
    int height = mOptions.getHeight();
    Rect captureRect = new Rect(0, 0, 0, 0);
    /**?x y??xy*/
    if (mOptions.iSOnlyXAndYInput()) {
        captureRect = new Rect(captureRectX, captureRectY, bitmapWidth + captureRectX,
                bitmapHeight + captureRectY);
    } else {
        captureRect = new Rect(captureRectX, captureRectY, width + captureRectX, height + captureRectY);
    }
    Rect bitmapRect = new Rect(0, 0, bitmapWidth, bitmapHeight);
    if (captureRect.intersect(bitmapRect)) {
        mOptions.setX(captureRect.left);
        mOptions.setY(captureRect.top);
        mOptions.setWidth(captureRect.right - captureRect.left);
        mOptions.setHeight(captureRect.bottom - captureRect.top);
        /** ???*/
        if (mOptions.getWidth() <= 0 || mOptions.getHeight() <= 0) {
            return null;
        }
        return mOptions;
    } else {
        return null;
    }
}

From source file:com.cellbots.remoteEyes.RemoteEyesActivity.java

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    mHolder.setFixedSize(w, h);//from  w  w w . j  ava2  s. com
    // Start the preview
    Parameters params = mCamera.getParameters();
    previewHeight = params.getPreviewSize().height;
    previewWidth = params.getPreviewSize().width;
    previewFormat = params.getPreviewFormat();

    // Crop the edges of the picture to reduce the image size
    r = new Rect(100, 100, previewWidth - 100, previewHeight - 100);

    mCallbackBuffer = new byte[460800];

    mCamera.setParameters(params);
    mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
        public void onPreviewFrame(byte[] imageData, Camera arg1) {
            if (!isUploading) {
                isUploading = true;
                uploadImage(imageData);
            }
        }
    });
    mCamera.addCallbackBuffer(mCallbackBuffer);
    mCamera.startPreview();
    setTorchMode(mTorchMode);
}