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(Rect r) 

Source Link

Document

Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).

Usage

From source file:ar.worq.microturistas.view.ScrimInsetsFrameLayout.java

@Override
protected boolean fitSystemWindows(Rect insets) {
    mInsets = new Rect(insets);
    setWillNotDraw(mInsetForeground == null);
    ViewCompat.postInvalidateOnAnimation(this);
    if (mOnInsetsCallback != null) {
        mOnInsetsCallback.onInsetsChanged(insets);
    }/* w w  w  .j a  v a2 s  .  co  m*/
    return true; // consume insets
}

From source file:app.philm.in.view.InsetFrameLayout.java

@Override
protected boolean fitSystemWindows(Rect insets) {
    mInsets = new Rect(insets);

    setWillNotDraw(mInsetBackground == null);
    ViewCompat.postInvalidateOnAnimation(this);

    if (mOnInsetsCallback != null) {
        mOnInsetsCallback.onInsetsChanged(insets);
    }//from   w  ww. j av  a  2s.c o m
    return true; // consume insets
}

From source file:com.shizhefei.view.largeimage.ImageManager.java

/**
 * @param imageScale width/?width imageScale = 4400???100?<br>
 *                   imageScale??position?blockSize?<br>
 *                   blockSize*imageScale?,
 *                   imageScale??positionblockSize
 *///from   w w w .j a  v  a 2 s . co  m
