【问题标题】:Crop image with important part in bottomright corner裁剪图像,右下角有重要部分
【发布时间】:2013-09-20 10:12:58
【问题描述】:

我正在设计一款必须在所有 Android 设备上看起来都不错的应用。在一个活动中,我想设置背景。我要使用的图像在右下角有一个重要的数字 我想要的是: - 保持纵横比 - 原始图像的右下角必须可见 - 全屏 - 必须在纵向和横向上工作

我已经尝试了所有的 scaletype 选项,fit 选项不会填满整个屏幕,并且 centercrop 会在所有侧面裁剪(所以它会切掉右下角的一部分)。

【问题讨论】:

  • 如前所述,适合选项不会填满整个屏幕。

标签: android imageview crop


【解决方案1】:

首先为你的drawable创建一个imageView并通过将<ImageView>更改为<com.packagename.CenterCropShiftsUp>对其进行自定义,并将scaleType设置为centerCrop。 在我刚才提到的包中创建CenterCropShiftsUp.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);
   }
}

它首先计算图像的 scaleFactor,然后使用这个比例来计算新的 drawableHeight(就像 centerCrop 一样)。通过这个高度,您可以计算 ImageView 的框架应该向上移动多远(使用setFrame() 使可绘制对象的底部与屏幕底部对齐)。

由于 centerCrop 本身的属性,您还要求的右侧对齐当然是自动修复的。

【讨论】:

    【解决方案2】:

    似乎有点晚了,但我想发布我的答案。我需要有一个左上角的视图,而宽度总是被裁剪的。我找到了这个库 (https://github.com/cesards/CropImageView),但我决定只使用它的一部分。它最终覆盖了setFrame,并在我的自定义图像视图的构造函数中将比例类型设置为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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 2019-07-19
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      相关资源
      最近更新 更多