向左对齐的Gallery
系统自带的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);
}
}
向左对齐的Gallery的更多相关文章
- css实现一行文字居中,多行文字左对齐
问题及场景: 当内容能一行显示在盒子内时,文字居中对齐. 当内容过多换行后显示在盒子内时,文字左对齐. 其实这种视觉上的需求还是蛮常见的.比如用于弹出提示框,当提示内容比较少时,内容居中显示在弹出框, ...
- iOS小知识:使UIButton中的图片和文字实现左对齐
UIButton setImage 和 setTitle之后,默认的 image和title 对齐方式是居中, 由于 title 长度不固定,所以如果要几个这样有image有title的按钮纵向排列对 ...
- [No000089]String的(补空位)左对齐,(补空位)右对齐
using System; namespace Chinese中文排序Sort { internal class Program { /// <summary> /// 取子字符串 /// ...
- C++ 画星号图形——空心三角形(星号左对齐)(核心代码介绍)
//输出一个由星号组成的三角形(星号左对齐) int a;//控制组成三角形的星号的行数 cout<<"请输入要组成三解形的星号的行数n(n>=2):\n"; c ...
- iOS上让按钮文本左对齐问题
一,问题分析 1.在做历史记录视图的时候,由于让键盘退出后才能触发表格的 didselect 那个代理方法,也就是得点两下才触发,而表格中的按钮点一下就可以立即响应. 2.于是我就有了用按钮事件代替 ...
- 6、UITableView表的分割线左对齐
//分割线左对齐 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtI ...
- word2010 数学公式/联立方程/大括号内方程组如何左对齐?
如何在word中输入的联立方程使其条件左对齐? 如输入: 实现如下对齐: 就是在每个逗号 .前输入一个 & 号就可以了, 注意这个逗号一定要是 位于这个方框里头,然后在其前面输入 & ...
- ios8 ios7 tableview cell 分割线左对齐
ios8中左对齐代码 //加入如下代码 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel ...
- Android -如何在底部左对齐,中对齐,右对齐三个按钮图片 巧用Relative Layout
Relative Layout 不仅可以指定同级的元素之间的位置关系(e.g. layout_toLeftOf) 还可以指定子元素与父元素之间的位置关系(e.g. layout_alignParent ...
随机推荐
- ACM - 动态规划专题 题目整理
CodeForces 429B Working out 预处理出从四个顶点到某个位置的最大权值,再枚举相遇点,相遇的时候只有两种情况,取最优解即可. #include<iostream> ...
- 上位机控制led
使用库函数,调试的结果在标红程序上,int main(void){ u8 a; u8 t; u8 len; u16 ti ...
- [转]dev C++编写windows程序遇到问题
1.工具-编译选项-编译器-在连接器命令行加入以下命令: -mwindows 2.出现错误:undefined reference to `PlaySoundA@12' 解决办法:工具-编译选项-编译 ...
- hdu 2099
PS:因为还是不爽...继续水题...感觉这道题就是考输出.. 代码: #include "stdio.h" void cal(int a,int b); int main(){ ...
- Fractal Tree扩展
之前的博客实现了最基础的分形树,在这个基础上略微调整一些参数可以得到很多有趣的由分形树发展出的图案. private void drawShape(Graphics g, double x1, dou ...
- PHP面向对象的程序设计一些简单的概念
一.面向对象的概述 数组和对象,都属于PHP中的复合类型(一个变量可以存储多个单元) 对象比数组更强大,不仅可以存储多个数据,还可以将函数存在对象中 对象的三大特性:封装,继承,多态 面向对象编 ...
- php 数组 添加元素、删除元素
拆分数组 PHP数组添加一个元素的方式: push(), arr[], Php代码 $arr = array(); array_push($arr, el1, el2 ... eln); 但其实有一种 ...
- php-抽象
//继承//子类可以继承父类的一切//特点:单继承//函数的重写 //多态//当父类引用指向子类实例,由于子类对父类的方法进行了重写,父类引用在调用该方法的时候表现出的不同//如果一个方法需要一个父类 ...
- 帝国cms缩略图:网站不同地方生成不同的缩略图
本文转自:http://blog.sina.com.cn/s/blog_4d49ba58010115sd.html 方便网站多地方调用不同尺寸的缩略图. 根据图片源,在网站任意位置生成不同大小的缩略图 ...
- vcf_filter.py
pyvcf 中带的一个工具 比其他工具用着好些 其他filter我很信不过~~ 自己写的功能又很有限 所以转投vcf_filter.py啦 Filtering a VCF file based on ...