安卓图片视图顶部裁剪

4
我正在开发一个Android应用程序,需要在屏幕顶部使用类似操作栏的标题栏......我希望它是透明的,但当您开始滚动视图时,视图的所有内容都会隐藏在其后面,因此我决定使用ImageView(在任何片段中都必须不同,因此我没有使用操作栏),因此我使用了FrameLayout,并将ImageView固定在顶部,高度为50,并使用与主视图相同的背景作为ImageView的源....我的问题是我在主视图中使用了Center Crop比例类型,它非常完美,但在标题栏中无用,所以我正在寻找类似于以下内容的东西: enter image description here

它与Center Crop完全相同,但从图像顶部裁剪。 我使用了此链接,但这不是我要找的...

3个回答

4

顶部裁剪 ImageView:

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;


public class TopCropImageView extends ImageView {
    private Matrix mMatrix;
    private boolean mHasFrame;

    @SuppressWarnings("UnusedDeclaration")
    public TopCropImageView(Context context) {
        this(context, null, 0);
    }

    @SuppressWarnings("UnusedDeclaration")
    public TopCropImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressWarnings("UnusedDeclaration")
    public TopCropImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mHasFrame = false;
        mMatrix = new Matrix();
        // we have to use own matrix because:
        // ImageView.setImageMatrix(Matrix matrix) will not call
        // configureBounds(); invalidate(); because we will operate on ImageView object
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b)
    {
        boolean changed = super.setFrame(l, t, r, b);
        if (changed) {
            mHasFrame = true;
            // we do not want to call this method if nothing changed
            setupScaleMatrix(r-l, b-t);
        }
        return changed;
    }

    private void setupScaleMatrix(int width, int height) {
        if (!mHasFrame) {
            // we have to ensure that we already have frame
            // called and have width and height
            return;
        }
        final Drawable drawable = getDrawable();
        if (drawable == null) {
            // we have to check if drawable is null because
            // when not initialized at startup drawable we can
            // rise NullPointerException
            return;
        }
        Matrix matrix = mMatrix;
        final int intrinsicWidth = drawable.getIntrinsicWidth();
        final int intrinsicHeight = drawable.getIntrinsicHeight();

        float factorWidth = width/(float) intrinsicWidth;
        float factorHeight = height/(float) intrinsicHeight;
        float factor = Math.max(factorHeight, factorWidth);

        // there magic happen and can be adjusted to current
        // needs
        matrix.setTranslate(-intrinsicWidth/2.0f, 0);
        matrix.postScale(factor, factor, 0, 0);
        matrix.postTranslate(width/2.0f, 0);
        setImageMatrix(matrix);
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        // We have to recalculate image after chaning image
        setupScaleMatrix(getWidth(), getHeight());
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        // We have to recalculate image after chaning image
        setupScaleMatrix(getWidth(), getHeight());
    }

    @Override
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
        // We have to recalculate image after chaning image
        setupScaleMatrix(getWidth(), getHeight());
    }

    // We do not have to overide setImageBitmap because it calls 
    // setImageDrawable method

}

2
我使用了可以在这里找到的PhotoView库来实现此功能: https://github.com/chrisbanes/PhotoView 你需要做的就是设置好这个库,在你所使用的图像视图上将比例类型设置为CENTER_CROP。然后在库中转到PhotoViewAttacher.java文件,并在"updateBaseMatrix()"方法下修改代码,修改CENTER_CROP为以下内容:
    else if (mScaleType == ScaleType.CENTER_CROP) {
     float scale = Math.max(widthScale, heightScale);
     mBaseMatrix.postScale(scale, scale);
     //Changed dy = 0 for top crop
     mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F,
         0);

基本上,将dy设置为0,这样矩阵就会保持在y轴图像的顶部,而不是将其居中。CENTER_CROP缩放类型的行为与TOP_CROP相似。

非常感谢,这节省了我很多时间! - Özgür

1
你的方法有点巧妙,我建议你看一下使用粘性片段(例如)。
视频、源代码和示例可以在此Google+帖子中找到。

1
这不是我需要的,但非常有帮助...谢谢 :) - user1631100
链接已失效,请更新或直接在此处提供相关信息。 - goemic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接