Speaking of the watermark is actually very simple, is to draw on a picture of another picture or text
The implementation is as follows:
[Java]View Plaincopy
- Package Com.tang.watermark;
- Import android.app.Activity;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.graphics.Canvas;
- Import Android.graphics.PixelFormat;
- Import Android.graphics.Bitmap.Config;
- Import android.graphics.drawable.BitmapDrawable;
- Import android.graphics.drawable.Drawable;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.ImageView;
- Public class Mainactivity extends Activity {
- ImageView ImageView;
- Bitmap Mark;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
- Imageview.setimageresource (R.drawable.heihei);
- Mark = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
- Button button = (button) Findviewbyid (R.id.button1);
- Button.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- //TODO auto-generated method stub
- Bitmap temp = watermark (Drawabletobitmap (imageview.getdrawable ()), Mark,100);
- Imageview.setimagebitmap (temp);
- }
- });
- }
- /**
- * Convert Bitmap to Drawable
- * @param bitmap
- * @return
- */
- public static drawable bitmaptodrawable (Bitmap Bitmap)
- {
- bitmapdrawable bd = new bitmapdrawable (bitmap);
- return BD;
- }
- /**
- * @param src
- * @param BG
- * @param color
- * @return
- */
- public static Bitmap watermark (Bitmap src,bitmap mark,int x,int y)
- {
- int w = src.getwidth ();
- int h = src.getheight ();
- Bitmap newb = Bitmap.createbitmap (W, H, config.argb_8888);
- Canvas CV = new canvas (NEWB);
- Cv.drawbitmap (SRC, 0, 0, null);
- Cv.drawbitmap (mark,x, y, null);
- Cv.save (Canvas.all_save_flag); //Save
- Cv.restore (); //Storage
- return newb;
- }
- /**
- * Drawable converted to bitmap
- * @param drawable
- * @return
- */
- public static Bitmap Drawabletobitmap (drawable drawable) {
- //Take drawable of the length of the width
- int w = drawable.getintrinsicwidth ();
- int h = drawable.getintrinsicheight ();
- //Take drawable color format
- Bitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565;
- Bitmap Bitmap = Bitmap.createbitmap (w, H, config);
- Canvas canvas = new canvas (bitmap);
- Drawable.setbounds (0, 0, W, h);
- Drawable.draw (canvas);
- return bitmap;
- }
Android Watermark a picture