首先,文章中出现的Gallery 已经不再适用,替代方法请看我的另一篇文章http://blog.csdn.net/xiangzhihong8/article/details/51120460
不过对于文章中说的倒影的原理是可以借鉴的。
1.图片的显示以及切换主要是自定义了一个Gallery
下面是代码myGallery.java:
- import android.content.Context;
- import android.graphics.Camera;
- import android.graphics.Matrix;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.animation.Transformation;
- import android.widget.Gallery;
- import android.widget.ImageView;
- @SuppressWarnings("deprecation")
- public class myGallery extends Gallery {
- private Camera mCamera = new Camera();
- private int mMaxRotationAngle = 60; //图片偏转角度 60
- private int mMaxZoom = -120;
- private int mCoveflowCenter;
- public myGallery(Context context) {
- super(context);
- this.setStaticTransformationsEnabled(true);
- }
- public myGallery(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.setStaticTransformationsEnabled(true);
- }
- public myGallery(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- this.setStaticTransformationsEnabled(true);
- }
- public int getMaxRotationAngle() {
- return mMaxRotationAngle;
- }
- public void setMaxRotationAngle(int maxRotationAngle) {
- mMaxRotationAngle = maxRotationAngle;
- }
- public int getMaxZoom() {
- return mMaxZoom;
- }
- public void setMaxZoom(int maxZoom) {
- mMaxZoom = maxZoom;
- }
- /** 获得Gallery中心到边界的距离*/
- private int getCenterOfCoverflow() {
- return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
- }
- /** 获得View中心位置到边界的距离 */
- private static int getCenterOfView(View view) {
- return view.getLeft() + view.getWidth() / 2;
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- mCoveflowCenter = getCenterOfCoverflow();
- super.onSizeChanged(w, h, oldw, oldh);
- }
- @Override
- protected boolean getChildStaticTransformation(View child, Transformation trans) {
- final int childCenter = getCenterOfView(child);
- final int childWidth = child.getWidth();
- int rotationAngle = 0;
- trans.clear();
- trans.setTransformationType(Transformation.TYPE_BOTH); // alpha和 matrix都变换
- if (childCenter == mCoveflowCenter) { //正中间的childView
- transformImageBitmap((ImageView) child, trans, 0);
- } else { //两侧的childView
- rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle );
- if (Math.abs(rotationAngle) > mMaxRotationAngle) {
- rotationAngle = (rotationAngle parent) {
- }
- });
- gallery.setOnItemClickListener(new OnItemClickListener() { // 设置点击事件监听
- @Override
- public void onItemClick(AdapterView parent, View view, int position, long id) {
- Toast.makeText(Main.this, "img " + (position+1) + " selected", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
4.最后是页面布局main.xml:
5.大功告成啦!看看效果:
欢迎下载源码!