Skip to main content

Video: Reflections on Android

Everyone's supposed to make a resolution on New Years, so here's mine: post more articles/blogs/videos about Android development. Starting with today's video tutorial.


Here's the first in what I hope will be a series of video tutorials on anything ranging from graphics to animation and back to graphics (hey, it's my show and I might as well talk about the things I enjoy).



For the Devoxx conference last November, I developed a simple picture-viewing application to demonstrate various facets of UI development that Romain Guy and I were talking about that week. You can see the presentations online at parleys.com (you'll have to register for 79 Euros to get access to them for now). But I wanted to deep dive into particular aspects of this application for my blog. Here's the first of these tutorials, in which I talk about the simple reflection effect used in the application. By the way, credit for the beautiful pictures goes to Romain, my source for all of my, er, borrowed images.



The video is in two parts (because YouTube thinks that I talk too much, so I had to split it). This first part introduces the show and talks about the effect at a high level:





Part 2 dives into the code that makes the reflection effect work:





The code in the video is a tad blurry (given the resolution of the video compared to the size of the IDE window), so here's the code from PictureView.java that I walk through for your reading pleasure (note that this code looks slightly different than that in the video due to formatting for a smaller line wrap. Also, the blurryBitmap image is now created to be only as high as the reflection height, as described in the comments):



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);

Paint reflectionPaint = new Paint();
reflectionPaint.setShader(compositor);

// Draw the reflection into the bitmap that we will return
Canvas canvas = new Canvas(reflection);
canvas.drawRect(0, 0, reflection.getWidth(),
reflection.getHeight(), reflectionPaint);
}
return reflection;
}

And finally, here's the Eclipse project complete with source code, images, and everything you need to build and run the application. The app is targeted at Android 2.2 (Froyo) (and probably could work on earlier versions as well), so you should be able to run it on the emulator or any appropriate device. Note that it's just a demo and not really a full-featured photo viewer; it was written to demonstrate particular effects and techniques, not to be a real application.



Now that my New Year's resolution is fulfilled, I should go back to working on Android framework code...

Comments

Popular posts from this blog

Women and children overboard

It's the  Catch-22  of clinical trials: to protect pregnant women and children from the risks of untested drugs....we don't test drugs adequately for them. In the last few decades , we've been more concerned about the harms of research than of inadequately tested treatments for everyone, in fact. But for "vulnerable populations,"  like pregnant women and children, the default was to exclude them. And just in case any women might be, or might become, pregnant, it was often easier just to exclude us all from trials. It got so bad, that by the late 1990s, the FDA realized regulations and more for pregnant women - and women generally - had to change. The NIH (National Institutes of Health) took action too. And so few drugs had enough safety and efficacy information for children that, even in official circles, children were being called "therapeutic orphans."  Action began on that, too. There is still a long way to go. But this month there was a sign that ...

Benefits Of Healthy eating Turmeric every day for the body

One teaspoon of turmeric a day to prevent inflammation, accumulation of toxins, pain, and the outbreak of cancer.  Yes, turmeric has been known since 2.5 centuries ago in India, as a plant anti-inflammatory / inflammatory, anti-bacterial, and also have a good detox properties, now proven to prevent Alzheimer's disease and cancer. Turmeric prevents inflammation:  For people who

Austerity-A Fancy Word for Destitute.

The reason for this post is not for the folks who have been caught in the first wave of personal economic hard reality, but the next wave. Regardless of the optimism espoused by grinning leaders and sycophant press, we are entering the final stage of global economic collapse. It began in 2008 and was forestalled for five years with fudge putty, but the weight of global indebtedness cannot be propped any longer and the final crunch is imminent. Austerity measures herald the final throes.  Indications of coming austerity.   Austerity measures are the final last ditch effort, futile or not! Back in the day many of us old-timers went through periods of "hard-times". In retrospect I realize there is no comparison to yesteryear hard times and today's version. Back then, expectations were never very high for the working class, there were no sophisticated systems or conveniences anyway. In fact the difference between being "set" or not was about having treats or not. Si...