Android截图以及加水印Demo


实现一个简单的截图功能以及给图片添加水印的功能,直接上代码!

Android截图以及加水印Demo下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /pub/Android源码集锦/2011年/10月/Android截图以及加水印Demo/

一、代码实现

  1. import android.app.Activity;  
  2. import android.graphics.Bitmap;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.graphics.Typeface;  
  7. import android.graphics.Bitmap.Config;  
  8. import android.os.Bundle;  
  9. import android.text.format.Time;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14.   
  15. public class GetAppThumbnailActivity extends Activity {  
  16.     private Button btnThum;  
  17.     private ImageView imgThum;  
  18.     private ImageView imgSource;  
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         setupViews();  
  25.     }  
  26.   
  27.     private void setupViews() {  
  28.         btnThum = (Button) findViewById(R.id.getThum);  
  29.         imgThum = (ImageView) findViewById(R.id.setThum);  
  30.         imgSource = (ImageView) findViewById(R.id.source);  
  31.   
  32.         btnThum.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 Bitmap bitmap = getViewBitmap(imgSource);  
  37.                 Bitmap bitmap1 = createBitmap(bitmap, "haha哈哈");  
  38.                 if (bitmap1 != null) {  
  39.                     imgThum.setImageBitmap(bitmap1);  
  40.                 }  
  41.             }  
  42.         });  
  43.     }  
  44.   
  45.     /** 
  46.      * Draw the view into a bitmap. 
  47.      */  
  48.     private Bitmap getViewBitmap(View v) {  
  49.         v.clearFocus();  
  50.         v.setPressed(false);  
  51.   
  52.         boolean willNotCache = v.willNotCacheDrawing();  
  53.         v.setWillNotCacheDrawing(false);  
  54.   
  55.         // Reset the drawing cache background color to fully transparent   
  56.         // for the duration of this operation   
  57.         int color = v.getDrawingCacheBackgroundColor();  
  58.         v.setDrawingCacheBackgroundColor(0);  
  59.   
  60.         if (color != 0) {  
  61.             v.destroyDrawingCache();  
  62.         }  
  63.         v.buildDrawingCache();  
  64.         Bitmap cacheBitmap = v.getDrawingCache();  
  65.         if (cacheBitmap == null) {  
  66.             return null;  
  67.         }  
  68.   
  69.         Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  
  70.   
  71.         // Restore the view   
  72.         v.destroyDrawingCache();  
  73.         v.setWillNotCacheDrawing(willNotCache);  
  74.         v.setDrawingCacheBackgroundColor(color);  
  75.   
  76.         return bitmap;  
  77.     }  
  78.   
  79.     // 给图片添加水印   
  80.     private Bitmap createBitmap(Bitmap src, String str) {  
  81.         Time t = new Time();  
  82.         t.setToNow();   
  83.         int w = src.getWidth();  
  84.         int h = src.getHeight();  
  85.         String mstrTitle = "截图时间:"+t.hour + ":" + t.minute + ":" + t.second;  
  86.         Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
  87.         Canvas canvas = new Canvas(bmpTemp);  
  88.         Paint p = new Paint();  
  89.         String familyName = "宋体";  
  90.         Typeface font = Typeface.create(familyName, Typeface.BOLD);  
  91.         p.setColor(Color.BLUE);  
  92.         p.setTypeface(font);  
  93.         p.setTextSize(22);  
  94.         canvas.drawBitmap(src, 00, p);  
  95.         canvas.drawText(mstrTitle, 020, p);  
  96.         canvas.save(Canvas.ALL_SAVE_FLAG);  
  97.         canvas.restore();  
  98.         return bmpTemp;  
  99.     }  
  100.   
  101. }
 
  1. import android.app.Activity;  
  2. import android.graphics.Bitmap;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.graphics.Typeface;  
  7. import android.graphics.Bitmap.Config;  
  8. import android.os.Bundle;  
  9. import android.text.format.Time;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14.   
  15. public class GetAppThumbnailActivity extends Activity {  
  16.     private Button btnThum;  
  17.     private ImageView imgThum;  
  18.     private ImageView imgSource;  
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         setupViews();  
  25.     }  
  26.   
  27.     private void setupViews() {  
  28.         btnThum = (Button) findViewById(R.id.getThum);  
  29.         imgThum = (ImageView) findViewById(R.id.setThum);  
  30.         imgSource = (ImageView) findViewById(R.id.source);  
  31.   
  32.         btnThum.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 Bitmap bitmap = getViewBitmap(imgSource);  
  37.                 Bitmap bitmap1 = createBitmap(bitmap, "haha哈哈");  
  38.                 if (bitmap1 != null) {  
  39.                     imgThum.setImageBitmap(bitmap1);  
  40.                 }  
  41.             }  
  42.         });  
  43.     }  
  44.   
  45.     /** 
  46.      * Draw the view into a bitmap. 
  47.      */  
  48.     private Bitmap getViewBitmap(View v) {  
  49.         v.clearFocus();  
  50.         v.setPressed(false);  
  51.   
  52.         boolean willNotCache = v.willNotCacheDrawing();  
  53.         v.setWillNotCacheDrawing(false);  
  54.   
  55.         // Reset the drawing cache background color to fully transparent   
  56.         // for the duration of this operation   
  57.         int color = v.getDrawingCacheBackgroundColor();  
  58.         v.setDrawingCacheBackgroundColor(0);  
  59.   
  60.         if (color != 0) {  
  61.             v.destroyDrawingCache();  
  62.         }  
  63.         v.buildDrawingCache();  
  64.         Bitmap cacheBitmap = v.getDrawingCache();  
  65.         if (cacheBitmap == null) {  
  66.             return null;  
  67.         }  
  68.   
  69.         Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  
  70.   
  71.         // Restore the view   
  72.         v.destroyDrawingCache();  
  73.         v.setWillNotCacheDrawing(willNotCache);  
  74.         v.setDrawingCacheBackgroundColor(color);  
  75.   
  76.         return bitmap;  
  77.     }  
  78.   
  79.     // 给图片添加水印   
  80.     private Bitmap createBitmap(Bitmap src, String str) {  
  81.         Time t = new Time();  
  82.         t.setToNow();   
  83.         int w = src.getWidth();  
  84.         int h = src.getHeight();  
  85.         String mstrTitle = "截图时间:"+t.hour + ":" + t.minute + ":" + t.second;  
  86.         Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
  87.         Canvas canvas = new Canvas(bmpTemp);  
  88.         Paint p = new Paint();  
  89.         String familyName = "宋体";  
  90.         Typeface font = Typeface.create(familyName, Typeface.BOLD);  
  91.         p.setColor(Color.BLUE);  
  92.         p.setTypeface(font);  
  93.         p.setTextSize(22);  
  94.         canvas.drawBitmap(src, 00, p);  
  95.         canvas.drawText(mstrTitle, 020, p);  
  96.         canvas.save(Canvas.ALL_SAVE_FLAG);  
  97.         canvas.restore();  
  98.         return bmpTemp;  
  99.     }  
  100.   
  101. }
  • 1
  • 2
  • 下一页

相关内容