图片左右滚动控件(带倒影)——重写Gallery


今天在网上找了些资料,做了一个图片左右滚动的Demo,类似幻灯片播放,同时,图片带倒影效果,运行效果如下图:

                                                         

实现方式是重写Gallery,使用自定义的Gallery来实现这一效果,工程一共三个文件,一个Activity,一个自定义的Gallery,还有就是一个适配器ImageAdapter,直接上代码:

ScrollGallery.java

[java]  view plain copy
  1. public class ScrollGallery extends Gallery {  
  2.     private Camera mCamera = new Camera();  
  3.     //左右图片倾斜的角度  
  4.     private int mMaxRotationAngle = 60;  
  5.     //背景区域大小  
  6.     private int mMaxZoom = -380;  
  7.     private int mCoveflowCenter;  
  8.   
  9.     public ScrollGallery(Context context) {  
  10.         super(context);  
  11.         this.setStaticTransformationsEnabled(true);  
  12.     }  
  13.   
  14.     public ScrollGallery(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         this.setStaticTransformationsEnabled(true);  
  17.     }  
  18.   
  19.     public ScrollGallery(Context context, AttributeSet attrs, int defStyle) {  
  20.         super(context, attrs, defStyle);  
  21.         this.setStaticTransformationsEnabled(true);  
  22.     }  
  23.   
  24.     public int getMaxRotationAngle() {  
  25.         return mMaxRotationAngle;  
  26.     }  
  27.   
  28.     public void setMaxRotationAngle(int maxRotationAngle) {  
  29.         mMaxRotationAngle = maxRotationAngle;  
  30.     }  
  31.   
  32.     public int getMaxZoom() {  
  33.         return mMaxZoom;  
  34.     }  
  35.   
  36.     public void setMaxZoom(int maxZoom) {  
  37.         mMaxZoom = maxZoom;  
  38.     }  
  39.   
  40.     private int getCenterOfCoverflow() {  
  41.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  
  42.                 + getPaddingLeft();  
  43.     }  
  44.   
  45.     private static int getCenterOfView(View view) {  
  46.         return view.getLeft() + view.getWidth() / 2;  
  47.     }  
  48.   
  49.     protected boolean getChildStaticTransformation(View child, Transformation t) {  
  50.   
  51.         final int childCenter = getCenterOfView(child);  
  52.         final int childWidth = child.getWidth();  
  53.         int rotationAngle = 0;  
  54.         t.clear();  
  55.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  56.         if (childCenter == mCoveflowCenter) {  
  57.             transformImageBitmap((ImageView) child, t, 0);  
  58.         } else {  
  59.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  60.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
  61.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle  
  62.                         : mMaxRotationAngle;  
  63.             }  
  64.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  65.         }  
  66.         return true;  
  67.     }  
  68.   
  69.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  70.         mCoveflowCenter = getCenterOfCoverflow();  
  71.         super.onSizeChanged(w, h, oldw, oldh);  
  72.     }  
  73.   
  74.     private void transformImageBitmap(ImageView child, Transformation t,  
  75.             int rotationAngle) {  
  76.         mCamera.save();  
  77.         final Matrix imageMatrix = t.getMatrix();  
  78.         final int imageHeight = child.getLayoutParams().height;  
  79.         final int imageWidth = child.getLayoutParams().width;  
  80.         final int rotation = Math.abs(rotationAngle);  
  81.         // 在Z轴上正向移动  
  82.         // 在Y轴上移动,上下移动;X轴上左右移动。  
  83.         mCamera.translate(0.0f, 0.0f, 100.0f);  
  84.         if (rotation < mMaxRotationAngle) {  
  85.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  86.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  87.         }  
  88.         // 在Y轴上旋转,竖向向里翻转。  
  89.         // 在X轴上旋转,则横向向里翻转。  
  90.         mCamera.rotateY(rotationAngle);  
  91.         mCamera.getMatrix(imageMatrix);  
  92.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  93.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  94.         mCamera.restore();  
  95.     }  
  96. }  

