Android 裁剪图像,重要部分位于右下角

Android 裁剪图像,重要部分位于右下角,android,imageview,crop,Android,Imageview,Crop,我正在设计一款应用程序,它必须在所有安卓设备上都很好看。在活动中,我要设置背景。我要使用的图像在右下角有一个重要的数字 我想要的是: -保持纵横比 -原始图像的右下角必须可见 -全屏 -必须从事肖像画和风景画 我已经尝试了所有的缩放类型选项,但“适合”选项并没有填满整个屏幕,而且中心裁剪不会在所有侧面进行,因此它会分割右下角的一部分。首先为您的可绘制对象创建一个imageView,并通过更改为对其进行自定义,然后将缩放类型设置为中心裁剪。 在我刚才提到的包中创建CenterCropshift s

我正在设计一款应用程序,它必须在所有安卓设备上都很好看。在活动中,我要设置背景。我要使用的图像在右下角有一个重要的数字 我想要的是: -保持纵横比 -原始图像的右下角必须可见 -全屏 -必须从事肖像画和风景画


我已经尝试了所有的缩放类型选项,但“适合”选项并没有填满整个屏幕,而且中心裁剪不会在所有侧面进行,因此它会分割右下角的一部分。

首先为您的可绘制对象创建一个imageView,并通过更改为对其进行自定义,然后将缩放类型设置为中心裁剪。 在我刚才提到的包中创建CenterCropshift sup.java,并使用以下代码向上移动drawable:

package nl.mijnverzekering.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CenterCropShiftsUp extends ImageView
{
   public CenterCropShiftsUp(Context context, AttributeSet attrs)
   {
      super(context, attrs);
   }

   @Override
   protected boolean setFrame(int l, int t, int r, int b)
   {
      int drawableWidth = getDrawable().getIntrinsicWidth();
      int drawableHeight = getDrawable().getIntrinsicHeight();

      int viewWidth = r - getPaddingLeft() - getPaddingRight();
      int viewHeight = b - getPaddingTop() - getPaddingBottom();

      float heightRatio = 1 / ((float) drawableHeight / (float) viewHeight);
      float widthRatio = 1 / ((float) drawableWidth / (float) viewWidth);

      // Choose the biggest ratio as scaleFactor
      // (centerCrop does the same: the drawable never scales down to leave part of the screen empty)
      float scale = heightRatio > widthRatio ? heightRatio : widthRatio;

      int newDrawableHeight = (int) (scale * (float) drawableHeight);

      // Shifts the t (top) of the imageFrame up (t -=)
      // This calculation aligns the bottom of the drawable to the bottom of the screen
      t -= (newDrawableHeight - b);

      return super.setFrame(l, t, r, b);
   }
}
它首先计算图像的比例因子,然后使用此比例计算新的可绘制高度,就像centerCrop那样。使用此高度,您可以使用setFrame计算ImageView的框架应向上移动多远,以使可绘制屏幕的底部与屏幕的底部对齐


当然,由于centerCrop本身的属性,右侧的对齐方式(您也请求了对齐方式)会自动修复。

首先为您的可绘制图像创建一个imageView,并通过更改为对其进行自定义,然后将scaleType设置为centerCrop。 在我刚才提到的包中创建CenterCropshift sup.java,并使用以下代码向上移动drawable:

package nl.mijnverzekering.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CenterCropShiftsUp extends ImageView
{
   public CenterCropShiftsUp(Context context, AttributeSet attrs)
   {
      super(context, attrs);
   }

   @Override
   protected boolean setFrame(int l, int t, int r, int b)
   {
      int drawableWidth = getDrawable().getIntrinsicWidth();
      int drawableHeight = getDrawable().getIntrinsicHeight();

      int viewWidth = r - getPaddingLeft() - getPaddingRight();
      int viewHeight = b - getPaddingTop() - getPaddingBottom();

      float heightRatio = 1 / ((float) drawableHeight / (float) viewHeight);
      float widthRatio = 1 / ((float) drawableWidth / (float) viewWidth);

      // Choose the biggest ratio as scaleFactor
      // (centerCrop does the same: the drawable never scales down to leave part of the screen empty)
      float scale = heightRatio > widthRatio ? heightRatio : widthRatio;

      int newDrawableHeight = (int) (scale * (float) drawableHeight);

      // Shifts the t (top) of the imageFrame up (t -=)
      // This calculation aligns the bottom of the drawable to the bottom of the screen
      t -= (newDrawableHeight - b);

      return super.setFrame(l, t, r, b);
   }
}
它首先计算图像的比例因子,然后使用此比例计算新的可绘制高度,就像centerCrop那样。使用此高度,您可以使用setFrame计算ImageView的框架应向上移动多远,以使可绘制屏幕的底部与屏幕的底部对齐


由于centerCrop本身的属性,右侧的对齐方式(您也请求了对齐方式)当然会自动修复。

似乎有点晚了,不过我想发布我的答案。我需要一个左上角移动的视图,而宽度总是被裁剪的。我找到了这个图书馆,但我决定只使用其中的一部分。它最终在自定义图像视图的构造函数中覆盖setFrameand并将缩放类型设置为Matrix

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    boolean changed = super.setFrame(l, t, r, b);

    int viewWidth = r - getPaddingLeft() - getPaddingRight();
    int viewHeight = b - getPaddingTop() - getPaddingBottom();

    if (viewHeight > 0 && viewWidth > 0) {
        final Matrix matrixCopy = new Matrix();
        matrixCopy.set(getImageMatrix());

        final Drawable drawable = getDrawable();
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();

        float scaleY = (float) viewHeight / (float) drawableHeight;
        float scaleX = (float) viewWidth / (float) drawableWidth;
        float scale = scaleX > scaleY ? scaleX : scaleY;
        matrixCopy.setScale(scale, scale);
        setImageMatrix(matrixCopy);
    }
    return changed;
}

似乎有点晚了,不过我想把我的答案贴出来。我需要一个左上角移动的视图,而宽度总是被裁剪的。我找到了这个图书馆,但我决定只使用其中的一部分。它最终在自定义图像视图的构造函数中覆盖setFrameand并将缩放类型设置为Matrix

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    boolean changed = super.setFrame(l, t, r, b);

    int viewWidth = r - getPaddingLeft() - getPaddingRight();
    int viewHeight = b - getPaddingTop() - getPaddingBottom();

    if (viewHeight > 0 && viewWidth > 0) {
        final Matrix matrixCopy = new Matrix();
        matrixCopy.set(getImageMatrix());

        final Drawable drawable = getDrawable();
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();

        float scaleY = (float) viewHeight / (float) drawableHeight;
        float scaleX = (float) viewWidth / (float) drawableWidth;
        float scale = scaleX > scaleY ? scaleX : scaleY;
        matrixCopy.setScale(scale, scale);
        setImageMatrix(matrixCopy);
    }
    return changed;
}

如前所述,贴合选项不会占据整个屏幕。如前所述,贴合选项不会占据整个屏幕。