Wednesday, 26 June 2013

Before highlight effect

    After highlight effect


Create new Android Project
Project Name: PlayingwithBitmaps
Build Target: Android 2.3.3   //or greater than that
Application Name: PlayingwithBitmaps
Package Name: com.hamad.playingwithbitmaps
Create Activity: Main
Min SDK: 10 // or greater than that

  1. create main layout:
  • One image view to display the image.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:id="@+id/rlMain" >

    <ImageView
        android:id="@+id/imViewAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/beautiful" />

</RelativeLayout>

    2. code of main activity:

package com.shaikhhamadali.blogspot.playingwithbitmaps;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.PorterDuff;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.widget.ImageView;

public class Main extends Activity {
 ImageView imViewAndroid;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
  imViewAndroid.setImageBitmap(highlightImage(BitmapFactory.decodeResource(getResources(), R.drawable.android_droid)));
 }
 public Bitmap highlightImage(Bitmap src) {
  // create new bitmap, which will be painted and becomes result image
  Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888);
  // setup canvas for painting
  Canvas canvas = new Canvas(bmOut);
  // setup default color
  canvas.drawColor(0, PorterDuff.Mode.CLEAR);
  // create a blur paint for capturing alpha
  Paint ptBlur = new Paint();
  ptBlur.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
  int[] offsetXY = new int[2];
  // capture alpha into a bitmap
  Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
  // create a color paint
  Paint ptAlphaColor = new Paint();
  ptAlphaColor.setColor(0xFFFFFFFF);
  // paint color for captured alpha region (bitmap)
  canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
  // free memory
  bmAlpha.recycle();

  // paint the image source
  canvas.drawBitmap(src, 0, 0, null);

  // return out final image
  return bmOut;
 }
}

  3. note that:

  • with the help of this,highlightImage() method you can highlight image on click,on action_down etc
  4. conclusion:

  • Know how to highlight an image bitmap from drawables.
  5. about the post:

  • The code seems to explain itself due to comments, and is very easy to understand.
  • Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  • Hope you enjoy it!

  6. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh