Android开发教程:倒影效果的ImagView


效果图如下:




代码如下:
  1. package lab.sodino.reflection;  
  2.   
  3. import Android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Bitmap.Config;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.LinearGradient;  
  9. import android.graphics.Matrix;  
  10. import android.graphics.Paint;  
  11. import android.graphics.PorterDuff.Mode;  
  12. import android.graphics.PorterDuffXfermode;  
  13. import android.graphics.Shader.TileMode;  
  14. import android.graphics.drawable.BitmapDrawable;  
  15. import android.util.AttributeSet;  
  16. import android.widget.ImageView;  
  17.   
  18. public class ReflectionImage extends ImageView {  
  19.     // 是否为Reflection模式   
  20.     private boolean mReflectionMode = true;  
  21.   
  22.     public ReflectionImage(Context context) {  
  23.         super(context);  
  24.     }  
  25.   
  26.     public ReflectionImage(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         measure(00);  
  29.         // 取得原始图片的bitmap并重画   
  30.         Bitmap originalImage = ((BitmapDrawable) this.getDrawable()).getBitmap();  
  31.         DoReflection(originalImage);  
  32.     }  
  33.   
  34.     public ReflectionImage(Context context, AttributeSet attrs, int defStyle) {  
  35.         super(context, attrs, defStyle);  
  36.         Bitmap originalImage = ((BitmapDrawable) this.getDrawable()).getBitmap();  
  37.         DoReflection(originalImage);  
  38.     }  
  39.   
  40.     public void setReflectionMode(boolean isRef) {  
  41.         mReflectionMode = isRef;  
  42.     }  
  43.   
  44.     public boolean getReflectionMode() {  
  45.         return mReflectionMode;  
  46.     }  
  47.   
  48.     // 偷懒了,只重写了setImageResource,和构造函数里面干了同样的事情   
  49.     @Override  
  50.     public void setImageResource(int resId) {  
  51.         Bitmap originalImage = BitmapFactory.decodeResource(getResources(), resId);  
  52.         DoReflection(originalImage);  
  53.         // super.setImageResource(resId);   
  54.     }  
  55.   
  56.     private void DoReflection(Bitmap originalImage) {  
  57.         // 原始图片和反射图片中间的间距   
  58.         final int reflectionGap = 4;  
  59.         int width = originalImage.getWidth();  
  60.         int height = originalImage.getHeight();  
  61.         // 反转   
  62.         Matrix matrix = new Matrix();  
  63.         // 第一个参数为1表示x方向上以原比例为准保持不变,正数表示方向不变。   
  64.         // 第二个参数为-1表示y方向上以原比例为准保持不变,负数表示方向取反。   
  65.         matrix.preScale(1, -0.75f);  
  66.         // reflectionImage就是下面透明的那部分,可以设置它的高度为原始的3/4,这样效果会更好些   
  67.         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 00, width, height * 3 / 4, matrix, false);  
  68.         // 创建一个新的bitmap,高度为原来的两倍   
  69.         Bitmap bitmap4Reflection = Bitmap.createBitmap(width, (height + height * 3 / 4), Config.ARGB_8888);  
  70.         // 其宽*高 = width * (height + height * 3 / 4)   
  71.         Canvas canvasRef = new Canvas(bitmap4Reflection);  
  72.         // 先画原始的图片   
  73.         canvasRef.drawBitmap(originalImage, 00null);  
  74.         // 画间距   
  75.         Paint deafaultPaint = new Paint();  
  76.         // defaultPaint不能为null,否则会有空指针异常。   
  77.         canvasRef.drawRect(0, height, width, height + reflectionGap, deafaultPaint);  
  78.         // 画被反转以后的图片   
  79.         canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  80.         // 创建一个渐变的蒙版放在下面被反转的图片上面   
  81.         Paint paint = new Paint();  
  82.         LinearGradient shader = new LinearGradient(200, originalImage.getHeight(), 10, bitmap4Reflection.getHeight()  
  83.                 + reflectionGap, 0x80ffffff0x00ffffff, TileMode.CLAMP);  
  84.         paint.setShader(shader);  
  85.         // Set the Transfer mode to be porter duff and destination in   
  86.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  87.         // 将蒙板画上   
  88.         canvasRef.drawRect(0, height, width, bitmap4Reflection.getHeight() + reflectionGap, paint);  
  89.         // 调用ImageView中的setImageBitmap   
  90.         this.setImageBitmap(bitmap4Reflection);  
  91.     }  
  92. }  

相关内容