Gallery

       刚开始做这个的时候,对于gallery了解的并不多,看起来一头雾脑 ,不知如何下手,下面我将结合我做的项目浅谈一下我对Gallery的理解

效果图是这样的

1、首先先创建activity_stinkpot.xml文件进行布局,使页面出现大体框架


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/layout_bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:padding="5dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_weight="1"
            android:text="奇奇怪怪的馬桶"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black" />
    
     <ImageView
         android:id="@+id/stinkpot_img"
         android:layout_marginTop="20dip"
        android:layout_width="250dip"
        android:layout_height="250dip"
        android:layout_gravity="center_horizontal"
        />

    <com.example.gallery.GalleryFlow
        android:id="@+id/mgallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
         />

</LinearLayout>

并对一些空件子项自定义


2、自定义Gallery来实现图片的浏览与选择

 

public class GalleryFlow extends Gallery {

	private Camera mCamera = new Camera();
	private int mMaxRotationAngle = 60;//最大旋转角度60
	private int mMaxZoom = -120;
	private int mCoveflowCenter;

	public GalleryFlow(Context context) {
		super(context);
		this.setStaticTransformationsEnabled(true);
	}

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

	public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setStaticTransformationsEnabled(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;
	}
	//获取Gallery的中心
	private int getCenterOfCoverflow() {
		return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
				+ getPaddingLeft();
	}
	//获取view中心
	private static int getCenterOfView(View view) {
		return view.getLeft() + view.getWidth() / 2;
	}

	protected boolean getChildStaticTransformation(View child, Transformation t) {

		final int childCenter = getCenterOfView(child);
		final int childWidth = child.getWidth();
		int rotationAngle = 0;

		t.clear();
		t.setTransformationType(Transformation.TYPE_MATRIX);//alpha和matrix都变换

		if (childCenter == mCoveflowCenter) {//正中间的childView
			transformImageBitmap((ImageView) child, t, 0);
		} else {//两侧的childView
			rotationAngle = (int) (((float) (mCoveflowCenter - 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) {
		mCoveflowCenter = getCenterOfCoverflow();
		super.onSizeChanged(w, h, oldw, oldh);
	}

	private void transformImageBitmap(ImageView child, Transformation t,
			int rotationAngle) {
		mCamera.save();
		final Matrix imageMatrix = t.getMatrix();
		final int imageHeight = child.getLayoutParams().height;
		final int imageWidth = child.getLayoutParams().width;
		final int rotation = Math.abs(rotationAngle);
		 // 在Z轴上正向移动camera的视角,实际效果为放大图片; 如果在Y轴上移动,则图片上下移动; X轴上对应图片左右移动。  
		mCamera.translate(0.0f, 0.0f, 100.0f);

		// As the angle of the view gets less, zoom in
		if (rotation < mMaxRotationAngle) {
			float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
			mCamera.translate(0.0f, 0.0f, zoomAmount);
		}

		
		mCamera.rotateY(rotationAngle); // rotationAngle 为正,沿y轴向内旋转; 为负,沿y轴向外旋转  
		mCamera.getMatrix(imageMatrix);
		imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
		imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
		mCamera.restore();
	}
}
1)先在Z轴上平称,其实就是得到一个缩放矩阵变换

2)是利用camera这个类来生成matrix,其实mCamera.rotateY就是围绕Y轴旋转。这里生成了一个旋转矩阵,此时调用mCamera.getMatrix(imageMatrix); 从Camera中得到matrix,

3)最后几句涉及到旋转与缩放,缩放操作其实应该是针对Child中点进行了,这里就是作一个平衡操作,我们必须是先平移,再缩放,再平移回原来位置

3、倒影的绘制

创建ImageAdapter继承自BaseAdapter,倒影效果是主要由原图+间距+倒影三部分组成,

原图,就是我们看到了最开始的图片

间距,是原图与倒影之间的间隙,我们这将此reflectionGap = 4;

倒影,是原图下半部分1/2高度,通过矩阵变换matrix.preScale(1, -1); 获取倒立图片,然后再加上线性遮罩和阴影实现

public class ImageAdapter extends BaseAdapter {

