Android Gallery 3张图无限循环 左右滑动都有效


废话不多说 上关键代码


Main


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.widget.AdapterView;  
  5. import android.widget.AdapterView.OnItemClickListener;  
  6.   
  7. public class Main extends Activity  
  8. {  
  9.     public static int[] images =  
  10.     { R.drawable.image1, R.drawable.image2, R.drawable.image3 };  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState)  
  14.     {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         final ImageAdapter adapter = new ImageAdapter(this, images);  
  18.   
  19.         GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01);  
  20.         galleryFlow.setAdapter(adapter);  
  21.         galleryFlow.setSelection(adapter.getCount() / 2);  
  22.         galleryFlow.setOnItemClickListener(new OnItemClickListener()  
  23.         {  
  24.             @Override  
  25.             public void onItemClick(AdapterView<?> parent, View view, int position, long id)  
  26.             {  
  27.                 int nowposition = (position % images.length);// 算出真正的位置  
  28.                 CustomToast.showToast(Main.this"position:" + nowposition, 2000);  
  29.             }  
  30.         });  
  31.     }  
  32. }  

ImageAdapter

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.test.jooylife.com;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.LinearGradient;  
  8. import android.graphics.Matrix;  
  9. import android.graphics.Paint;  
  10. import android.graphics.PorterDuffXfermode;  
  11. import android.graphics.Bitmap.Config;  
  12. import android.graphics.PorterDuff.Mode;  
  13. import android.graphics.Shader.TileMode;  
  14. import android.view.View;  
  15. import android.view.ViewGroup;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.ImageView;  
  18. import android.widget.ImageView.ScaleType;  
  19.   
  20. public class ImageAdapter extends BaseAdapter  
  21. {  
  22.     private Context mContext;  
  23.     private int[] mImageIds;  
  24.   
  25.     public ImageAdapter(Context c, int[] ImageIds)  
  26.     {  
  27.         mContext = c;  
  28.         mImageIds = ImageIds;  
  29.     }  
  30.   
  31.     public int getCount()  
  32.     {  
  33.         return Integer.MAX_VALUE;  
  34.     }  
  35.   
  36.     public Object getItem(int position)  
  37.     {  
  38.         return position;  
  39.     }  
  40.   
  41.     public long getItemId(int position)  
  42.     {  
  43.         return position;  
  44.     }  
  45.   
  46.     /** 
  47.      * 加载图片 
  48.      *  
  49.      * @param position 
  50.      * @return 
  51.      */  
  52.     private ImageView getImageView(int position)  
  53.     {  
  54.         final int reflectionGap = 4;  
  55.         Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), mImageIds[position % mImageIds.length]);  
  56.         int width = originalImage.getWidth();  
  57.         int height = originalImage.getHeight();  
  58.   
  59.         Matrix matrix = new Matrix();  
  60.         matrix.preScale(1, -1);  
  61.   
  62.         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false);  
  63.   
  64.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);  
  65.   
  66.         Canvas canvas = new Canvas(bitmapWithReflection);  
  67.   
  68.         canvas.drawBitmap(originalImage, 00null);  
  69.   
  70.         Paint deafaultPaint = new Paint();  
  71.         canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);  
  72.   
  73.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  74.   
  75.         Paint paint = new Paint();  
  76.         LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff0x00ffffff, TileMode.CLAMP);  
  77.   
  78.         paint.setShader(shader);  
  79.   
  80.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  81.   
  82.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);  
  83.   
  84.         ImageView imageView = new ImageView(mContext);  
  85.         imageView.setImageBitmap(bitmapWithReflection);  
  86.         imageView.setLayoutParams(new GalleryFlow.LayoutParams(400780));  
  87.         imageView.setScaleType(ScaleType.MATRIX);  
  88.         imageView.setImageResource(mImageIds[position % mImageIds.length]);  
  89.         return imageView;  
  90.     }  
  91.   
  92.     public View getView(int position, View convertView, ViewGroup parent)  
  93.     {  
  94.         return getImageView(position);  
  95.     }  
  96.   
  97.     public float getScale(boolean focused, int offset)  
  98.     {  
  99.         return Math.max(01.0f / (float) Math.pow(2, Math.abs(offset)));  
  100.     }  
  101. }  


