之前写的《Android ImageSwitcher和Gallery的使用》一文中提到我在教室一下午为实现那个效果找各种资料。期间在网上找了一个个人觉得比较不错的效果,现在贴图上来:

Android 自定义Gallery浏览图片

其实这个效果使用的知识点就是图像的获取、创建、缩放、旋转、Matrix类、Canvas类等,另外就是自定义的Gallery控件。

相信大家都期待马上上代码了吧,嘻嘻。(注释比较多,相信大家都能看懂。)

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"
    android:background="@android:color/background_light" >

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="16sp" />
    
    <com.example.demo.GalleryView        
        android:id="@+id/mygallery"
        android:spacing="20dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:unselectedAlpha="1.2"
        android:layout_below="@id/tvTitle"
        android:layout_marginTop="10dip" />

</RelativeLayout>

新建一个GalleryView类:

public class GalleryView extends Gallery {

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

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // e1是按下的事件,e2是抬起的事件
        int keyCode;
        if (isScrollingLeft(e1, e2)) {
            keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
        } else {
            keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
        }
        onKeyDown(keyCode, null);
        return true;
        
    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
        return e2.getX() > e1.getX();
    }

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

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

    public GalleryView(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的中心x */
    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
                + getPaddingLeft();
    }

    /** 获取View的中心x */
    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, -20.0f);

        // As the angle of the view gets less, zoom in
        if (rotation < mMaxRotationAngle) {
            float zoomAmount = (float) (mMaxZoom + (rotation * 1.0));
            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();
    }
}
View Code

相关文章: