向左对齐的Gallery

转载地址:http://www.eoeandroid.com/thread-158088-1-1.html


系统自带的Gallery选中的item总是在组件的中间。但是有些时候我们需要把选中的元素放在左边或者是Gallery一出来就要放在左边。

修改Gallery靠左对齐的思路:
1 、Gellary总是对center进行锁定的,所以可以考虑修改它的center的位置,把center改成在left的位置就可以了。
Gallery 中有个方法:
/**
*@return The center of this Gallery.
*/
  private int getCenterOfGallery() {
        return (getWidth() - mPaddingLeft -mPaddingRight) / 2 + mPaddingLeft;
}
只可惜这个方法是private,所以没有办法重写。那就把源码拷贝出来进行修改吧,但是当你拷贝处理的时候会发现很多的变量是没有办法用的,这是因为:
a.      @ViewDebug.ExportedProperty  
This annotation can be used to markfields and methods to be dumped by the view server. Only non-void methods withno arguments can be annotated by this annotation.
这个很好理解,不作翻译。
b. {@hide}
      Views which have been hidden or removedwhich need to be animated on their way out. This field should be made private,so it is hidden from the SDK.
这也很好理解,自己翻译。
总结:Gallery的源码太过复杂所以这个方法只好放弃。
2 、重写Gallery。这个问题的关键是如何重写,重写什么。

Gallery其实只是显示了数据,那么我们不必把数据移动位置而把摄像机移动位置不就可以了吗?正是这样完成了Gallery的重写。代码如下:


package eoe.android.CustomGallery;

import android.R.attr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

public class GalleryFlow extends Gallery {

        private Camera mCamera;
        private int mWidth;
        private int mPaddingLeft;
        private boolean flag;
        private static int firstChildWidth;
        private static int firstChildPaddingLeft;
        private int offsetX;

        public GalleryFlow(Context context) {
                super(context);
                mCamera = new Camera();
                this.setStaticTransformationsEnabled(true);
        }

        public GalleryFlow(Context context, AttributeSet attrs) {
                super(context, attrs);
                mCamera = new Camera();
                setAttributesValue(context, attrs);
                this.setStaticTransformationsEnabled(true);
        }

        public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                mCamera = new Camera();
                setAttributesValue(context, attrs);
                this.setStaticTransformationsEnabled(true);
        }

        private void setAttributesValue(Context context, AttributeSet attrs) {
                TypedArray typedArray = context.obtainStyledAttributes(attrs,
                                new int[] { attr.paddingLeft });
                mPaddingLeft = typedArray.getDimensionPixelSize(0, 0);
                typedArray.recycle();
        }

        protected boolean getChildStaticTransformation(View child, Transformation t) {
                t.clear();
                t.setTransformationType(Transformation.TYPE_MATRIX);
                mCamera.save();
                final Matrix imageMatrix = t.getMatrix();
                if (flag) {
                        firstChildWidth = getChildAt(0).getWidth();
                        firstChildPaddingLeft = getChildAt(0).getPaddingLeft();
                        flag = false;
                }
                offsetX = firstChildWidth / 2 + firstChildPaddingLeft + mPaddingLeft
                                - mWidth / 2;
                mCamera.translate(offsetX, 0f, 0f);
                mCamera.getMatrix(imageMatrix);
                mCamera.restore();
                return true;
        }

        
        @Override
        public boolean onTouchEvent(MotionEvent event) {
                event.offsetLocation(-offsetX, 0);
                return super.onTouchEvent(event);
        }

        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                if (!flag) {
                        mWidth = w * 2;
                        getLayoutParams().width = mWidth;
                        flag = true;
                }
                super.onSizeChanged(w, h, oldw, oldh);
        }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值