GalleryFlow

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.graphics.Camera;  
  3. import android.graphics.Matrix;  
  4. import android.util.AttributeSet;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.animation.Transformation;  
  8. import android.widget.Gallery;  
  9. import android.widget.ImageView;  
  10.   
  11. public class GalleryFlow extends Gallery  
  12. {  
  13.     private Camera mCamera = new Camera();  
  14.     private int mMaxRotationAngle = 150;  
  15.     private int mMaxZoom = -100;  
  16.     private int mCoveflowCenter;  
  17.   
  18.     public GalleryFlow(Context context)  
  19.     {  
  20.         super(context);  
  21.         this.setStaticTransformationsEnabled(true);  
  22.     }  
  23.   
  24.     public GalleryFlow(Context context, AttributeSet attrs)  
  25.     {  
  26.         super(context, attrs);  
  27.         this.setStaticTransformationsEnabled(true);  
  28.     }  
  29.   
  30.     public GalleryFlow(Context context, AttributeSet attrs, int defStyle)  
  31.     {  
  32.         super(context, attrs, defStyle);  
  33.         this.setStaticTransformationsEnabled(true);  
  34.     }  
  35.   
  36.     public int getMaxRotationAngle()  
  37.     {  
  38.         return mMaxRotationAngle;  
  39.     }  
  40.   
  41.     public void setMaxRotationAngle(int maxRotationAngle)  
  42.     {  
  43.         mMaxRotationAngle = maxRotationAngle;  
  44.     }  
  45.   
  46.     public int getMaxZoom()  
  47.     {  
  48.         return mMaxZoom;  
  49.     }  
  50.   
  51.     public void setMaxZoom(int maxZoom)  
  52.     {  
  53.         mMaxZoom = maxZoom;  
  54.     }  
  55.   
  56.     private int getCenterOfCoverflow()  
  57.     {  
  58.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();  
  59.     }  
  60.   
  61.     private static int getCenterOfView(View view)  
  62.     {  
  63.         return view.getLeft() + view.getWidth() / 2;  
  64.     }  
  65.   
  66.     protected boolean getChildStaticTransformation(View child, Transformation t)  
  67.     {  
  68.         final int childCenter = getCenterOfView(child);  
  69.         final int childWidth = child.getWidth();  
  70.         int rotationAngle = 0;  
  71.   
  72.         t.clear();  
  73.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  74.   
  75.         if (childCenter == mCoveflowCenter)  
  76.         {  
  77.             transformImageBitmap((ImageView) child, t, 0);  
  78.         } else  
  79.         {  
  80.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  81.             if (Math.abs(rotationAngle) > mMaxRotationAngle)  
  82.             {  
  83.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;  
  84.             }  
  85.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  86.         }  
  87.   
  88.         return true;  
  89.     }  
  90.   
  91.     protected void onSizeChanged(int w, int h, int oldw, int oldh)  
  92.     {  
  93.         mCoveflowCenter = getCenterOfCoverflow();  
  94.         super.onSizeChanged(w, h, oldw, oldh);  
  95.     }  
  96.   
  97.     private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle)  
  98.     {  
  99.         mCamera.save();  
  100.         final Matrix imageMatrix = t.getMatrix();  
  101.         final int imageHeight = child.getLayoutParams().height;  
  102.         final int imageWidth = child.getLayoutParams().width;  
  103.         final int rotation = Math.abs(rotationAngle);  
  104.   
  105.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。  
  106.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。  
  107.         mCamera.translate(0.0f, 0.0f, 100.0f);  
  108.   
  109.         if (rotation < mMaxRotationAngle)  
  110.         {  
  111.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  112.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  113.         }  
  114.   
  115.         mCamera.getMatrix(imageMatrix);  
  116.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  117.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  118.         mCamera.restore();  
  119.     }  
  120.   
  121.     @Override  
  122.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)  
  123.     {  
  124.         // return super.onFling(e1, e2, velocityX/1.5f, velocityY);  
  125.         return false;  
  126.     }  
  127. }  

本例下载 附送了一个可订制的时间的Android Toast


附,发现4.1版本以后,由于硬件加速的问题,出现图片错位。

GalleryFlow中增加方法

private int offsetChildrenLeftAndRight()
    {
        int offset = 0;
        for (int i = getChildCount() - 1; i >= 0; i--)
        {

            getChildAt(i).offsetLeftAndRight(offset);

            if (android.os.Build.VERSION.SDK_INT >= 16)
                getChildAt(i).invalidate();
        }
        return offset;
    }

修改方法

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. protected boolean getChildStaticTransformation(View child, Transformation t)  
  2.     {  
  3.         final int childCenter = getCenterOfView(child) + offsetChildrenLeftAndRight();  
  4.         final int childWidth = child.getWidth();  
  5.         int rotationAngle = 0;  
  6.   
  7.         t.clear();  
  8.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  9.   
  10.         if (childCenter == mCoveflowCenter)  
  11.         {  
  12.             transformImageBitmap((ImageView) child, t, 0);  
  13.         } else  
  14.         {  
  15.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  16.             if (Math.abs(rotationAngle) > mMaxRotationAngle)  
  17.             {  
  18.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;  
  19.             }  
  20.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  21.         }  
  22.   
  23.         return true;  
  24.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值