Android图片查看顶部裁剪

时间:2014-03-10 00:12:29

标签: java android eclipse

我正在开发一个Android应用程序,我需要在屏幕顶部使用一个类似于操作栏的标题.....我希望它是透明的,但是当你开始滚动查看所有内容时后面的视图因此我决定使用图像视图(它必须在任何片段中不同,这就是为什么我没有使用操作栏)所以我使用框架布局并修复顶部的图像视图与高度50和我使用相同的主视图背景作为图像视图的来源....我的问题是我在主视图中使用中心裁剪的比例类型,它是完美的但它在标题中没用,所以我是寻找这样的事情: enter image description here

与中心裁剪完全相同,但是从图像顶部裁剪。 我使用了this link,但这不是我想要的......

3 个答案:

答案 0 :(得分:3)

Top Crop 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

}

答案 1 :(得分:1)

你的方法有点hacky,我建议你看一下使用Sticky Fragment(例如)。

视频,来源和示例可在this Google+ Post

中找到

答案 2 :(得分:1)

我设法使用可在此处找到的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