上篇介紹了使用Animation實現3D動畫旋轉翻頁效果,現在介紹圖片倒影實現,先看效果圖
本示例主要通過自定義Gallery和ImageAdapter(繼承自BaseAdapter)實現
1、倒影繪製
ImageAdapter繼承自BaseAdapter,詳細實現可見 Android 滑動效果入門篇(二)—— Gallery 這裏重點介紹倒影原理及實現
倒影原理:
倒影效果是主要由原圖+間距+倒影三部分組成,高度大約爲原圖的3/2(原圖爲1、倒影爲1/2)
原圖,就是我們看到了最開始的圖片
間距,是原圖與倒影之間的間隙,如:reflectionGap = 4;
倒影,是原圖下半部分1/2高度,通過矩陣變換matrix.preScale(1, -1); 獲取倒立圖片,然後再加上線性遮罩和陰影實現
倒影實現:
- /** 反射倒影 */
- 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); // 創建倒影圖片(高度爲原圖3/2)
- 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.CLAMP);
- 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(180, 240));
- imageView.setScaleType(ScaleType.MATRIX);
- mImages[index++] = imageView;
- }
- return true;
- }
/** 反射倒影 */
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); // 創建倒影圖片(高度爲原圖3/2)
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.CLAMP);
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(180, 240));
imageView.setScaleType(ScaleType.MATRIX);
mImages[index++] = imageView;
}
return true;
}
2、myGallery
自定義Gallery來實現倒影圖片的瀏覽與選擇
- 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的中心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, 100.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();
- }
- }
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的中心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, 100.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();
}
}
3、Activity
Activity中,主要實現自定義Gallery的圖片填充ImageAdapter、myGallery選擇事件監聽、點擊事件監聽
- 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();
- }
- });
- }
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();
}
});
}
main.xml佈局文件中,通過實現自定義的myGallery,來顯示圖片集合
- <?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_below="@id/tvTitle"
- android:layout_marginTop="10dip" />
- </RelativeLayout>
<?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_below="@id/tvTitle"
android:layout_marginTop="10dip" />
</RelativeLayout>
參考推薦: