Android 滑動效果進階篇(六)—— 倒影效果

上篇介紹了使用Animation實現3D動畫旋轉翻頁效果,現在介紹圖片倒影實現,先看效果圖


本示例主要通過自定義Gallery和ImageAdapter(繼承自BaseAdapter)實現


1、倒影繪製

ImageAdapter繼承自BaseAdapter,詳細實現可見 Android 滑動效果入門篇(二)—— Gallery 這裏重點介紹倒影原理及實現

倒影原理:

倒影效果是主要由原圖+間距+倒影三部分組成,高度大約爲原圖的3/2(原圖爲1、倒影爲1/2)

原圖,就是我們看到了最開始的圖片

間距,是原圖與倒影之間的間隙,如:reflectionGap = 4;

倒影,是原圖下半部分1/2高度,通過矩陣變換matrix.preScale(1, -1); 獲取倒立圖片,然後再加上線性遮罩和陰影實現


倒影實現:

  1. /** 反射倒影 */  
  2. public boolean createReflectedImages() {  
  3.     final int reflectionGap = 4;  
  4.     int index = 0;  
  5.     for (Map<String, Object> map : list) {  
  6.         Integer id = (Integer) map.get("image");  
  7.         Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), id);   // 獲取原始圖片   
  8.         int width = originalImage.getWidth();  
  9.         int height = originalImage.getHeight();  
  10.   
  11.         Matrix matrix = new Matrix();  
  12.         matrix.preScale(1, -1);         // 圖片矩陣變換(從低部向頂部的倒影)   
  13.         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);   // 截取原圖下半部分   
  14.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);          // 創建倒影圖片(高度爲原圖3/2)   
  15.   
  16.         Canvas canvas = new Canvas(bitmapWithReflection);   // 繪製倒影圖(原圖 + 間距 + 倒影)   
  17.         canvas.drawBitmap(originalImage, 00null);       // 繪製原圖   
  18.         Paint paint = new Paint();  
  19.         canvas.drawRect(0, height, width, height + reflectionGap, paint);       // 繪製原圖與倒影的間距   
  20.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);    // 繪製倒影圖   
  21.   
  22.         paint = new Paint();  
  23.         LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff0x00ffffff, TileMode.CLAMP);  
  24.         paint.setShader(shader);    // 線性漸變效果   
  25.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));     // 倒影遮罩效果   
  26.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);     // 繪製倒影的陰影效果   
  27.   
  28.         ImageView imageView = new ImageView(mContext);  
  29.         imageView.setImageBitmap(bitmapWithReflection);     // 設置倒影圖片   
  30.         imageView.setLayoutParams(new myGallery.LayoutParams(180240));  
  31.         imageView.setScaleType(ScaleType.MATRIX);  
  32.         mImages[index++] = imageView;  
  33.     }  
  34.     return true;  
  35. }  
	/** 反射倒影 */
	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來實現倒影圖片的瀏覽與選擇

  1. public class myGallery extends Gallery {  
  2.   
  3.     private Camera mCamera = new Camera();  
  4.     private int mMaxRotationAngle = 60;     // 最大旋轉角度 60   
  5.     private int mMaxZoom = -120;  
  6.     private int mCoveflowCenter;  
  7.   
  8.     public myGallery(Context context) {  
  9.         super(context);  
  10.         this.setStaticTransformationsEnabled(true);  
  11.     }  
  12.   
  13.     public myGallery(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         this.setStaticTransformationsEnabled(true);  
  16.     }  
  17.   
  18.     public myGallery(Context context, AttributeSet attrs, int defStyle) {  
  19.         super(context, attrs, defStyle);  
  20.         this.setStaticTransformationsEnabled(true);  
  21.     }  
  22.   
  23.     public int getMaxRotationAngle() {  
  24.         return mMaxRotationAngle;  
  25.     }  
  26.   
  27.     public void setMaxRotationAngle(int maxRotationAngle) {  
  28.         mMaxRotationAngle = maxRotationAngle;  
  29.     }  
  30.   
  31.     public int getMaxZoom() {  
  32.         return mMaxZoom;  
  33.     }  
  34.   
  35.     public void setMaxZoom(int maxZoom) {  
  36.         mMaxZoom = maxZoom;  
  37.     }  
  38.   
  39.     /** 獲取Gallery的中心x */  
  40.     private int getCenterOfCoverflow() {  
  41.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();  
  42.     }  
  43.   
  44.     /** 獲取View的中心x */  
  45.     private static int getCenterOfView(View view) {  
  46.         return view.getLeft() + view.getWidth() / 2;  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  51.         mCoveflowCenter = getCenterOfCoverflow();  
  52.         super.onSizeChanged(w, h, oldw, oldh);  
  53.     }  
  54.   
  55.     @Override  
  56.     protected boolean getChildStaticTransformation(View child, Transformation trans) {  
  57.         final int childCenter = getCenterOfView(child);  
  58.         final int childWidth = child.getWidth();  
  59.         int rotationAngle = 0;  
  60.   
  61.         trans.clear();  
  62.         trans.setTransformationType(Transformation.TYPE_BOTH);      // alpha 和 matrix 都變換   
  63.   
  64.         if (childCenter == mCoveflowCenter) {   // 正中間的childView   
  65.             transformImageBitmap((ImageView) child, trans, 0);    
  66.         } else {        // 兩側的childView   
  67.             rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle );  
  68.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
  69.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;  
  70.             }  
  71.             transformImageBitmap((ImageView) child, trans, rotationAngle);  
  72.         }  
  73.   
  74.         return true;  
  75.     }  
  76.   
  77.     private void transformImageBitmap(ImageView child, Transformation trans, int rotationAngle) {  
  78.         mCamera.save();  
  79.           
  80.         final Matrix imageMatrix = trans.getMatrix();  
  81.         final int imageHeight = child.getLayoutParams().height;  
  82.         final int imageWidth = child.getLayoutParams().width;  
  83.         final int rotation = Math.abs(rotationAngle);  
  84.   
  85.         // 在Z軸上正向移動camera的視角,實際效果爲放大圖片; 如果在Y軸上移動,則圖片上下移動; X軸上對應圖片左右移動。   
  86.         mCamera.translate(0.0f, 0.0f, 100.0f);  
  87.   
  88.         // As the angle of the view gets less, zoom in   
  89.         if (rotation < mMaxRotationAngle) {  
  90.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  91.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  92.         }  
  93.   
  94.         mCamera.rotateY(rotationAngle);     // rotationAngle 爲正,沿y軸向內旋轉; 爲負,沿y軸向外旋轉   
  95.           
  96.         mCamera.getMatrix(imageMatrix);  
  97.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  98.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  99.           
  100.         mCamera.restore();  
  101.     }  
  102. }  
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選擇事件監聽、點擊事件監聽

  1. private void initRes(){  
  2.     tvTitle = (TextView) findViewById(R.id.tvTitle);  
  3.     gallery = (myGallery) findViewById(R.id.mygallery);     // 獲取自定義的myGallery控件   
  4.   
  5.     adapter = new ImageAdapter(this);     
  6.     adapter.createReflectedImages();    // 創建倒影效果   
  7.     gallery.setAdapter(adapter);  
  8.       
  9.     gallery.setOnItemSelectedListener(new OnItemSelectedListener() {    // 設置選擇事件監聽   
  10.         @Override  
  11.         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  12.             tvTitle.setText(adapter.titles[position]);  
  13.         }  
  14.   
  15.         @Override  
  16.         public void onNothingSelected(AdapterView<?> parent) {  
  17.         }  
  18.     });  
  19.   
  20.     gallery.setOnItemClickListener(new OnItemClickListener() {          // 設置點擊事件監聽   
  21.         @Override  
  22.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  23.             Toast.makeText(Main.this"img " + (position+1) + " selected", Toast.LENGTH_SHORT).show();  
  24.         }  
  25.     });  
  26. }  
	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,來顯示圖片集合

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/tvTitle"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:textSize="16sp" />  
  12.       
  13.     <com.homer.reflect.myGallery  
  14.         android:id="@+id/mygallery"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_below="@id/tvTitle"  
  18.         android:layout_marginTop="10dip" />  
  19. </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>


源碼下載



參考推薦:

Android實現圖片的倒影效果

Android中圖片倒影、圓角效果重繪

發佈了18 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章