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

android 为图片打上水印

时间:2014-09-22 12:33:12      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:水印   android   bitmap   imageview   canvas   

说起水印其实很简单,就是在一张图片上面画上另一张图片或文字

实现如下:

package com.tang.watermark;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	ImageView imageView;
	Bitmap mark;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView = (ImageView) findViewById(R.id.imageView1); 
		imageView.setImageResource(R.drawable.heihei);
		mark = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		
		
		Button button = (Button) findViewById(R.id.button1);
		
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bitmap temp = watermark(drawableToBitmap(imageView.getDrawable()),mark,50,100);
				imageView.setImageBitmap(temp);
			}
		});
	}
    /**
     * 将bitmap转换为Drawable
     * @param bitmap
     * @return
     */
    public static Drawable bitmapToDrawable(Bitmap bitmap)
    {
    	BitmapDrawable bd = new BitmapDrawable(bitmap);
    	return bd;
    }
    
    /**
     * @param src
     * @param bg
     * @param color
     * @return
     */
    public static Bitmap watermark(Bitmap src,Bitmap mark,int x,int y)
	{
		int w = src.getWidth();  
	    int h = src.getHeight();  
	    Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
	    Canvas cv = new Canvas(newb);   
	    cv.drawBitmap(src, 0, 0, null ); 
	    cv.drawBitmap(mark,x, y, null );
	    cv.save( Canvas.ALL_SAVE_FLAG );//保存  
	    cv.restore();//存储  
	    return newb;
	}
    
	
    /**
     * Drawable 转换为Bitmap
     * @param drawable
     * @return
     */
	public static Bitmap drawableToBitmap(Drawable drawable) {  
        // 取 drawable 的长宽  
        int w = drawable.getIntrinsicWidth();  
        int h = drawable.getIntrinsicHeight();   
        // 取 drawable 的颜色格式  
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                : Bitmap.Config.RGB_565;  
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);   
        Canvas canvas = new Canvas(bitmap);  
        drawable.setBounds(0, 0, w, h);  
        drawable.draw(canvas);  
        return bitmap;  
    }  

}


android 为图片打上水印

标签:水印   android   bitmap   imageview   canvas   

原文地址:http://blog.csdn.net/tangnengwu/article/details/39472853

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