	int mGalleryItemBackground;
	private Context mContext;
	private Integer[] mImageIds;
	private ImageView[] mImages;

	public ImageAdapter(Context c, Integer[] ImageIds) {
		mContext = c;
		mImageIds = ImageIds;
		mImages = new ImageView[mImageIds.length];
	}
	//反射倒影
	public boolean createReflectedImages() {
		final int reflectionGap = 4;
		int index = 0;

		for (int imageId : mImageIds) {
			Bitmap originalImage = BitmapFactory.decodeResource(
					mContext.getResources(), imageId);//获取原图片
			int width = originalImage.getWidth();
			int height = originalImage.getHeight();

			Matrix matrix = new Matrix();
			matrix.preScale(1, -1);//图片矩阵变换,从底部到顶部倒影

			Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
					height / 2, width, height / 2, matrix, false);// 截取原图下半部分

			Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
					(height + height / 2), Config.ARGB_8888);//创建倒影图片(高度为原图3/2) 

			Canvas canvas = new Canvas(bitmapWithReflection); // 绘制倒影图即原图 + 间距 + 倒影

			canvas.drawBitmap(originalImage, 0, 0, null);//绘制原图

			Paint deafaultPaint = new Paint();
			canvas.drawRect(0, height, width, height + reflectionGap,
					deafaultPaint); //绘制原图与倒影的间距  

			canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // 绘制倒影图  

			Paint paint = new Paint();
			LinearGradient shader = new LinearGradient(0,
					originalImage.getHeight(), 0,
					bitmapWithReflection.getHeight() + reflectionGap,
					0x70ffffff, 0x00ffffff, TileMode.CLAMP);

			paint.setShader(shader); // 线性渐变效果

			paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 倒影遮罩效果

			canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
					+ reflectionGap, paint);// 绘制倒影的阴影效果  

			ImageView imageView = new ImageView(mContext);
			imageView.setImageBitmap(bitmapWithReflection);// 设置倒影图片
			imageView.setLayoutParams(new GalleryFlow.LayoutParams(180, 240));
			// imageView.setScaleType(ScaleType.MATRIX);
			mImages[index++] = imageView;
		}
		return true;
	}

	private Resources getResources() {
		// TODO Auto-generated method stub
		return null;
	}
	//获取图片位置
	public int getCount() {
		return mImageIds.length;
	}

	public Object getItem(int position) {
		return position;
	}
	//获取图片Id
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		return mImages[position];
	}

	public float getScale(boolean focused, int offset) {
		return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
	}

}
4、Activity的主要图片点击事件

public class StinkPot_Activity extends Activity{
	
	private ImageView showImage;
	GalleryFlow galleryFlow;
	ImageAdapter adapter;
	//图片组
	Integer[] images = { R.drawable.stinkpot001, R.drawable.stinkpot002,
			R.drawable.stinkpot003, R.drawable.stinkpot004, R.drawable.stinkpot005,
			R.drawable.stinkpot006, R.drawable.stinkpot007, R.drawable.stinkpot008,
			R.drawable.stinkpot009, R.drawable.stinkpot010, R.drawable.stinkpot011 };

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stinkpot);

		showImage=(ImageView)findViewById(R.id.stinkpot_img);
		showImage.setImageResource(images[0]);
		adapter = new ImageAdapter(this, images);
		adapter.createReflectedImages();
		// 图片单击事件
		AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO 自动生成的方法存根

				showImage.setImageResource(images[position]);
				// Toast.makeText(GalleryActivity.this, "图片 " + (position + 1),
				// Toast.LENGTH_SHORT).show();
			}

		};

		galleryFlow = (GalleryFlow) findViewById(R.id.mgallery);
		galleryFlow.setAdapter(adapter);
		galleryFlow.setOnItemClickListener(listener);// 注册图片单击

	}	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值