public List<DrawData> getDrawData(float imageScale, Rect imageRect) {
    long startTime = SystemClock.uptimeMillis();
    LoadData loadData = mLoadData;
    if (loadData == null || loadData.mDecoder == null) {
        return new ArrayList<DrawData>(0);
    }
    int imageWidth = loadData.mImageWidth;
    int imageHeight = loadData.mImageHeight;
    List<CacheData> cacheDatas = loadData.mCacheDatas;
    Bitmap cacheImageData = loadData.mCacheImageData;
    int cacheImageScale = loadData.mCacheImageScale;

    List<DrawData> drawDatas = new ArrayList<DrawData>();
    if (imageRect.left < 0) {
        imageRect.left = 0;
    }
    if (imageRect.top < 0) {
        imageRect.top = 0;
    }
    if (imageRect.right > loadData.mImageWidth) {
        imageRect.right = loadData.mImageWidth;
    }
    if (imageRect.bottom > loadData.mImageHeight) {
        imageRect.bottom = loadData.mImageHeight;
    }

    if (cacheImageData == null) {
        try {
            int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
            int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
            int s = (int) Math.sqrt(imageWidth * imageHeight / (screenWidth / 2) / (screenHeight / 2));
            cacheImageScale = getNearScale(s);
            if (cacheImageScale < s) {
                cacheImageScale *= 2;
            }
            handler.sendMessage(handler.obtainMessage(MESSAGE_PIC, cacheImageScale));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Rect cacheImageRect = new Rect(imageRect);
        int cache = dip2px(context, 100);
        cache = (int) (cache * imageScale);
        cacheImageRect.right += cache;
        cacheImageRect.top -= cache;
        cacheImageRect.left -= cache;
        cacheImageRect.bottom += cache;

        if (cacheImageRect.left < 0) {
            cacheImageRect.left = 0;
        }
        if (cacheImageRect.top < 0) {
            cacheImageRect.top = 0;
        }
        if (cacheImageRect.right > imageWidth) {
            cacheImageRect.right = imageWidth;
        }
        if (cacheImageRect.bottom > imageHeight) {
            cacheImageRect.bottom = imageHeight;
        }
        Rect r = new Rect();
        r.left = (int) Math.abs(1.0f * cacheImageRect.left / cacheImageScale);
        r.right = (int) Math.abs(1.0f * cacheImageRect.right / cacheImageScale);
        r.top = (int) Math.abs(1.0f * cacheImageRect.top / cacheImageScale);
        r.bottom = (int) Math.abs(1.0f * cacheImageRect.bottom / cacheImageScale);
        drawDatas.add(new DrawData(cacheImageData, r, cacheImageRect));

        Log.d("vvvv", "imageRect:" + imageRect + " tempImageScale:" + cacheImageScale);
        Log.d("vvvv", "rect:" + r);
    }
    int scale = getNearScale(imageScale);
    if (cacheImageScale <= scale && cacheImageData != null) {
        return drawDatas;
    }
    // if (true) {
    // return drawDatas;
    // }
    Log.d("dddd", "scale: " + scale);
    int blockSize = BASE_BLOCKSIZE * scale;
    int maxRow = imageHeight / blockSize + (imageHeight % blockSize == 0 ? 0 : 1);
    int maxCol = imageWidth / blockSize + (imageWidth % blockSize == 0 ? 0 : 1);

    // scaleposition
    int startRow = imageRect.top / blockSize + (imageRect.top % blockSize == 0 ? 0 : 1) - 1;
    int endRow = imageRect.bottom / blockSize + (imageRect.bottom % blockSize == 0 ? 0 : 1);
    int startCol = imageRect.left / blockSize + (imageRect.left % blockSize == 0 ? 0 : 1) - 1;
    int endCol = imageRect.right / blockSize + (imageRect.right % blockSize == 0 ? 0 : 1);
    if (startRow < 0) {
        startRow = 0;
    }
    if (startCol < 0) {
        startCol = 0;
    }
    if (endRow > maxRow) {
        endRow = maxRow;
    }
    if (endCol > maxCol) {
        endCol = maxCol;
    }

    int cacheStartRow = startRow - 1;
    int cacheEndRow = endRow + 1;
    int cacheStartCol = startCol - 1;
    int cacheEndCol = endCol + 1;
    if (cacheStartRow < 0) {
        cacheStartRow = 0;
    }
    if (cacheStartCol < 0) {
        cacheStartCol = 0;
    }
    if (cacheEndRow > maxRow) {
        cacheEndRow = maxRow;
    }
    if (cacheEndCol > maxCol) {
        cacheEndCol = maxCol;
    }

    Log.d("countTime", "preTime :" + (SystemClock.uptimeMillis() - startTime));
    startTime = SystemClock.uptimeMillis();

    Set<Position> needShowPositions = new HashSet<Position>();

    // ?
    handler.removeMessages(preMessageWhat);
    int what = preMessageWhat == MESSAGE_BLOCK_1 ? MESSAGE_BLOCK_2 : MESSAGE_BLOCK_1;
    preMessageWhat = what;

    if (loadData.mCurrentCacheData != null && loadData.mCurrentCacheData.scale != scale) {
        cacheDatas.add(new CacheData(loadData.mCurrentCacheData.scale,
                new HashMap<Position, Bitmap>(loadData.mCurrentCacheData.images)));
        loadData.mCurrentCacheData = null;
    }
    if (loadData.mCurrentCacheData == null) {
        Iterator<CacheData> iterator = cacheDatas.iterator();
        while (iterator.hasNext()) {
            CacheData cacheData = iterator.next();
            if (scale == cacheData.scale) {
                loadData.mCurrentCacheData = new CacheData(scale,
                        new ConcurrentHashMap<Position, Bitmap>(cacheData.images));
                iterator.remove();
            }
        }
    }
    if (loadData.mCurrentCacheData == null) {
        loadData.mCurrentCacheData = new CacheData(scale, new ConcurrentHashMap<Position, Bitmap>());
        for (int row = startRow; row <= endRow; row++) {
            for (int col = startCol; col <= endCol; col++) {
                Position position = new Position(row, col);
                needShowPositions.add(position);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }

        /**
         * <pre>
         * #########  1
         * #       #
         * #       #
         * #########
         *
         * <pre>
         */

        //  #########
        for (int row = cacheStartRow; row < startRow; row++) {
            for (int col = cacheStartCol; col <= cacheEndCol; col++) {
                Position position = new Position(row, col);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        //  #########
        for (int row = endRow + 1; row < cacheEndRow; row++) {
            for (int col = cacheStartCol; col <= cacheEndCol; col++) {
                Position position = new Position(row, col);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        // # 
        // #
        for (int row = startRow; row < endRow; row++) {
            for (int col = cacheStartCol; col < startCol; col++) {
                Position position = new Position(row, col);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        // # ?
        // #
        for (int row = startRow; row < endRow; row++) {
            for (int col = endRow + 1; col < cacheEndRow; col++) {
                Position position = new Position(row, col);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
    } else {
        /*
         * scale?position
        */
        Set<Position> usePositions = new HashSet<ImageManager.Position>();
        for (int row = startRow; row <= endRow; row++) {
            for (int col = startCol; col <= endCol; col++) {
                Position position = new Position(row, col);
                Bitmap bitmap = loadData.mCurrentCacheData.images.get(position);
                if (bitmap == null) {
                    needShowPositions.add(position);
                    handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
                } else {
                    usePositions.add(position);
                    Rect rect = madeRect(bitmap, row, col, scale, imageScale);
                    drawDatas.add(new DrawData(bitmap, null, rect));
                }
            }
        }
        //  #########
        for (int row = cacheStartRow; row < startRow; row++) {
            for (int col = cacheStartCol; col <= cacheEndCol; col++) {
                Position position = new Position(row, col);
                usePositions.add(position);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        //  #########
        for (int row = endRow + 1; row < cacheEndRow; row++) {
            for (int col = cacheStartCol; col <= cacheEndCol; col++) {
                Position position = new Position(row, col);
                usePositions.add(position);
                Log.d("9999", " " + position);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        // # 
        // #
        for (int row = startRow; row < endRow; row++) {
            for (int col = cacheStartCol; col < startCol; col++) {
                Position position = new Position(row, col);
                usePositions.add(position);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        // # ?
        // #
        for (int row = startRow; row < endRow; row++) {
            for (int col = endRow + 1; col < cacheEndRow; col++) {
                Position position = new Position(row, col);
                usePositions.add(position);
                handler.sendMessage(handler.obtainMessage(what, new MessageData(position, scale)));
            }
        }
        loadData.mCurrentCacheData.images.keySet().retainAll(usePositions);
    }

    Log.d("countTime", "currentScale time :" + (SystemClock.uptimeMillis() - startTime));
    startTime = SystemClock.uptimeMillis();

    //
    if (!needShowPositions.isEmpty()) {
        Collections.sort(cacheDatas, new NearComparator(scale));
        Iterator<CacheData> iterator = cacheDatas.iterator();
        while (iterator.hasNext()) {
            CacheData cacheData = iterator.next();
            int scaleKey = cacheData.scale;
            if (scaleKey / scale == 2) {// scale.,???

                Log.d("countTime", ",? time :" + (SystemClock.uptimeMillis() - startTime));
                startTime = SystemClock.uptimeMillis();

                int ds = scaleKey / scale;

                // ??
                int size = scale * BASE_BLOCKSIZE;

                // 
                int startRowKey = cacheStartRow / 2;
                int endRowKey = cacheEndRow / 2;
                int startColKey = cacheStartCol / 2;
                int endColKey = cacheEndCol / 2;

                Iterator<Entry<Position, Bitmap>> imageiterator = cacheData.images.entrySet().iterator();
                while (imageiterator.hasNext()) {
                    Entry<Position, Bitmap> entry = imageiterator.next();
                    Position position = entry.getKey();
                    if (!(startRowKey <= position.row && position.row <= endRowKey
                            && startColKey <= position.col && position.col <= endColKey)) {
                        imageiterator.remove();
                    }
                }

                Iterator<Entry<Position, Bitmap>> imagesIterator = cacheData.images.entrySet().iterator();
                while (imagesIterator.hasNext()) {
                    Entry<Position, Bitmap> entry = imagesIterator.next();
                    Position position = entry.getKey();
                    int startPositionRow = position.row * ds;
                    int endPositionRow = startPositionRow + ds;
                    int startPositionCol = position.col * ds;
                    int endPositionCol = startPositionCol + ds;
                    Bitmap bitmap = entry.getValue();
                    int iW = bitmap.getWidth();
                    int iH = bitmap.getHeight();

                    // ???
                    int blockImageSize = BASE_BLOCKSIZE / ds;

                    Log.d("nnnn", " bitmap.getWidth():" + bitmap.getWidth() + " imageHeight:" + iH);
                    for (int row = startPositionRow, i = 0; row <= endPositionRow; row++, i++) {
                        int top = i * blockImageSize;
                        if (top >= iH) {
                            break;
                        }
                        for (int col = startPositionCol, j = 0; col <= endPositionCol; col++, j++) {
                            int left = j * blockImageSize;
                            if (left >= iW) {
                                break;
                            }
                            if (needShowPositions.remove(new Position(row, col))) {
                                int right = left + blockImageSize;
                                int bottom = top + blockImageSize;
                                if (right > iW) {
                                    right = iW;
                                }
                                if (bottom > iH) {
                                    bottom = iH;
                                }
                                Rect rect = new Rect();
                                rect.left = col * size;
                                rect.top = row * size;
                                rect.right = rect.left + (right - left) * scaleKey;
                                rect.bottom = rect.top + (bottom - top) * scaleKey;
                                drawDatas.add(new DrawData(bitmap, new Rect(left, top, right, bottom), rect));
                            }
                        }
                    }
                }

                Log.d("countTime", ",? time :" + (SystemClock.uptimeMillis() - startTime));
            } else if (scale / scaleKey == 2) {// ???
                int size = scaleKey * BASE_BLOCKSIZE;

                Log.d("countTime", "   time :" + (SystemClock.uptimeMillis() - startTime));
                startTime = SystemClock.uptimeMillis();

                int startRowKey = imageRect.top / size + (imageRect.top % size == 0 ? 0 : 1) - 1;
                int endRowKey = imageRect.bottom / size + (imageRect.bottom % size == 0 ? 0 : 1);
                int startColKey = imageRect.left / size + (imageRect.left % size == 0 ? 0 : 1) - 1;
                int endColKey = imageRect.right / size + (imageRect.right % size == 0 ? 0 : 1);

                Log.d("nnnn",
                        "startRowKey" + startRowKey + " endRowKey:+" + endRowKey + " endColKey:" + endColKey);

                Position tempPosition = new Position();
                Iterator<Entry<Position, Bitmap>> imageiterator = cacheData.images.entrySet().iterator();
                while (imageiterator.hasNext()) {
                    Entry<Position, Bitmap> entry = imageiterator.next();
                    Position position = entry.getKey();
                    if (!(startRowKey <= position.row && position.row <= endRowKey
                            && startColKey <= position.col && position.col <= endColKey)) {
                        imageiterator.remove();
                        Log.d("nnnn", "position:" + position + " remove");
                    } else {
                        Bitmap bitmap = entry.getValue();
                        tempPosition.set(position.row / 2 + (position.row % 2 == 0 ? 0 : 1),
                                position.col / 2 + (position.col % 2 == 0 ? 0 : 1));
                        if (needShowPositions.contains(tempPosition)) {
                            Rect rect = new Rect();
                            rect.left = position.col * size;
                            rect.top = position.row * size;
                            rect.right = rect.left + bitmap.getWidth() * scaleKey;
                            rect.bottom = rect.top + bitmap.getHeight() * scaleKey;
                            drawDatas.add(new DrawData(bitmap, null, rect));
                        }
                    }
                }

                Log.d("countTime", "   time :" + (SystemClock.uptimeMillis() - startTime));
                startTime = SystemClock.uptimeMillis();
            } else {
                iterator.remove();
            }
        }
    }
    return drawDatas;
}

From source file:com.mirasense.scanditsdk.plugin.ScanditSDK.java

/**
 * Adjusts the layout parameters of the barcode picker according to the margins set through java script.
 *
 * @param bundle The bundle with the java script parameters.
 * @param animationDuration Over how long the change should be animated.
 *//*from w ww.  j  ava 2 s.c  o  m*/
private void adjustLayout(Bundle bundle, double animationDuration) {
    if (mBarcodePicker == null) {
        return;
    }
    final RelativeLayout.LayoutParams rLayoutParams = (RelativeLayout.LayoutParams) mBarcodePicker
            .getLayoutParams();

    Display display = ((WindowManager) webView.getContext().getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    if (bundle.containsKey(ScanditSDKParameterParser.paramPortraitMargins)
            && display.getHeight() > display.getWidth()) {
        String portraitMargins = bundle.getString(ScanditSDKParameterParser.paramPortraitMargins);
        String[] split = portraitMargins.split("[/]");
        if (split.length == 4) {
            try {
                final Rect oldPortraitMargins = new Rect(mPortraitMargins);
                mPortraitMargins = new Rect(Integer.valueOf(split[0]), Integer.valueOf(split[1]),
                        Integer.valueOf(split[2]), Integer.valueOf(split[3]));
                if (animationDuration > 0) {
                    Animation anim = new Animation() {
                        @Override
                        protected void applyTransformation(float interpolatedTime, Transformation t) {
                            rLayoutParams.topMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                                    (int) (oldPortraitMargins.top
                                            + (mPortraitMargins.top - oldPortraitMargins.top)
                                                    * interpolatedTime));
                            rLayoutParams.rightMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                                    (int) (oldPortraitMargins.right
                                            + (mPortraitMargins.right - oldPortraitMargins.right)
                                                    * interpolatedTime));
                            rLayoutParams.bottomMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                                    (int) (oldPortraitMargins.bottom
                                            + (mPortraitMargins.bottom - oldPortraitMargins.bottom)
                                                    * interpolatedTime));
                            rLayoutParams.leftMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                                    (int) (oldPortraitMargins.left
                                            + (mPortraitMargins.left - oldPortraitMargins.left)
                                                    * interpolatedTime));
                            mBarcodePicker.setLayoutParams(rLayoutParams);
                        }
                    };
                    anim.setDuration((int) (animationDuration * 1000));
                    mBarcodePicker.startAnimation(anim);
                } else {

                    rLayoutParams.topMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            mPortraitMargins.top);
                    rLayoutParams.rightMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            mPortraitMargins.right);
                    rLayoutParams.bottomMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            mPortraitMargins.bottom);
                    rLayoutParams.leftMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            mPortraitMargins.left);
                    mBarcodePicker.setLayoutParams(rLayoutParams);
                }
            } catch (NumberFormatException e) {

            }
        }
    } else if (bundle.containsKey(ScanditSDKParameterParser.paramLandscapeMargins)
            && display.getWidth() > display.getHeight()) {
        String landscapeMargins = bundle.getString(ScanditSDKParameterParser.paramLandscapeMargins);
        String[] split = landscapeMargins.split("[/]");
        if (split.length == 4) {
            final Rect oldLandscapeMargins = new Rect(mLandscapeMargins);
            mLandscapeMargins = new Rect(Integer.valueOf(split[0]), Integer.valueOf(split[1]),
                    Integer.valueOf(split[2]), Integer.valueOf(split[3]));

            Animation anim = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    rLayoutParams.topMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            (int) (oldLandscapeMargins.top
                                    + (mLandscapeMargins.top - oldLandscapeMargins.top) * interpolatedTime));
                    rLayoutParams.rightMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            (int) (oldLandscapeMargins.right
                                    + (mLandscapeMargins.right - oldLandscapeMargins.right)
                                            * interpolatedTime));
                    rLayoutParams.bottomMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            (int) (oldLandscapeMargins.bottom
                                    + (mLandscapeMargins.bottom - oldLandscapeMargins.bottom)
                                            * interpolatedTime));
                    rLayoutParams.leftMargin = SbSystemUtils.pxFromDp(webView.getContext(),
                            (int) (oldLandscapeMargins.left
                                    + (mLandscapeMargins.left - oldLandscapeMargins.left) * interpolatedTime));
                    mBarcodePicker.setLayoutParams(rLayoutParams);
                }
            };
            anim.setDuration((int) (animationDuration * 1000));
            mBarcodePicker.startAnimation(anim);
        }
    }
}

From source file:ac.robinson.paperchains.PaperChainsActivity.java

private void saveAudio() {
    mSaveButton.setClickable(false); // don't allow clicks regardless of status

    String accessToken = getSoundCloudAccessToken();
    if (TextUtils.isEmpty(accessToken)) {
        // TODO: if they *do* have SoundCloud installed, use the in-app token method
        startActivityForResult(new Intent(PaperChainsActivity.this, SoundCloudLoginActivity.class),
                SOUNDCLOUD_LOGIN_RESULT);
    } else {/*  w  ww  .ja  v a2  s  .  c  om*/
        animateRecordingInterface(-1, mSaveButton); // -1 = animate in
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSaveButton.setImageResource(R.drawable.ic_refresh_white_24dp);
                mSaveButton.startAnimation(mRotateAnimation);
            }
        }, BUTTON_ANIMATION_DURATION); // delayed so that the rotation doesn't interfere with the inwards animation

        new SoundCloudUploadTask(PaperChainsActivity.this, sSoundCloudUploaderApiWrapper,
                new Token(accessToken, null, Token.SCOPE_NON_EXPIRING), mPageId, new Rect(mCurrentAudioRect))
                        .execute(mAudioRecorder.getRecordFileName());
    }
}