自定义Gallery实现立体,倒影的效果

    Android自带的Gallery画廊控件位于屏幕的底部,是很不错很漂亮的一个控件。我们也可以自己更改gallery,来实现不同的效果,也可以加上倒影,翻转等华丽的界面,一直都比较喜欢研究android的UI,所以特别留意了一下有关重写Gallery的方法,网上有很多种样式,我找到了一个比较普遍的改写样式,找不到原作者了,自己收藏起来并重新做了调整,加上了更为详细的注释,便于日后有需要的时候用,这个Demo完全可以自己作为工具类在以后做图片处理的时候使用,方便,下面是实现后的效果图以及部分代码,可在文章底部下载本demo完整代码~!

                                           

 

自定义的gallery:

package com.cogent.gallery;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;

public class MyGallery extends Gallery {

	private Camera mCamera = new Camera();
	/** 最大转动角度 */
	private int mMaxRotationAngle = 30;
	/** 最大缩放值 */
	private int mMaxZoom = -300;
	/** 半径值 */
	private int mRadius;
	
	public MyGallery(Context context){
		super(context);
		this.setStaticTransformationsEnabled(true);
	}

	public MyGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setStaticTransformationsEnabled(true);
	}

	public MyGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setStaticTransformationsEnabled(true);
	}

	public int getmMaxRotationAngle() {
		return mMaxRotationAngle;
	}

	public void setmMaxRotationAngle(int mMaxRotationAngle) {
		this.mMaxRotationAngle = mMaxRotationAngle;
	}

	public int getmMaxZoom() {
		return mMaxZoom;
	}

	public void setmMaxZoom(int mMaxZoom) {
		this.mMaxZoom = mMaxZoom;
	}

	private int getCenterOfCoverflow() {
		return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
				+ getPaddingLeft();
	}

	private static int getCenterOfView(View view) {
		return view.getLeft() + view.getWidth() / 2;
	}

	// 重写gallery中的方法,控制gallery中的每个图片的旋转
	@Override
	protected boolean getChildStaticTransformation(View child, Transformation t) {
		// TODO Auto-generated method stub
		// 取得当前子view的半径值
		int childCenter = getCenterOfView(child);
		// 取得当前子view的宽度
		int childWidth = child.getWidth();
		// 旋转的角度
		int rotationAngle = 0;
		// 重置转换状态
		t.clear();
		// 设置转换类型
		t.setTransformationType(Transformation.TYPE_MATRIX);
		//如果图片位于中心位置不需要进行转换
		if (childCenter == mRadius) {
			transformImageBitmap((ImageView) child, t, 0);
		} else {
			// 根据图片在gallery中的位置来计算图片的旋转角度
			rotationAngle = (int) (((float) (mRadius - childCenter) / childWidth) * mMaxRotationAngle);
			if (Math.abs(rotationAngle) > mMaxRotationAngle) {
				rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
						: mMaxRotationAngle;
			}
			transformImageBitmap((ImageView) child, t, rotationAngle);
		}
		return true;
	}

	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mRadius = getCenterOfCoverflow();
		super.onSizeChanged(w, h, oldw, oldh);
	}

	private void transformImageBitmap(ImageView child, Transformation t,
			int rotationAngle) {
		// 对效果进行保存
		mCamera.save();
		Matrix imageMatrix = t.getMatrix();
		// 图片的高度
		int imageHeight = child.getLayoutParams().height;
		// 图片的宽度
		int imageWidth = child.getLayoutParams().width;
		// 返回旋转角度的绝对值
		int rotation = Math.abs(rotationAngle);

		// 在Z轴上正向移动camera的视角,实际效果为放大图片。
		// 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
		mCamera.translate(0.0f, 0.0f, 100.0f);
		if (rotation < mMaxRotationAngle) {
			float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
			mCamera.translate(0.0f, 0.0f, zoomAmount);
		}

		// 在Y轴上旋转,对图片纵向向里翻转。
		// 如果在X轴上旋转,则对应图片横向向里翻转。
		mCamera.rotateY(rotationAngle);
		mCamera.getMatrix(imageMatrix);
		imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
		imageMatrix.preTranslate(imageWidth / 2, imageHeight / 2);
		mCamera.restore();
	}

}


仅仅是收藏做学习用,虽然界面还不够漂亮,但是可以慢慢研究里面的代码自行改动,下面是源代码

点击下载源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值