码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-04-23 15:53:19      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:android   imageview   图片   水印   图片特效   

原图:

技术分享


效果图:

技术分享




参考代码如下:

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

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

标签:android   imageview   图片   水印   图片特效   

原文地址:http://blog.csdn.net/hai_qing_xu_kong/article/details/45220425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!