Android watermarks Images

Source: Internet
Author: User

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 ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.