Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

打造炫酷的 Android Gallery 3D 立體滾動效果

編輯:Android 開發實例

Gallery 3D 效果以其立體翻轉、透鏡般的視覺體驗,一直是圖片瀏覽中的經典亮點。要實現它,核心在於自定義 Gallery,結合 Camera 類進行三維旋轉與縮放,再為圖片加上倒影。整個實現可分為三個部分。

一、擴展 Gallery:GalleryFlow

這一層主要負責控制每張圖片的旋轉與縮放。關鍵是重寫 getChildStaticTransformation 方法,根據圖片相對於中心的位置計算旋轉角度:

public class GalleryFlow extends Gallery {
    private Camera mCamera = new Camera();     // 相機類
    private int mMaxRotationAngle = 60;        // 最大轉動角度
    private int mMaxZoom = -300;               // 最大縮放值
    private int mCoveflowCenter;               // 半徑值

    public GalleryFlow(Context context) {
        super(context);
        // 支持轉換,執行 getChildStaticTransformation 方法
        this.setStaticTransformationsEnabled(true);
    }
    // ... 其餘構造方法同理

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

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

    // 控制 gallery 中每個圖片的旋轉(重寫 gallery 中方法)
    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);
        if (childCenter == mCoveflowCenter) {
            transformImageBitmap((ImageView) child, t, 0);
        } else {
            // 根據圖片在 gallery 中的位置計算旋轉角度
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
            }
            transformImageBitmap((ImageView) child, t, rotationAngle);
        }
        return true;
    }

    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 軸上正向移動視角,實際效果為放大圖片
        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 軸上旋轉,對應圖片豎向翻轉
        mCamera.rotateY(rotationAngle);
        mCamera.getMatrix(imageMatrix);
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
        mCamera.restore();
    }
}

二、填充圖片容器:ImageAdapter

這一層的核心任務是為每張圖片生成倒影效果。它透過 Matrix 對原圖做 Y 軸翻轉,再借助 LinearGradient 漸變與 PorterDuffXfermode 為倒影添加自然的淡出陰影:

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);  // 指定矩陣(x 軸不變,y 軸相反)

        // 返回寬度不變、高度為原圖 1/2 的倒影位圖
        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);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

        Paint paint = new Paint();
        paint.setAntiAlias(false);
        // 設置漸變陰影,讓倒影自然淡出
        LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap,
                0x70ffffff, 0x00ffffff, TileMode.MIRROR);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.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(300, 400));
        mImages[index++] = imageView;
    }
    return true;
}

三、創建 Activity

public class Gallery3DActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_gallery);

        Integer[] images = { R.drawable.img0001, R.drawable.img0030,
                R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,
                R.drawable.img0230, R.drawable.img0330, R.drawable.img0354 };

        ImageAdapter adapter = new ImageAdapter(this, images);
        adapter.createReflectedImages();  // 創建倒影效果
        GalleryFlow galleryFlow = (GalleryFlow) this.findViewById(R.id.Gallery01);
        galleryFlow.setFadingEdgeLength(0);
        galleryFlow.setSpacing(-100);  // 圖片之間的間距
        galleryFlow.setAdapter(adapter);

        galleryFlow.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });
        galleryFlow.setSelection(4);
    }
}

歸納一下:BaseAdapter 中主要完成了圖片的倒影效果以及顯示區域的創建;GalleryFlow 則負責根據圖片在屏幕中的位置對其進行旋轉與縮放。兩者協同,便誕生了這份頗具立體縱深感的三維滾動視覺。代碼中已附詳細注釋,稍加揣摩便能融會貫通。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved