1. 程式人生 > >向左對齊的Gallery

向左對齊的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);
        }
}