Learn how to add watermarks to images in android (33 ),

Source: Internet
Author: User

Learn how to add watermarks to images in android (33 ),

Source image:



:





The reference code is as follows:

public class MainActivity extends Activity {private Button btn_start;private ImageView img;private Bitmap bitmap;private Bitmap wbitmap;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.image_layout);initView();}private void initView() {img = (ImageView) findViewById(R.id.iv_image);bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();wbitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.img_label_user)).getBitmap();btn_start = (Button) findViewById(R.id.btn_start);btn_start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Bitmap bm = ImageTools.Watermark(bitmap,wbitmap,100);img.setImageBitmap(bm);}});}}

ImageTools tool class:

/*** Watermark ** @ param src: Watermark image * @ param watermark Watermark image * @ param alpha transparency of the watermark * @ return */public static Bitmap Watermark (Bitmap src, bitmap watermark, int alpha) {if (src = null) {return null;} int w = src. getWidth (); int h = src. getHeight (); int ww = watermark. getWidth (); int wh = watermark. getHeight (); Paint paint = new Paint (); paint. setAlpha (alpha); paint. setAntiAlias (true); Bitmap newb = Bitmap. createBitmap (w, h, Config. ARGB_8888); Canvas cv = new Canvas (newb); cv. drawBitmap (src, 0, 0, null); cv. drawBitmap (watermark, 0, h/2, paint); cv. save (Canvas. ALL_SAVE_FLAG); cv. restore (); return newb ;}




Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/45220425 sentiment control _

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.