缩放图库中的选定项目

时间:2011-04-27 16:11:38

标签: android gallery

您知道如何缩放图库中的所选项目吗?我知道显然从0.9 SDK中删除了getScale()和getAlpha()。那我怎么能达到同样的效果呢?

由于

2 个答案:

答案 0 :(得分:3)

回答可能为时已晚,但在搜索其他内容时我发现了这个问题。

我是通过自定义图库并覆盖getChildStaticTransformation()并添加其他内容来实现的。

这是一个例子

private int centerOfGallery;

public CustomGallery(Context context) {
    super(context);
    init();
}

public CustomGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    setStaticTransformationsEnabled(true);
}

private int getCenterWidthOfView(View child) {
    return child.getLeft() + child.getWidth() / 2;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    centerOfGallery = (w - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
}

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
    mCamera.save();
    final Matrix matrix = t.getMatrix();
    final int centerWidthOfChild = getCenterWidthOfView(child);
    final int delta = centerOfGallery - centerWidthOfChild;

    final float scale = (float)(maxScale - Math.abs(delta) * 0.5f / centerOfGallery);
    mCamera.getMatrix(matrix);
    matrix.preScale(scale, scale);
    matrix.preTranslate(-1, -1);
    matrix.postTranslate(1, 1);
    mCamera.restore();

    if (version >= 15) { // For Jelly Bean hack
        child.invalidate();
    }

    return true;
}

,其中 maxScale是所选项目的最大比例(例如1.5f)

之后,在缩放它们时,请注意图库中项目之间的间距。如有必要,您可以使用setSpacing()。

希望这有帮助

的Seb

答案 1 :(得分:0)

相关问题