首先,文章中出现的Gallery 已经不再适用,替代方法请看我的另一篇文章http://blog.csdn.net/xiangzhihong8/article/details/51120460
不过对于文章中说的倒影的原理是可以借鉴的。
1.图片的显示以及切换主要是自定义了一个Gallery
下面是代码myGallery.java:
- 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;
- @SuppressWarnings("deprecation")
- public class myGallery extends Gallery {
- private Camera mCamera = new Camera();
- private int mMaxRotationAngle = 60; //图片偏转角度 60
- private int mMaxZoom = -120;
- private int mCoveflowCenter;
- 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 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;
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- mCoveflowCenter = getCenterOfCoverflow();
- super.onSizeChanged(w, h, oldw, oldh);
- }
- @Override
- protected boolean getChildStaticTransformation(View child, Transformation trans) {
- final int childCenter = getCenterOfView(child);
- final int childWidth = child.getWidth();
- int rotationAngle = 0;
- trans.clear();
- trans.setTransformationType(Transformation.TYPE_BOTH); // alpha和 matrix都变换
- if (childCenter == mCoveflowCenter) { //正中间的childView
- transformImageBitmap((ImageView) child, trans, 0);
- } else { //两侧的childView
- rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle );
- if (Math.abs(rotationAngle) > mMaxRotationAngle) {
- rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
- }
- transformImageBitmap((ImageView) child, trans, rotationAngle);
- }
- return true;
- }
- private void transformImageBitmap(ImageView child, Transformation trans, int rotationAngle) {
- mCamera.save();
- final Matrix imageMatrix = trans.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, 200.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();
- }
- }
2.接下来就是要为图片添加倒影了,用过PhotoShop的都知道添加倒影就是将原有图片倒置,设置渐变式的显示,再将其放在原图片下面就行了,这里的方法也是一样
在为Gallery添加图片的同时,为每个图片添加倒影,需要在Adapter中做
下面就是相关代码 ImageAdapter.java:
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.LinearGradient;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.PorterDuff.Mode;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.Shader.TileMode;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ImageView.ScaleType;
- public class ImageAdapter extends BaseAdapter {
- private ImageView[] mImages; // 存储每个图片的ImageView
- private Context mContext;
- public List<Map<String, Object>> list;
- public Integer[] imgs = { R.drawable.img1, R.drawable.img2,
- R.drawable.img3, R.drawable.img4, R.drawable.img5 };
- public String[] titles = { "孙燕姿", "就是要唱歌", "微笑", "大海", "漂亮"};
- public ImageAdapter(Context c) {
- this.mContext = c;
- list = new ArrayList<Map<String, Object>>();
- for (int i = 0; i < imgs.length; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("image", imgs[i]);
- list.add(map);
- }
- mImages = new ImageView[list.size()];
- }
- /**
- * 创建倒影效果
- */
- @SuppressWarnings("deprecation")
- public boolean createReflectedImages() {
- final int reflectionGap = 4;//原图与倒影之间的间隙
- int index = 0;
- for (Map<String, Object> map : list) {
- Integer id = (Integer) map.get("image");
- Bitmap originalImage = BitmapFactory.decodeResource(
- mContext.getResources(), id); // 获得图片资源
- // 获得图片的长宽
- 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对象,图片高是原图的一半
- Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
- (height + height / 2), Config.ARGB_8888); // 创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍
- Canvas canvas = new Canvas(bitmapWithReflection);
- canvas.drawBitmap(originalImage, 0, 0, null); // 创建画布对象,将原图画于画布,起点是原点位置
- Paint paint = new Paint();
- canvas.drawRect(0, height, width, height + reflectionGap, paint);
- canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // 将反转后的图片画到画布中
- paint = new Paint();
- LinearGradient shader = new LinearGradient(0,
- originalImage.getHeight(), 0,
- bitmapWithReflection.getHeight() + reflectionGap,
- 0x70ffffff, 0x00ffffff, TileMode.MIRROR);// 创建线性渐变LinearGradient对象
- 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); // 设置带倒影的Bitmap
- //设置ImageView的大小,可以根据图片大小设置
- // imageView.setLayoutParams(newmyGallery.LayoutParams(width,height));
- imageView.setLayoutParams(new myGallery.LayoutParams(250, 500));//设置ImageView的大小,可根据需要设置固定宽高
- imageView.setScaleType(ScaleType.FIT_CENTER);//将图片按比例缩放
- mImages[index++] = imageView;
- }
- return true;
- }
- @Override
- public int getCount() {
- return imgs.length;
- }
- @Override
- public Object getItem(int position) {
- return mImages[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- return mImages[position]; // 获得Gallery中对应位置的ImageView
- }
- public float getScale(boolean focused, int offset) {
- return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
- }
- }
3.然后在主Activity中进行配置Main.java:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Main extends Activity {
- private TextView tvTitle;
- private myGallery gallery;
- private ImageAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initRes();
- }
- private void initRes(){
- tvTitle = (TextView) findViewById(R.id.tvTitle);
- gallery = (myGallery) findViewById(R.id.mygallery); // 获取自定义的myGallery控件
- adapter = new ImageAdapter(this);
- adapter.createReflectedImages(); // 创建倒影效果
- gallery.setAdapter(adapter);
- gallery.setOnItemSelectedListener(new OnItemSelectedListener() { // 设置选择事件监听
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- tvTitle.setText(adapter.titles[position]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- gallery.setOnItemClickListener(new OnItemClickListener() { // 设置点击事件监听
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(Main.this, "img " + (position+1) + " selected", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
4.最后是页面布局main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:textSize="16sp" />
- <com.homer.reflect.myGallery
- android:id="@+id/mygallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true" />
- </RelativeLayout>
5.大功告成啦!看看效果: