首先,文章中出现的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;
- 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;
- }
-
-
- private int getCenterOfCoverflow() {
- return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
- }
-
-
- 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);
-
- if (childCenter == mCoveflowCenter) {
- transformImageBitmap((ImageView) child, trans, 0);
- } else {
- 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);
-
-
- mCamera.translate(0.0f, 0.0f, 200.0f);
-
-
- if (rotation < mMaxRotationAngle) {
- float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
- mCamera.translate(0.0f, 0.0f, zoomAmount);
- }
-
- mCamera.rotateY(rotationAngle);
-
- 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;
-
- 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 bitmapWithReflection = Bitmap.createBitmap(width,
- (height + height / 2), Config.ARGB_8888);
-
- 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);
- 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 myGallery.LayoutParams(250, 500));
- 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];
- }
-
- 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);
-
- 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.大功告成啦!看看效果: