您当前的位置: 首页 >  android

xiangzhihong8

暂无认证

  • 1浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

android 实现倒影

xiangzhihong8 发布时间:2016-04-17 23:24:10 ,浏览量:1

首先,文章中出现的Gallery 已经不再适用,替代方法请看我的另一篇文章http://blog.csdn.net/xiangzhihong8/article/details/51120460

不过对于文章中说的倒影的原理是可以借鉴的。

1.图片的显示以及切换主要是自定义了一个Gallery

下面是代码myGallery.java:

[java]  view plain  copy
 print ?
  1. import android.content.Context;  
  2. import android.graphics.Camera;  
  3. import android.graphics.Matrix;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.view.animation.Transformation;  
  7. import android.widget.Gallery;  
  8. import android.widget.ImageView;  
  9.   
  10. @SuppressWarnings("deprecation")  
  11. public class myGallery extends Gallery {  
  12.   
  13.     private Camera mCamera = new Camera();  
  14.     private int mMaxRotationAngle = 60;     //图片偏转角度 60  
  15.     private int mMaxZoom = -120;  
  16.     private int mCoveflowCenter;  
  17.   
  18.     public myGallery(Context context) {  
  19.         super(context);  
  20.         this.setStaticTransformationsEnabled(true);  
  21.     }  
  22.   
  23.     public myGallery(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.         this.setStaticTransformationsEnabled(true);  
  26.     }  
  27.   
  28.     public myGallery(Context context, AttributeSet attrs, int defStyle) {  
  29.         super(context, attrs, defStyle);  
  30.         this.setStaticTransformationsEnabled(true);  
  31.     }  
  32.   
  33.     public int getMaxRotationAngle() {  
  34.         return mMaxRotationAngle;  
  35.     }  
  36.   
  37.     public void setMaxRotationAngle(int maxRotationAngle) {  
  38.         mMaxRotationAngle = maxRotationAngle;  
  39.     }  
  40.   
  41.     public int getMaxZoom() {  
  42.         return mMaxZoom;  
  43.     }  
  44.   
  45.     public void setMaxZoom(int maxZoom) {  
  46.         mMaxZoom = maxZoom;  
  47.     }  
  48.   
  49.     /** 获得Gallery中心到边界的距离*/  
  50.     private int getCenterOfCoverflow() {  
  51.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();  
  52.     }  
  53.   
  54.     /** 获得View中心位置到边界的距离 */  
  55.     private static int getCenterOfView(View view) {  
  56.         return view.getLeft() + view.getWidth() / 2;  
  57.     }  
  58.       
  59.     @Override  
  60.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  61.         mCoveflowCenter = getCenterOfCoverflow();  
  62.         super.onSizeChanged(w, h, oldw, oldh);  
  63.     }  
  64.   
  65.     @Override  
  66.     protected boolean getChildStaticTransformation(View child, Transformation trans) {  
  67.         final int childCenter = getCenterOfView(child);  
  68.         final int childWidth = child.getWidth();  
  69.         int rotationAngle = 0;  
  70.   
  71.         trans.clear();  
  72.         trans.setTransformationType(Transformation.TYPE_BOTH);      // alpha和 matrix都变换  
  73.   
  74.         if (childCenter == mCoveflowCenter) {   //正中间的childView   
  75.             transformImageBitmap((ImageView) child, trans, 0);    
  76.         } else {                                //两侧的childView     
  77.             rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle );  
  78.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
  79.                 rotationAngle = (rotationAngle  parent) {        
  80.             }        
  81.         });        
  82.                  
  83.         gallery.setOnItemClickListener(new OnItemClickListener() {          // 设置点击事件监听        
  84.             @Override   
  85.             public void onItemClick(AdapterView parent, View view, int position, long id) {        
  86.                 Toast.makeText(Main.this, "img " + (position+1) + " selected", Toast.LENGTH_SHORT).show();        
  87.             }        
  88.         });        
  89.     }  
  90. }  

4.最后是页面布局main.xml:

[html]  view plain  copy
 print ?
  1.   
  2.   
  3.       
  4.   
  5.       
  6.   
  7.   

5.大功告成啦!看看效果:

 欢迎下载源码!

关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0513s