Android watermarks Images
The watermark is actually very simple. It is to draw another image or text on an image.
The implementation is as follows:
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. onClick Listener; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity {ImageView imageView; Bitmap mark; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. imageView1); imageView. setImageResource (R. drawable. heihei); mark = BitmapFacto Ry. decodeResource (getResources (), R. drawable. ic_launcher); Button button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubBitmap temp = watermark (drawableToBitmap (imageView. getDrawable (), mark, 50,100); imageView. setImageBitmap (temp) ;}}) ;}/ *** converts 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 the cv. restore (); // store return newb;}/*** Drawable into Bitmap * @ param drawable * @ return */public static Bitmap drawableToBitmap (Drawable drawable) {// obtain the length and width of drawable int w = drawable. getIntrinsicWidth (); int h = drawable. getIntrinsicHeight (); // retrieves the 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 ;}}