当前位置: 代码迷 >> Android >> 一行学android之给图片添加水印效果(33)
  详细解决方案

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

热度:68   发布时间:2016-04-28 01:19:55.0
一起学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 情绪控_

  相关解决方案