ImageAdapter.java

[java]  view plain copy
  1. public class ImageAdapter extends BaseAdapter {  
  2.     //倒影的缩放比例  
  3.     private static final int REFLECTION = 5;  
  4.       
  5.     int mGalleryItemBackground;  
  6.     private Context mContext;  
  7.     private int[] mImageIds;  
  8.     private ImageView[] mImages;  
  9.   
  10.     public ImageAdapter(Context c, int[] ImageIds) {  
  11.         mContext = c;  
  12.         mImageIds = ImageIds;  
  13.         mImages = new ImageView[mImageIds.length];  
  14.     }  
  15.   
  16.     public boolean createReflectedImages() {  
  17.         final int reflectionGap = 5;  
  18.         int index = 0;  
  19.         for (int imageId : mImageIds) {  
  20.             Bitmap originalImage = BitmapFactory.decodeResource(  
  21.                     mContext.getResources(), imageId);  
  22.             int width = originalImage.getWidth();  
  23.             int height = originalImage.getHeight();  
  24.             Matrix matrix = new Matrix();  
  25.             matrix.preScale(1, -1);  
  26.             Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,  
  27.                     height / 2, width, height / 2, matrix, false);  
  28.             Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
  29.                     (height + height / REFLECTION), Config.ARGB_8888);  
  30.             Canvas canvas = new Canvas(bitmapWithReflection);  
  31.             canvas.drawBitmap(originalImage, 00null);  
  32.             Paint deafaultPaint = new Paint();  
  33.             canvas.drawRect(0, height, width, height + reflectionGap,  
  34.                     deafaultPaint);  
  35.             canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  36.             Paint paint = new Paint();  
  37.             LinearGradient shader = new LinearGradient(0,  
  38.                     originalImage.getHeight(), 0,  
  39.                     bitmapWithReflection.getHeight() + reflectionGap,  
  40.                     0x70ffffff0x00ffffff, TileMode.CLAMP);  
  41.             paint.setShader(shader);  
  42.             paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  43.             canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
  44.                     + reflectionGap, paint);  
  45.             ImageView imageView = new ImageView(mContext);  
  46.             imageView.setImageBitmap(bitmapWithReflection);  
  47.             imageView.setLayoutParams(new ScrollGallery.LayoutParams(180240));  
  48.             mImages[index++] = imageView;  
  49.         }  
  50.         return true;  
  51.     }  
  52.   
  53.     public int getCount() {  
  54.         return mImageIds.length;  
  55.     }  
  56.   
  57.     public Object getItem(int position) {  
  58.         return position;  
  59.     }  
  60.   
  61.     public long getItemId(int position) {  
  62.         return position;  
  63.     }  
  64.   
  65.     public View getView(int position, View convertView, ViewGroup parent) {  
  66.         return mImages[position];  
  67.     }  
  68.   
  69.     public float getScale(boolean focused, int offset) {  
  70.         return Math.max(01.0f / (float) Math.pow(2, Math.abs(offset)));  
  71.     }  
  72. }  

ScrollImageActivity.java

[java]  view plain copy
  1. public class ScrollImageActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.       
  4.     private ScrollGallery galleryFlow = null;  
  5.       
  6.     int images[] = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.f};  
  7.       
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  12.         setContentView(R.layout.main);  
  13.         galleryFlow = (ScrollGallery) this.findViewById(R.id.gf_images);  
  14.         ImageAdapter adapter = new ImageAdapter(this, images);  
  15.         adapter.createReflectedImages();  
  16.         galleryFlow.setAdapter(adapter);  
  17.         galleryFlow.setSelection(1);  
  18.     }  
  19. }  

main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.ryan.scrollimage.ScrollGallery  
  8.         android:id="@+id/gf_images"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="0dip"  
  11.         android:layout_weight="1" />  
  12.   
  13. </LinearLayout>  
来源: http://blog.csdn.net/ryantang03/article/details/8053643
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值