阅读更多
import android.content.Context;import android.graphics.Camera;import android.graphics.Matrix;import android.os.AsyncTask;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.Transformation;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageView;import com.hk.shop.comm.Comm;/** * 用在首页产品展示模块的gallery
* 1、这是一个3d的gallery
* 2、能自动判断当前选中的图片,并实现回调 * * @author yizhe * @date 2012-6-19 */public class HKGallery4Module extends Gallery {Context context;// 以下属性用于处理3dprivate Camera camera = new Camera();// 相机类private int maxRotationAngle = 60;// 最大转动角度private int maxZoom = 60;// z方向的移动至,相当于缩放private int coveflowCenter;// 半径值private int height;// 以下属性用于处理获取当前选中的项private int currentPosition;private int lastPosition;public int getLastPosition() {return lastPosition;}public void setLastPosition(int lastPosition) {this.lastPosition = lastPosition;}boolean isItemSelectedChange;// 选中的项是否发生了变化public HKGallery4Module(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.context = context;this.setStaticTransformationsEnabled(true);ini();}public HKGallery4Module(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;this.setStaticTransformationsEnabled(true);ini();}public HKGallery4Module(Context context) {super(context);this.context = context;this.setStaticTransformationsEnabled(true);ini();}void ini() {setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {setCurrentPosition(position);isItemSelectedChange = true;}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}// ------------------------以下用户处理获取当前选中项------------------------//@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_UP) {JudgeTask task = new JudgeTask();task.execute(); // 手离开开始执行,判断gallery是否停止}return super.onTouchEvent(event);}// ------------------判断停止----------------//class JudgeTask extends AsyncTask {int i = -1;// 测试@Overrideprotected Object doInBackground(Object... params) {// 每500毫秒执行一次判断,实验证明可以精确判断try {do {i++;lastPosition = getCurrentPosition();Thread.sleep(500);} while (lastPosition != getCurrentPosition());} catch (InterruptedException e) {}return null;}@Overrideprotected void onPostExecute(Object result) {Comm.print("getCurrentPosition----- " + getCurrentPosition());// 已经停止// 如果选中的项发生了变化,执行...if (isItemSelectedChange) {if (null != onSelectedChange) {onSelectedChange.onSelectedChange(getCurrentPosition());}}isItemSelectedChange = false;super.onPostExecute(result);}}protected HKGallerySelectedChange onSelectedChange;public void setOnSelectedChange(HKGallerySelectedChange c) {this.onSelectedChange = c;}public interface HKGallerySelectedChange {public void onSelectedChange(int position);}// ------------------------以下用户处理3d效果------------------------//@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);height = getHeight();}public int getMaxRotationAngle() {return maxRotationAngle;}public void setMaxRotationAngle(int maxRotationAngle) {this.maxRotationAngle = maxRotationAngle;}public int getMaxZoom() {return maxZoom;}public void setMaxZoom(int maxZoom) {this.maxZoom = maxZoom;}private int getCenterOfCoverflow() {return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2+ getPaddingLeft();}private static int getCenterOfView(View view) {return view.getLeft() + view.getWidth() / 2;}// 控制gallery中每个图片的旋转(重写的gallery中方法)@Overrideprotected boolean getChildStaticTransformation(View child, Transformation t) {// 取得当前子view的半径值final int childCenter = getCenterOfView(child);final int childWidth = child.getWidth();// 旋转角度int rotationAngle = 0;// 重置转换状态t.clear();// 设置转换类型t.setTransformationType(Transformation.TYPE_BOTH);// 如果图片位于中心位置不需要进行旋转if (childCenter == coveflowCenter) {transformImageBitmap((ImageView) child, t, 0);} else {// 根据图片在gallery中的位置来计算图片的旋转角度rotationAngle = (int) (((float) (coveflowCenter - childCenter) / childWidth) * maxRotationAngle);// 如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)if (Math.abs(rotationAngle) > maxRotationAngle) {rotationAngle = (rotationAngle < 0) ? -maxRotationAngle: maxRotationAngle;}transformImageBitmap((ImageView) child, t, rotationAngle);}return true;}protected void onSizeChanged(int w, int h, int oldw, int oldh) {coveflowCenter = getCenterOfCoverflow();super.onSizeChanged(w, h, oldw, oldh);}private void transformImageBitmap(ImageView child, Transformation t,int rotationAngle) {// 对效果进行保存camera.save();// 返回旋转角度的绝对值// final int rotation = Math.abs(rotationAngle);// 在Z轴上正向移动camera的视角,实际效果为放大图片。// 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。camera.translate(0f, 0f, maxZoom);// // 精确的缩放控制// if (rotation < mMaxRotationAngle) {// float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));// mCamera.translate(0.0f, 0.0f, zoomAmount);// }// 如果在Y轴上旋转,对应图片竖向向里翻转。// 如果在X轴上旋转,对应图片横向向里翻转。camera.rotateY(rotationAngle);final Matrix imageMatrix = t.getMatrix();camera.getMatrix(imageMatrix);camera.restore();// 设置旋转中心点// 图片高度,用的gallery的高度// 图片宽度,用的图片宽度;这里图片的高度取不到,都是-1,不知道什么原因final int imageWidth = child.getLayoutParams().width;imageMatrix.preTranslate(-(imageWidth / 2), -height / 2);imageMatrix.postTranslate((imageWidth / 2), height / 2);camera.save();}public int getCurrentPosition() {return currentPosition;}public void setCurrentPosition(int currentPosition) {this.currentPosition = currentPosition;}}

更多相关文章

  1. android 3D gallery 并 判断当前选中项
  2. android之Matrix
  3. Android(安卓)通过HTTP POST 上传图片文件
  4. android 在SDCard获取图片
  5. android API Demo之使用ViewFlipper制作轮播广告
  6. 临时
  7. android 仿淘宝的加载刷新效果
  8. android代码实现按钮自旋转
  9. 后续:优化

随机推荐

  1. Android(安卓)设置DrawableRight和Drawab
  2. Android(安卓)获取手机流量
  3. Linearlayout的android:divider属性
  4. Android利用Fiddler进行网络数据抓包
  5. 查看Android进程内存资源信息
  6. android:shape的使用
  7. 《Android高级开发》课程总结及知识点复
  8. Android(安卓)Context的意义
  9. 3.6.3新版本AndroidStudio报Could not re
  10. Eclipse环境下格式化Android的代码风格