一起学android之给图片添加水印效果(33)

原图:

技术分享


效果图:

技术分享




参考代码如下:

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 情绪控_

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。