Wallpaper Cropper中的计算

时间:2015-09-08 09:04:36

标签: android wallpaper

我需要知道为什么这个额外空间被添加为左边距。由于这个wakllpaper没有在我的应用程序中正确设置。如果我尝试将最左边的部分设置为壁纸,那么由于此额外空间边距,中心部分将被设置为壁纸。 cropImageAndSetWallpaper(android.net.Uri uri,com.android.wallpapercropper.WallpaperCropActivity $ OnBitmapCroppedHandler onBitmapCroppedHandler,boolean finishActivityWhenDone)

    boolean centerCrop = getResources().getBoolean(R.bool.center_crop);
    // Get the crop
    boolean ltr = mCropView.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;

    Display d = getWindowManager().getDefaultDisplay();

    Point displaySize = new Point();
    d.getSize(displaySize);
    boolean isPortrait = displaySize.x < displaySize.y;

    Point defaultWallpaperSize = getDefaultWallpaperSize(getResources(),
            getWindowManager());
    // Get the crop
    RectF cropRect = mCropView.getCrop();

    Point inSize = mCropView.getSourceDimensions();

    int cropRotation = mCropView.getImageRotation();
    float cropScale = mCropView.getWidth() / (float) cropRect.width();

    Matrix rotateMatrix = new Matrix();
    rotateMatrix.setRotate(cropRotation);
    float[] rotatedInSize = new float[] { inSize.x, inSize.y };
    rotateMatrix.mapPoints(rotatedInSize);
    rotatedInSize[0] = Math.abs(rotatedInSize[0]);
    rotatedInSize[1] = Math.abs(rotatedInSize[1]);

    // Due to rounding errors in the cropview renderer the edges can be slightly offset
    // therefore we ensure that the boundaries are sanely defined
    cropRect.left = Math.max(0, cropRect.left);
    cropRect.right = Math.min(rotatedInSize[0], cropRect.right);
    cropRect.top = Math.max(0, cropRect.top);
    cropRect.bottom = Math.min(rotatedInSize[1], cropRect.bottom);

    // ADJUST CROP WIDTH
    // Extend the crop all the way to the right, for parallax
    // (or all the way to the left, in RTL)
    float extraSpace;
    if (centerCrop) {
        extraSpace = 2f * Math.min(rotatedInSize[0] - cropRect.right, cropRect.left);
    } else {
        extraSpace = ltr ? rotatedInSize[0] - cropRect.right : cropRect.left;
    }
    // Cap the amount of extra width
    float maxExtraSpace = defaultWallpaperSize.x / cropScale - cropRect.width();
    extraSpace = Math.min(extraSpace, maxExtraSpace);

    if (centerCrop) {
        cropRect.left -= extraSpace / 2f;
        cropRect.right += extraSpace / 2f;
    } else {
        if (ltr) {
            cropRect.right += extraSpace;
        } else {
            cropRect.left -= extraSpace;
        }
    }

    // ADJUST CROP HEIGHT
    if (isPortrait) {
        cropRect.bottom = cropRect.top + defaultWallpaperSize.y / cropScale;
    } else { // LANDSCAPE
        float extraPortraitHeight =
                defaultWallpaperSize.y / cropScale - cropRect.height();
        float expandHeight =
                Math.min(Math.min(rotatedInSize[1] - cropRect.bottom, cropRect.top),
                        extraPortraitHeight / 2);
        cropRect.top -= expandHeight;
        cropRect.bottom += expandHeight;
    }
    final int outWidth = (int) Math.round(cropRect.width() * cropScale);
    final int outHeight = (int) Math.round(cropRect.height() * cropScale);

    Runnable onEndCrop = new Runnable() {
        public void run() {
            if (finishActivityWhenDone) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        }
    };
    BitmapCropTask cropTask = new BitmapCropTask(this, uri,
            cropRect, cropRotation, outWidth, outHeight, true, false, onEndCrop);
    if (onBitmapCroppedHandler != null) {
        cropTask.setOnBitmapCropped(onBitmapCroppedHandler);
    }
    cropTask.execute();

1 个答案:

答案 0 :(得分:0)

为壁纸添加额外空间以实现视差效果。如果您的手机使用从右到左的布局,则此空间会添加到您选择的裁剪的左侧。谷歌的启动器在RTL布局的最右边页面开始。

如果你在图片的左侧选择一个裁剪,那么扩展将在裁剪的右侧进行,你将在发射器的主页面上看到主要是这个扩展。