We can add a watermark to the picture when we tweet or Csdn blog, a separate logo, how is it implemented? First encapsulate a Bitmaptools package class, the problem that the class solves is one will break through the storage into the SDcard, two give the picture watermark.
Bitmaptools
Package Com.example.g11_bitmap04;import Java.io.file;import Java.io.fileoutputstream;import java.io.OutputStream; Import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.os.environment;public class BitmapTools { Public Bitmaptools () {//TODO auto-generated constructor stub}public static Bitmap CreateBitmap (Resources resources, int R Esid,string name) {Bitmap Bitmap=bitmapfactory.decoderesource (resources, RESID);//Copy a new Bitmap, Because the watermark operation cannot be directly on the original Bitmap//bitmap.config the stored format Bitmap newbitmap=bitmap.copy (Bitmap.Config.ARGB_8888, true);// Canvas canvas=new Canvas (newbitmap) using the custom canvas; Paint paint=new Paint ();p aint.settextsize (), Canvas.drawtext ("Hello", [+], paint);//Determine if SDcard is in the available state if ( Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//Save the picture directly in the root directory file root= Environment.getexternalstoragedirectory (); OutputStream Outputstream=null;try {Outputstream=neW FileOutputStream (Root, name),//compress the picture and save it in PNG format in SDcard newbitmap.compress (Bitmap.CompressFormat.PNG, OutputStream);} catch (Exception e) {//Todo:handle Exception}}return Newbitmap;}}
This method is called in mainactivtiy
Package Com.example.g11_bitmap04;import Android.os.bundle;import Android.app.activity;import Android.graphics.bitmap;import Android.view.menu;import Android.view.view;import Android.widget.Button;import Android.widget.imageview;public class Mainactivity extends Activity {private ImageView ImageView; Private button button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (R.layout.activity_main); imageview= (ImageView) This.findviewbyid (r.id.imageview1); Button= ( Button) This.findviewbyid (R.id.button1), Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubbitmap Bitmap=bitmaptools.createbitmap (Getresources (), R.drawable.a , "A.png"); Imageview.setimagebitmap (bitmap);}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}