1

Do you know how can I scale the selected item in a Gallery? I know that apparently getScale() and getAlpha() were removed from 0.9 SDK. So how could I accomplish the same effect?

Thanks

lblasa
  • 6,284
  • 4
  • 27
  • 29

2 Answers2

3

Maybe it's too late to answer, but I found this question when searching something else.

I did it by having a custom gallery and overriding getChildStaticTransformation() and adding some other things.

Here is an example

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;
}

where maxScale is the maximum scale you want for selected item (e.g. 1.5f)

After that, be careful about the spacing between items in the gallery when scaling them. You can use setSpacing() if necessary.

Hope this helps

Seb

Seb
  • 306
  • 2
  • 4
0

try this Image in Canvas with touch events

Community
  • 1
  • 1
  • I don't think it applies since the scaling is being done based on the touch events. In my casa, I would like to apply the scaling when the item is selected when flinging, so basically the scaling should be done automatically when the selection is made. Any ideas? – lblasa Apr 27 '11 at 16:28
  • i dnt knw much, u can refer this & implement in fling, sry –  Apr 27 '11 at 16:33