Gallery中内容居左的方法 ——重写Gallery

开发时遇到一个界面美观问题:
Gallery 中的内容默认居中,想让其居左,找了很多资料都不太理想,最后终于找到一个重写Gallery 的办法,试验后发现是好用的~。
 
以下即为重写的 MyGallery 代码。
 
再在布局中把
 <Gallery
   ...
 />
 
换成使用自定义的 MyGallery 即可
 <包名.MyGallery
   ...
 />
 
Gallery 元素左对齐的方法 重写Gallery
 

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

 

方法是重写Gallery,但让Gallery居中的那个方法是私有的,无法重写,用下面重写的Gallery就可以。

[android] <wbr>Gallery中内容居左的方法 <wbr>鈥斺斨匦碐allery
 

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 MyGallery extends Gallery {

        private Camera mCamera;

        private int mWidth;

        private int mPaddingLeft;

        private boolean flag;

        private static int firstChildWidth;

        private static int firstChildPaddingLeft;

 

       

        public MyGallery(Context context) {

                super(context);

                mCamera = new Camera();

                this.setStaticTransformationsEnabled(true);

        }

 

       

        public MyGallery(Context context, AttributeSet attrs) {

                super(context, attrs);

                mCamera = new Camera();

                //setAttributesValue(context, attrs);

                this.setStaticTransformationsEnabled(true);

        }

 

       

        public MyGallery(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();

                final int imageHeight = child.getLayoutParams().height;

                final int imageWidth = child.getLayoutParams().width;

                if (flag) {

                        firstChildWidth = getChildAt(0).getWidth();

                        firstChildPaddingLeft = getChildAt(0).getPaddingLeft();

                        flag = false;

                }

                mCamera.translate(firstChildWidth / 2 + firstChildPaddingLeft + mPaddingLeft - mWidth / 2, 0f, 0f);

                mCamera.getMatrix(imageMatrix);

                mCamera.restore();

                return true;

        }

 

        protected void onSizeChanged(int w, int h, int oldw, int oldh) {

                if (!flag) {

                        mWidth = w ;

                        getLayoutParams().width = mWidth;

                        flag = true;

                }

                super.onSizeChanged(w, h, oldw, oldh);

        }

 

        @Override

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                // TODO Auto-generated method stub

                // return super.onFling(e1, e2, velocityX, velocityY);

                return false;

        }

 

        @Override

        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

                // TODO Auto-generated method stub

                return super.onScroll(e1, e2, distanceX, distanceY);

        }

 

}

转载于:https://www.cnblogs.com/hedandan/archive/2012/08/28/2660480.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值