Monday 26 August 2013

Before Water Mark

Water Mark the Image in ImageView

After Water Mark

Water Mark the Image in ImageView

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" >

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

</RelativeLayout>

 2. code of main activity:

package com.shaikhhamadali.blogspot.playingwithbitmaps;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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);
  Point p=new Point();
  p.set(180, 1000);
  Bitmap b=waterMark(BitmapFactory.decodeResource(getResources(), R.drawable.car),"Welcome To Hamad's Blog",p,Color.WHITE,90,30,true);
  imViewAndroid.setImageBitmap(b);

 }
 public  Bitmap waterMark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline) {
  //get source image width and height
  int w = src.getWidth();
  int h = src.getHeight();

  Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
  //create canvas object
  Canvas canvas = new Canvas(result);
  //draw bitmap on canvas
  canvas.drawBitmap(src, 0, 0, null);
  //create paint object
  Paint paint = new Paint();
  //apply color
  paint.setColor(color);
  //set transparency
  paint.setAlpha(alpha);
  //set text size
  paint.setTextSize(size);
  paint.setAntiAlias(true);
  //set should be underlined or not
  paint.setUnderlineText(underline);
  //draw text on given location
  canvas.drawText(watermark, location.x, location.y, paint);

  return result;
 }
}

3. note that:
  • With the help of this,waterMark() method you can Water mark image on image on click,on action_down,on the fly etc.
  4. conclusion:
  • Some information about Paint and WaterMark.
  • Know how to flip 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