/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
private Bitmap getReflection(Bitmap bitmap) { Bitmap reflection = reflections.get(bitmap); if (reflection == null) { // We're cropping the height of the reflection to 80 int reflectionH = 80; reflection = Bitmap.createBitmap(bitmap.getWidth(), reflectionH, Bitmap.Config.ARGB_8888); Bitmap blurryBitmap = Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() - reflectionH, bitmap.getWidth(), reflectionH); // cheap and easy scaling algorithm; down-scale it, then // upscale it. The filtering during the scale operations // will blur the resulting image blurryBitmap = Bitmap.createScaledBitmap( Bitmap.createScaledBitmap( blurryBitmap,blurryBitmap.getWidth() / 2, blurryBitmap.getHeight() / 2, true), blurryBitmap.getWidth(), blurryBitmap.getHeight(), true); // This shader will hold a cropped, inverted, // blurry version of the original image BitmapShader bitmapShader = new BitmapShader(blurryBitmap, TileMode.CLAMP, TileMode.CLAMP); Matrix invertMatrix = new Matrix(); invertMatrix.setScale(1f, -1f); invertMatrix.preTranslate(0, -reflectionH); bitmapShader.setLocalMatrix(invertMatrix); // This shader holds an alpha gradient Shader alphaGradient = new LinearGradient(0, 0, 0, reflectionH, 0x80ffffff, 0x00000000, TileMode.CLAMP); // This shader combines the previous two, resulting in a // blurred, fading reflection ComposeShader compositor = new ComposeShader(bitmapShader, alphaGradient, PorterDuff.Mode.DST_IN); reflectionPaint.setShader(compositor); // Draw the reflection into the bitmap that we will return canvas.drawRect(0, 0, reflection.getWidth(), reflection.getHeight(), reflectionPaint); } return reflection;
URL: http://graphics-geek.blogspot.com/2011/01/video-reflections-on-android.html