admin管理员组文章数量:1130349
Android Gallery简单3d效果使用
为避免每次都要去找,还是自己整理一下好了。有需要的同学自行下载源码,只要稍微有点基本功的同学估计都看得懂!至于新手而言,能拿来用就好了,我们并不关心“手机”是如何生产出来的,只要知道手机怎么使用就好。。。
Gallery修改类:
/*** The camera class is used to 3D transformation matrix.*/private Camera mCamera = new Camera();private Matrix mMatrix = new Matrix();/*** The max rotation angle.*/private int mMaxRotationAngle = 60;/*** The max zoom value (Z axis).*/private int mMaxZoom = -120;/*** The center of the gallery.*/private int mCoveflowCenter = 0;public GalleryFlow(Context context) {this(context, null);// this.setStaticTransformationsEnabled(true);// Enable set the children drawing order.this.setChildrenDrawingOrderEnabled(true);}public GalleryFlow(Context context, AttributeSet attrs) {this(context, attrs, 0);// this.setStaticTransformationsEnabled(true);// Enable set the children drawing order.this.setChildrenDrawingOrderEnabled(true);}public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// Enable set transformation.// this.setStaticTransformationsEnabled(true);// Enable set the children drawing order.this.setChildrenDrawingOrderEnabled(true);}public int getMaxRotationAngle() {return mMaxRotationAngle;}public void setMaxRotationAngle(int maxRotationAngle) {mMaxRotationAngle = maxRotationAngle;}public int getMaxZoom() {return mMaxZoom;}public void setMaxZoom(int maxZoom) {mMaxZoom = maxZoom;}@Overrideprotected int getChildDrawingOrder(int childCount, int i) {// Current selected index.int selectedIndex = getSelectedItemPosition()- getFirstVisiblePosition();if (selectedIndex < 0) {return i;}if (i < selectedIndex) {return i;} else if (i >= selectedIndex) {return childCount - 1 - i + selectedIndex;} else {return i;}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {mCoveflowCenter = getCenterOfCoverflow();super.onSizeChanged(w, h, oldw, oldh);}// 获得Coverflow的中点(应该是图片流视图的中点)private int getCenterOfCoverflow() {return ((getWidth() - getPaddingLeft() - getPaddingRight()) >> 1)+ getPaddingLeft();// return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 +// getPaddingLeft();}// 获得子view的中点private int getCenterOfView(View view) {return view.getLeft() + view.getWidth() / 2;}// 计算 child 偏离 父控件中心的 offset 值, -1 <= offset <= 1protected float calculateOffsetOfCenter(View view) {final int pCenter = getCenterOfCoverflow();final int cCenter = getCenterOfView(view);float offset = (cCenter - pCenter) / (pCenter * 1.0f);offset = Math.min(offset, 1.0f);offset = Math.max(offset, -1.0f);return offset;}// @Override// public void setUnselectedAlpha(float unselectedAlpha) {// unselectedAlpha=0xff;// super.setUnselectedAlpha(unselectedAlpha);// }private void transformImageBitmap(View child, Matrix mt, int rotationAngle) {mCamera.save();final Matrix imageMatrix = mt;final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);final int halfHeight = child.getMeasuredHeight() >> 1;final int rotation = Math.abs(rotationAngle);// Zoom on Z axis.mCamera.translate(0, 0, mMaxZoom);if (rotation < mMaxRotationAngle) {float zoomAmount = (float) (mMaxZoom + rotation * 1.5f);mCamera.translate(0, 0, zoomAmount);}// Rotate the camera on Y axis.mCamera.rotateY(rotationAngle);// Get the matrix from the camera, in fact, the matrix is S (scale)// transformation.mCamera.getMatrix(imageMatrix);// The matrix final is T2 * S * T1, first translate the center point to// (0, 0),// then scale, and then translate the center point to its original// point.// T * S * TmMatrix.preTranslate(-halfWidth, -halfHeight - 15); // 还没清楚为什么要减去15mMatrix.postTranslate(halfWidth, halfHeight - 15);mCamera.restore();}void getTransformationMatrix(View child, float offset) {final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);final int halfHeight = child.getMeasuredHeight() >> 1;mCamera.save();mCamera.translate(-offset * 50, 0.0f, Math.abs(offset) * 200);mCamera.getMatrix(mMatrix);mCamera.restore();mMatrix.preTranslate(-halfWidth, -halfHeight);mMatrix.postTranslate(halfWidth, halfHeight);}@Overrideprotected boolean drawChild(Canvas canvas, View child, long drawingTime) {// TODO Auto-generated method stubboolean ret;// Android SDK 4.1if (android.os.Build.VERSION.SDK_INT > 15) {getChildStaticTrans(child, mMatrix);// final float offset = calculateOffsetOfCenter(child);// getTransformationMatrix(child, offset);// child.setAlpha(1 - Math.abs(offset));//final int saveCount = canvas.save();canvas.concat(mMatrix);ret = super.drawChild(canvas, child, drawingTime);canvas.restoreToCount(saveCount);} else {ret = super.drawChild(canvas, child, drawingTime);}return ret;}protected boolean getChildStaticTrans(View child, Matrix mt) {final int childCenter = getCenterOfView(child);final int childWidth = child.getWidth();int rotationAngle = 0;if (childCenter == mCoveflowCenter) {transformImageBitmap(child, mt, 0);} else {// Calculate the rotation angle.rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);// Make the angle is not bigger than maximum.if (Math.abs(rotationAngle) > mMaxRotationAngle) {rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle: mMaxRotationAngle;}transformImageBitmap(child, mt, rotationAngle);}return true;}
使用代码片段
/*** 三步骤*/private void initPBListCtrl() {mGallery = (GalleryFlow) findViewById(R.id.gallery_phonebook);mGallery.setSpacing(3);// 设置画间间隔mGallery.setAnimationDuration(2000);mGallery.setGravity(Gravity.CENTER_VERTICAL);mGallery.setOnItemClickListener(this);mGallery.setOnItemSelectedListener(this);mAdapter = new MyAdapter(this, mAllContactsList);mGallery.setAdapter(mAdapter);}
源码点击下载
Android Gallery简单3d效果使用
为避免每次都要去找,还是自己整理一下好了。有需要的同学自行下载源码,只要稍微有点基本功的同学估计都看得懂!至于新手而言,能拿来用就好了,我们并不关心“手机”是如何生产出来的,只要知道手机怎么使用就好。。。
Gallery修改类:
/*** The camera class is used to 3D transformation matrix.*/private Camera mCamera = new Camera();private Matrix mMatrix = new Matrix();/*** The max rotation angle.*/private int mMaxRotationAngle = 60;/*** The max zoom value (Z axis).*/private int mMaxZoom = -120;/*** The center of the gallery.*/private int mCoveflowCenter = 0;public GalleryFlow(Context context) {this(context, null);// this.setStaticTransformationsEnabled(true);// Enable set the children drawing order.this.setChildrenDrawingOrderEnabled(true);}public GalleryFlow(Context context, AttributeSet attrs) {this(context, attrs, 0);// this.setStaticTransformationsEnabled(true);// Enable set the children drawing order.this.setChildrenDrawingOrderEnabled(true);}public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// Enable set transformation.// this.setStaticTransformationsEnabled(true);// Enable set the children drawing order.this.setChildrenDrawingOrderEnabled(true);}public int getMaxRotationAngle() {return mMaxRotationAngle;}public void setMaxRotationAngle(int maxRotationAngle) {mMaxRotationAngle = maxRotationAngle;}public int getMaxZoom() {return mMaxZoom;}public void setMaxZoom(int maxZoom) {mMaxZoom = maxZoom;}@Overrideprotected int getChildDrawingOrder(int childCount, int i) {// Current selected index.int selectedIndex = getSelectedItemPosition()- getFirstVisiblePosition();if (selectedIndex < 0) {return i;}if (i < selectedIndex) {return i;} else if (i >= selectedIndex) {return childCount - 1 - i + selectedIndex;} else {return i;}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {mCoveflowCenter = getCenterOfCoverflow();super.onSizeChanged(w, h, oldw, oldh);}// 获得Coverflow的中点(应该是图片流视图的中点)private int getCenterOfCoverflow() {return ((getWidth() - getPaddingLeft() - getPaddingRight()) >> 1)+ getPaddingLeft();// return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 +// getPaddingLeft();}// 获得子view的中点private int getCenterOfView(View view) {return view.getLeft() + view.getWidth() / 2;}// 计算 child 偏离 父控件中心的 offset 值, -1 <= offset <= 1protected float calculateOffsetOfCenter(View view) {final int pCenter = getCenterOfCoverflow();final int cCenter = getCenterOfView(view);float offset = (cCenter - pCenter) / (pCenter * 1.0f);offset = Math.min(offset, 1.0f);offset = Math.max(offset, -1.0f);return offset;}// @Override// public void setUnselectedAlpha(float unselectedAlpha) {// unselectedAlpha=0xff;// super.setUnselectedAlpha(unselectedAlpha);// }private void transformImageBitmap(View child, Matrix mt, int rotationAngle) {mCamera.save();final Matrix imageMatrix = mt;final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);final int halfHeight = child.getMeasuredHeight() >> 1;final int rotation = Math.abs(rotationAngle);// Zoom on Z axis.mCamera.translate(0, 0, mMaxZoom);if (rotation < mMaxRotationAngle) {float zoomAmount = (float) (mMaxZoom + rotation * 1.5f);mCamera.translate(0, 0, zoomAmount);}// Rotate the camera on Y axis.mCamera.rotateY(rotationAngle);// Get the matrix from the camera, in fact, the matrix is S (scale)// transformation.mCamera.getMatrix(imageMatrix);// The matrix final is T2 * S * T1, first translate the center point to// (0, 0),// then scale, and then translate the center point to its original// point.// T * S * TmMatrix.preTranslate(-halfWidth, -halfHeight - 15); // 还没清楚为什么要减去15mMatrix.postTranslate(halfWidth, halfHeight - 15);mCamera.restore();}void getTransformationMatrix(View child, float offset) {final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);final int halfHeight = child.getMeasuredHeight() >> 1;mCamera.save();mCamera.translate(-offset * 50, 0.0f, Math.abs(offset) * 200);mCamera.getMatrix(mMatrix);mCamera.restore();mMatrix.preTranslate(-halfWidth, -halfHeight);mMatrix.postTranslate(halfWidth, halfHeight);}@Overrideprotected boolean drawChild(Canvas canvas, View child, long drawingTime) {// TODO Auto-generated method stubboolean ret;// Android SDK 4.1if (android.os.Build.VERSION.SDK_INT > 15) {getChildStaticTrans(child, mMatrix);// final float offset = calculateOffsetOfCenter(child);// getTransformationMatrix(child, offset);// child.setAlpha(1 - Math.abs(offset));//final int saveCount = canvas.save();canvas.concat(mMatrix);ret = super.drawChild(canvas, child, drawingTime);canvas.restoreToCount(saveCount);} else {ret = super.drawChild(canvas, child, drawingTime);}return ret;}protected boolean getChildStaticTrans(View child, Matrix mt) {final int childCenter = getCenterOfView(child);final int childWidth = child.getWidth();int rotationAngle = 0;if (childCenter == mCoveflowCenter) {transformImageBitmap(child, mt, 0);} else {// Calculate the rotation angle.rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);// Make the angle is not bigger than maximum.if (Math.abs(rotationAngle) > mMaxRotationAngle) {rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle: mMaxRotationAngle;}transformImageBitmap(child, mt, rotationAngle);}return true;}
使用代码片段
/*** 三步骤*/private void initPBListCtrl() {mGallery = (GalleryFlow) findViewById(R.id.gallery_phonebook);mGallery.setSpacing(3);// 设置画间间隔mGallery.setAnimationDuration(2000);mGallery.setGravity(Gravity.CENTER_VERTICAL);mGallery.setOnItemClickListener(this);mGallery.setOnItemSelectedListener(this);mAdapter = new MyAdapter(this, mAllContactsList);mGallery.setAdapter(mAdapter);}
源码点击下载
本文标签: Android Gallery简单3d效果使用
版权声明:本文标题:Android Gallery简单3d效果使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/IT/1694679704a254975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论