android 3D gallery 并 判断当前选中项

Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.graphics.Camera;  
  3. import android.graphics.Matrix;  
  4. import android.os.AsyncTask;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.animation.Transformation;  
  9. import android.widget.AdapterView;  
  10. import android.widget.Gallery;  
  11. import android.widget.ImageView;  
  12.   
  13. import com.hk.shop.comm.Comm;  
  14.   
  15. /** 
  16.  * 用在首页产品展示模块的gallery<br/> 
  17.  * 1、这是一个3d的gallery<br/> 
  18.  * 2、能自动判断当前选中的图片,并实现回调 
  19.  *  
  20.  * @author yizhe 
  21.  * @date 2012-6-19 
  22.  */  
  23. public class HKGallery4Module extends Gallery {  
  24.     Context context;  
  25.     // 以下属性用于处理3d  
  26.     private Camera camera = new Camera();// 相机类  
  27.     private int maxRotationAngle = 60;// 最大转动角度  
  28.     private int maxZoom = 60;// z方向的移动至,相当于缩放  
  29.     private int coveflowCenter;// 半径值  
  30.     private int height;  
  31.   
  32.     // 以下属性用于处理获取当前选中的项  
  33.     private int currentPosition;  
  34.     private int lastPosition;  
  35.   
  36.     public int getLastPosition() {  
  37.         return lastPosition;  
  38.     }  
  39.   
  40.     public void setLastPosition(int lastPosition) {  
  41.         this.lastPosition = lastPosition;  
  42.     }  
  43.   
  44.     boolean isItemSelectedChange;// 选中的项是否发生了变化  
  45.   
  46.     public HKGallery4Module(Context context, AttributeSet attrs, int defStyle) {  
  47.         super(context, attrs, defStyle);  
  48.         this.context = context;  
  49.         this.setStaticTransformationsEnabled(true);  
  50.         ini();  
  51.     }  
  52.   
  53.     public HKGallery4Module(Context context, AttributeSet attrs) {  
  54.         super(context, attrs);  
  55.         this.context = context;  
  56.         this.setStaticTransformationsEnabled(true);  
  57.         ini();  
  58.     }  
  59.   
  60.     public HKGallery4Module(Context context) {  
  61.         super(context);  
  62.         this.context = context;  
  63.         this.setStaticTransformationsEnabled(true);  
  64.         ini();  
  65.     }  
  66.   
  67.     void ini() {  
  68.         setOnItemSelectedListener(new OnItemSelectedListener() {  
  69.   
  70.             @Override  
  71.             public void onItemSelected(AdapterView<?> parent, View view,  
  72.                     int position, long id) {  
  73.                 setCurrentPosition(position);  
  74.                 isItemSelectedChange = true;  
  75.             }  
  76.   
  77.             @Override  
  78.             public void onNothingSelected(AdapterView<?> parent) {  
  79.   
  80.             }  
  81.         });  
  82.     }  
  83.   
  84.     // ------------------------以下用户处理获取当前选中项------------------------//  
  85.     @Override  
  86.     public boolean onTouchEvent(MotionEvent event) {  
  87.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  88.             JudgeTask task = new JudgeTask();  
  89.             task.execute(); // 手离开开始执行,判断gallery是否停止  
  90.         }  
  91.         return super.onTouchEvent(event);  
  92.     }  
  93.   
  94.     // ------------------判断停止----------------//  
  95.     class JudgeTask extends AsyncTask<Object, Object, Object> {  
  96.         int i = -1;// 测试  
  97.   
  98.         @Override  
  99.         protected Object doInBackground(Object... params) {  
  100.   
  101.             // 每500毫秒执行一次判断,实验证明可以精确判断  
  102.             try {  
  103.                 do {  
  104.                     i++;  
  105.                     lastPosition = getCurrentPosition();  
  106.                     Thread.sleep(500);  
  107.                 } while (lastPosition != getCurrentPosition());  
  108.             } catch (InterruptedException e) {  
  109.             }  
  110.   
  111.             return null;  
  112.         }  
  113.   
  114.         @Override  
  115.         protected void onPostExecute(Object result) {  
  116.             Comm.print("getCurrentPosition----- " + getCurrentPosition());  
  117.             // 已经停止  
  118.             // 如果选中的项发生了变化,执行...  
  119.             if (isItemSelectedChange) {  
  120.                 if (null != onSelectedChange) {  
  121.                     onSelectedChange.onSelectedChange(getCurrentPosition());  
  122.                 }  
  123.             }  
  124.             isItemSelectedChange = false;  
  125.             super.onPostExecute(result);  
  126.         }  
  127.     }  
  128.   
  129.     protected HKGallerySelectedChange onSelectedChange;  
  130.   
  131.     public void setOnSelectedChange(HKGallerySelectedChange c) {  
  132.         this.onSelectedChange = c;  
  133.     }  
  134.       
  135.     public interface HKGallerySelectedChange {  
  136.   
  137.         public void onSelectedChange(int position);  
  138.   
  139.     }  
  140.   
  141.   
  142.     // ------------------------以下用户处理3d效果------------------------//  
  143.     @Override  
  144.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  145.         super.onLayout(changed, l, t, r, b);  
  146.         height = getHeight();  
  147.     }  
  148.   
  149.     public int getMaxRotationAngle() {  
  150.         return maxRotationAngle;  
  151.     }  
  152.   
  153.     public void setMaxRotationAngle(int maxRotationAngle) {  
  154.         this.maxRotationAngle = maxRotationAngle;  
  155.     }  
  156.   
  157.     public int getMaxZoom() {  
  158.         return maxZoom;  
  159.     }  
  160.   
  161.     public void setMaxZoom(int maxZoom) {  
  162.         this.maxZoom = maxZoom;  
  163.     }  
  164.   
  165.     private int getCenterOfCoverflow() {  
  166.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  
  167.                 + getPaddingLeft();  
  168.     }  
  169.   
  170.     private static int getCenterOfView(View view) {  
  171.         return view.getLeft() + view.getWidth() / 2;  
  172.     }  
  173.   
  174.     // 控制gallery中每个图片的旋转(重写的gallery中方法)  
  175.     @Override  
  176.     protected boolean getChildStaticTransformation(View child, Transformation t) {  
  177.         // 取得当前子view的半径值  
  178.         final int childCenter = getCenterOfView(child);  
  179.         final int childWidth = child.getWidth();  
  180.         // 旋转角度  
  181.         int rotationAngle = 0;  
  182.         // 重置转换状态  
  183.         t.clear();  
  184.         // 设置转换类型  
  185.         t.setTransformationType(Transformation.TYPE_BOTH);  
  186.         // 如果图片位于中心位置不需要进行旋转  
  187.         if (childCenter == coveflowCenter) {  
  188.             transformImageBitmap((ImageView) child, t, 0);  
  189.         } else {  
  190.             // 根据图片在gallery中的位置来计算图片的旋转角度  
  191.             rotationAngle = (int) (((float) (coveflowCenter - childCenter) / childWidth) * maxRotationAngle);  
  192.             // 如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)  
  193.             if (Math.abs(rotationAngle) > maxRotationAngle) {  
  194.                 rotationAngle = (rotationAngle < 0) ? -maxRotationAngle  
  195.                         : maxRotationAngle;  
  196.             }  
  197.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  198.         }  
  199.         return true;  
  200.     }  
  201.   
  202.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  203.         coveflowCenter = getCenterOfCoverflow();  
  204.         super.onSizeChanged(w, h, oldw, oldh);  
  205.     }  
  206.   
  207.     private void transformImageBitmap(ImageView child, Transformation t,  
  208.             int rotationAngle) {  
  209.         // 对效果进行保存  
  210.         camera.save();  
  211.   
  212.         // 返回旋转角度的绝对值  
  213.         // final int rotation = Math.abs(rotationAngle);  
  214.   
  215.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。  
  216.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。  
  217.         camera.translate(0f, 0f, maxZoom);  
  218.         // // 精确的缩放控制  
  219.         // if (rotation < mMaxRotationAngle) {  
  220.         // float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  221.         // mCamera.translate(0.0f, 0.0f, zoomAmount);  
  222.         // }  
  223.         // 如果在Y轴上旋转,对应图片竖向向里翻转。  
  224.         // 如果在X轴上旋转,对应图片横向向里翻转。  
  225.         camera.rotateY(rotationAngle);  
  226.         final Matrix imageMatrix = t.getMatrix();  
  227.         camera.getMatrix(imageMatrix);  
  228.         camera.restore();  
  229.   
  230.         // 设置旋转中心点  
  231.         // 图片高度,用的gallery的高度  
  232.         // 图片宽度,用的图片宽度;这里图片的高度取不到,都是-1,不知道什么原因  
  233.         final int imageWidth = child.getLayoutParams().width;  
  234.   
  235.         imageMatrix.preTranslate(-(imageWidth / 2), -height / 2);  
  236.         imageMatrix.postTranslate((imageWidth / 2), height / 2);  
  237.         camera.save();  
  238.     }  
  239.   
  240.     public int getCurrentPosition() {  
  241.         return currentPosition;  
  242.     }  
  243.   
  244.     public void setCurrentPosition(int currentPosition) {  
  245.         this.currentPosition = currentPosition;  
  246.     }  
  247.   
  248. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值