Today received the task, to do watermark function, because rookie one, on the internet for a long time, most of the demon are watermark photos or text is too vague, but also packaged not so good, combined with a few demon, finally on the basis of their own packaging a class, including the watermark and string functions, as follows:
Say less nonsense, the code presents:
Set up a ImageView control in XML:
<imageview
Android:id= "@+id/imageview"
Android:layout_width= "300DP"
android:layout_height= "300DP"
Android:layout_centervertical= "true"
Android:layout_centerhorizontal= "true"/>
The form code class is as follows:
private void Initview () {
Get a picture of a local resource (here you can get bitmap picture objects from a camera or album)
Bitmap src = bitmapfactory.decoderesource (getresources (), R.DRAWABLE.YT);
int w = src.getwidth ();//Get the width of the picture
int h = src.getheight ();//Get the picture high
Bitmap of compressed pictures
Float scale = (float) (1000000.0/(W * h));
Matrix matrix = new Matrix ();
Matrix.setscale (scale, scale);
Bitmap src1 = bitmap.createbitmap (src, 0, 0, src.getwidth (), Src.getheight (), Matrix, True);
Release the original bitmap
Src.recycle ();
Bitmap water = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.LG);
Add watermark
Src1 = Watermark.createwaterbitmap (Src1, Water, "2016-5-20 13:14:00");
Src1 = Watermark.watermarkbitmap (Src1,water, "2016-5-19");
Show
ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
Imageview.setimagebitmap (SRC1);
}
The following are the packaged method classes:
/**
* @author Feiniu
* Created by Administrator on 2016/5/19 0019.
*/
public class WaterMark {
/**
* Watermark logo and Title
*/
public static Bitmap Createwaterbitmap (Bitmap src, Bitmap watermark, String title) {
Bitmap newb = null;//Create a bitmap to save the watermark
if (src = = null) {
return null;
}
The width of the original picture
int w = src.getwidth ();
int h = src.getheight ();//high of the original picture
int ww = watermark.getwidth ();//width of watermark image
int wh = Watermark.getheight ();//High watermark image
LOG.V ("wz", W + "," + H + "," + ww + "," + WH ");//view bitmap size in log file
Newb = Bitmap.createbitmap (W, H, Bitmap.Config.ARGB_8888);//Create a new bitmap of the same size as Src
Canvas CV = new canvas (NEWB);//Create a canvas of equal size
Cv.drawbitmap (SRC, 0, 0, NULL);//Start by drawing src into the canvas from the coordinates 0,0
Set the size of the watermark logo
Watermark = Bitmap.createscaledbitmap (watermark, N, I, true);
Paint Textpaint = new paint (Paint.anti_alias_flag);
Typeface font = typeface.create (Typeface.default, typeface.bold);
Draw watermark in Src (lower right corner)
Cv.drawbitmap (Watermark, ten, ten, null);
Add text
if (title! = null) {
float BaseX = w-350;
float newy = h-30;
Textpaint.settextsize (35);
Textpaint.setcolor (color.red);
Textpaint.setcolor (Color.rgb (255, 127, 0));
Textpaint.settypeface (font);
Cv.drawtext (title, BaseX, Newy, Textpaint);
}
Cv.save ();//Save
Cv.restore ();//Storage
Return newb;//returns a bitmap with a watermark
}
}
At this point, you can see the effect of running the project.
Android to achieve image watermark function