歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 自定義Gallery實現3D立體,倒影的效果

自定義Gallery實現3D立體,倒影的效果

日期:2017/3/1 10:06:29   编辑:Linux編程

Android自帶的Gallery畫廊控件位於屏幕的底部,是很不錯很漂亮的一個控件。我們也可以自己更改gallery,來實現不同的效果,也可以加上倒影,翻轉等華麗的界面,一直都比較喜歡研究android的UI,所以特別留意了一下有關重寫Gallery的方法,網上有很多種樣式,我找到了一個比較普遍的改寫樣式,找不到原作者了,自己收藏起來並重新做了調整,加上了更為詳細的注釋,便於日後有需要的時候用,這個Demo完全可以自己作為工具類在以後做圖片處理的時候使用,方便,下面是實現後的效果圖以及部分代碼,可在文章底部下載本demo完整代碼~!

自定義的gallery:

package com.cogent.gallery;

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;

public class MyGallery extends Gallery {

private Camera mCamera = new Camera();
/** 最大轉動角度 */
private int mMaxRotationAngle = 30;
/** 最大縮放值 */
private int mMaxZoom = -300;
/** 半徑值 */
private int mRadius;

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 getmMaxRotationAngle() {
return mMaxRotationAngle;
}

public void setmMaxRotationAngle(int mMaxRotationAngle) {
this.mMaxRotationAngle = mMaxRotationAngle;
}

public int getmMaxZoom() {
return mMaxZoom;
}

public void setmMaxZoom(int mMaxZoom) {
this.mMaxZoom = mMaxZoom;
}

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

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

// 重寫gallery中的方法,控制gallery中的每個圖片的旋轉
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
// TODO Auto-generated method stub
// 取得當前子view的半徑值
int childCenter = getCenterOfView(child);
// 取得當前子view的寬度
int childWidth = child.getWidth();
// 旋轉的角度
int rotationAngle = 0;
// 重置轉換狀態
t.clear();
// 設置轉換類型
t.setTransformationType(Transformation.TYPE_MATRIX);
//如果圖片位於中心位置不需要進行轉換
if (childCenter == mRadius) {
transformImageBitmap((ImageView) child, t, 0);
} else {
// 根據圖片在gallery中的位置來計算圖片的旋轉角度
rotationAngle = (int) (((float) (mRadius - 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) {
mRadius = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}

private void transformImageBitmap(ImageView child, Transformation t,
int rotationAngle) {
// 對效果進行保存
mCamera.save();
Matrix imageMatrix = t.getMatrix();
// 圖片的高度
int imageHeight = child.getLayoutParams().height;
// 圖片的寬度
int imageWidth = child.getLayoutParams().width;
// 返回旋轉角度的絕對值
int rotation = Math.abs(rotationAngle);

// 在Z軸上正向移動camera的視角,實際效果為放大圖片。
// 如果在Y軸上移動,則圖片上下移動;X軸上對應圖片左右移動。
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軸上旋轉,對圖片縱向向裡翻轉。
// 如果在X軸上旋轉,則對應圖片橫向向裡翻轉。
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
imageMatrix.preTranslate(imageWidth / 2, imageHeight / 2);
mCamera.restore();
}

}

僅僅是收藏做學習用,雖然界面還不夠漂亮,但是可以慢慢研究裡面的代碼自行改動,下面是源代碼

源碼下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/11月/26日/自定義Gallery實現3D立體,倒影的效果

Copyright © Linux教程網 All Rights Reserved