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

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

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

Not a word was spoken (but many were learned)

Video is often used in the EFL classroom for listening comprehension activities, facilitating discussions and, of course, language work. But how can you exploit silent films without any language in them? Since developing learners' linguistic resources should be our primary goal (well, at least the blogger behind the blog thinks so), here are four suggestions on how language (grammar and vocabulary) can be generated from silent clips. Split-viewing Split-viewing is an information gap activity where the class is split into groups with one group facing the screen and the other with their back to the screen. The ones facing the screen than report on what they have seen - this can be done WHILE as well as AFTER they watch. Alternatively, students who are not watching (the ones sitting with their backs to the screen) can be send out of the classroom and come up with a list of the questions to ask the 'watching group'. This works particularly well with action or crime scenes with