gallery 从最左边开始显示并且默认选中第一个
2013-08-12 16:49
302 查看
import android.content.Context;
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();
this.setStaticTransformationsEnabled(true);
}
/*
* 构造方法
*/
public MyGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mCamera = new Camera();
this.setStaticTransformationsEnabled(true);
}
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;
}
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) {
return super.onFling(e1, e2, 0, velocityY);//方法一:只去除翻页惯性
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return super.onScroll(e1, e2, distanceX, distanceY);
// TODO Auto-generated method stub
}
}
在程序中调用MyGallery就可以实现,调用方面跟Gallery一样
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();
this.setStaticTransformationsEnabled(true);
}
/*
* 构造方法
*/
public MyGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mCamera = new Camera();
this.setStaticTransformationsEnabled(true);
}
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;
}
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) {
return super.onFling(e1, e2, 0, velocityY);//方法一:只去除翻页惯性
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return super.onScroll(e1, e2, distanceX, distanceY);
// TODO Auto-generated method stub
}
}
在程序中调用MyGallery就可以实现,调用方面跟Gallery一样
相关文章推荐
- gallery 从最左边开始显示并且默认选中第一个
- android 使用GridView实现Gallery的效果,并且从最左边开始
- [Android疑难杂症]Gallery默认和横竖屏切换选中状态问题(2.3、2.3系统存在)
- UITableView默认选中第一个cell
- 替代union all 复杂的sql 并且可以接着双重排序,最主要的是他避免了一条语句两(多)条order by 插件默认执行第一个结束
- jQuery设置下拉框select 默认选中第一个option
- 如何设置extextenders tabcontainer 加载时默认显示第一个TabPnel
- easyui combobox默认选中第一个
- UITableView默认选中第一个cell
- Gallery从最左边开始
- 表视图默认选中第一个cell以及button外沿特性设置
- 今天来马克一下把数据库中的数据动态显示到前台页面下拉框中/默认选中
- easyui tree 默认选中第一个节点
- TableView使默认下划线从最左边开始
- UltraWebGrid默认选中第一行及前台body里触发onload事件,实现多个iframe默认关联第一个。
- Javascript实现页面左边的菜单选中项高亮显示
- ListView或GridView嵌入ScrollView中时,只显示一个Item并且默认将此Item置顶的问题解决办法
- iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug
- 关于C# Winform dataGridView显示时没有默认选中以及dataGridView添加DataTable中的数据
- 在多个单选框中用jquery将第一个默认选中