Android之给图片添加水印效果

原图:

 

效果图:

 

 

 

参考代码如下:

 

public class MainActivity extends Activity {
	private Button btn_start;
	private ImageView img;
	private Bitmap bitmap;
	private Bitmap wbitmap;


	@Override
	protected 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() {

			@Override
			public void onClick(View v) {
				Bitmap bm = ImageTools.Watermark(bitmap,wbitmap,100);
				img.setImageBitmap(bm);
			}
		});

	}
}


ImageTools工具类:

 

 

/**
	 * 水印
	 * @param src 添加水印的图
	 * @param watermark 水印图
	 * @param alpha 水印的透明度
	 * @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;
	}

 

 

 

 

 

 

 

转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/45220425 情绪控_

©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:上身试试 返回首页