Posted By

arpit on 06/12/11


Tagged

Reflection android


Versions (?)

Creating a reflection effect in Android


 / Published in: Java
 

URL: http://graphics-geek.blogspot.com/2011/01/video-reflections-on-android.html

  1. private Bitmap getReflection(Bitmap bitmap) {
  2. Bitmap reflection = reflections.get(bitmap);
  3. if (reflection == null) {
  4. // We're cropping the height of the reflection to 80
  5. int reflectionH = 80;
  6. reflection = Bitmap.createBitmap(bitmap.getWidth(),
  7. reflectionH, Bitmap.Config.ARGB_8888);
  8.  
  9. Bitmap blurryBitmap = Bitmap.createBitmap(bitmap, 0,
  10. bitmap.getHeight() - reflectionH,
  11. bitmap.getWidth(), reflectionH);
  12. // cheap and easy scaling algorithm; down-scale it, then
  13. // upscale it. The filtering during the scale operations
  14. // will blur the resulting image
  15. blurryBitmap = Bitmap.createScaledBitmap(
  16. Bitmap.createScaledBitmap(
  17. blurryBitmap,blurryBitmap.getWidth() / 2,
  18. blurryBitmap.getHeight() / 2, true),
  19. blurryBitmap.getWidth(), blurryBitmap.getHeight(), true);
  20. // This shader will hold a cropped, inverted,
  21. // blurry version of the original image
  22. BitmapShader bitmapShader = new BitmapShader(blurryBitmap,
  23. TileMode.CLAMP, TileMode.CLAMP);
  24. Matrix invertMatrix = new Matrix();
  25. invertMatrix.setScale(1f, -1f);
  26. invertMatrix.preTranslate(0, -reflectionH);
  27. bitmapShader.setLocalMatrix(invertMatrix);
  28.  
  29. // This shader holds an alpha gradient
  30. Shader alphaGradient = new LinearGradient(0, 0, 0, reflectionH,
  31. 0x80ffffff, 0x00000000, TileMode.CLAMP);
  32.  
  33. // This shader combines the previous two, resulting in a
  34. // blurred, fading reflection
  35. ComposeShader compositor = new ComposeShader(bitmapShader,
  36. alphaGradient, PorterDuff.Mode.DST_IN);
  37.  
  38. Paint reflectionPaint = new Paint();
  39. reflectionPaint.setShader(compositor);
  40.  
  41. // Draw the reflection into the bitmap that we will return
  42. Canvas canvas = new Canvas(reflection);
  43. canvas.drawRect(0, 0, reflection.getWidth(),
  44. reflection.getHeight(), reflectionPaint);
  45. }
  46. return reflection;

Report this snippet  

You need to login to post a comment.