歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android截圖以及加水印Demo

Android截圖以及加水印Demo

日期:2017/3/1 11:09:14   编辑:Linux編程

實現一個簡單的截圖功能以及給圖片添加水印的功能,直接上代碼!

Android截圖以及加水印Demo下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.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. public class GetAppThumbnailActivity extends Activity {
  15. private Button btnThum;
  16. private ImageView imgThum;
  17. private ImageView imgSource;
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. setupViews();
  23. }
  24. private void setupViews() {
  25. btnThum = (Button) findViewById(R.id.getThum);
  26. imgThum = (ImageView) findViewById(R.id.setThum);
  27. imgSource = (ImageView) findViewById(R.id.source);
  28. btnThum.setOnClickListener(new OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. Bitmap bitmap = getViewBitmap(imgSource);
  32. Bitmap bitmap1 = createBitmap(bitmap, "haha哈哈");
  33. if (bitmap1 != null) {
  34. imgThum.setImageBitmap(bitmap1);
  35. }
  36. }
  37. });
  38. }
  39. /**
  40. * Draw the view into a bitmap.
  41. */
  42. private Bitmap getViewBitmap(View v) {
  43. v.clearFocus();
  44. v.setPressed(false);
  45. boolean willNotCache = v.willNotCacheDrawing();
  46. v.setWillNotCacheDrawing(false);
  47. // Reset the drawing cache background color to fully transparent
  48. // for the duration of this operation
  49. int color = v.getDrawingCacheBackgroundColor();
  50. v.setDrawingCacheBackgroundColor(0);
  51. if (color != 0) {
  52. v.destroyDrawingCache();
  53. }
  54. v.buildDrawingCache();
  55. Bitmap cacheBitmap = v.getDrawingCache();
  56. if (cacheBitmap == null) {
  57. return null;
  58. }
  59. Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
  60. // Restore the view
  61. v.destroyDrawingCache();
  62. v.setWillNotCacheDrawing(willNotCache);
  63. v.setDrawingCacheBackgroundColor(color);
  64. return bitmap;
  65. }
  66. // 給圖片添加水印
  67. private Bitmap createBitmap(Bitmap src, String str) {
  68. Time t = new Time();
  69. t.setToNow();
  70. int w = src.getWidth();
  71. int h = src.getHeight();
  72. String mstrTitle = "截圖時間:"+t.hour + ":" + t.minute + ":" + t.second;
  73. Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
  74. Canvas canvas = new Canvas(bmpTemp);
  75. Paint p = new Paint();
  76. String familyName = "宋體";
  77. Typeface font = Typeface.create(familyName, Typeface.BOLD);
  78. p.setColor(Color.BLUE);
  79. p.setTypeface(font);
  80. p.setTextSize(22);
  81. canvas.drawBitmap(src, 0, 0, p);
  82. canvas.drawText(mstrTitle, 0, 20, p);
  83. canvas.save(Canvas.ALL_SAVE_FLAG);
  84. canvas.restore();
  85. return bmpTemp;
  86. }
  87. }
  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. public class GetAppThumbnailActivity extends Activity {
  15. private Button btnThum;
  16. private ImageView imgThum;
  17. private ImageView imgSource;
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. setupViews();
  23. }
  24. private void setupViews() {
  25. btnThum = (Button) findViewById(R.id.getThum);
  26. imgThum = (ImageView) findViewById(R.id.setThum);
  27. imgSource = (ImageView) findViewById(R.id.source);
  28. btnThum.setOnClickListener(new OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. Bitmap bitmap = getViewBitmap(imgSource);
  32. Bitmap bitmap1 = createBitmap(bitmap, "haha哈哈");
  33. if (bitmap1 != null) {
  34. imgThum.setImageBitmap(bitmap1);
  35. }
  36. }
  37. });
  38. }
  39. /**
  40. * Draw the view into a bitmap.
  41. */
  42. private Bitmap getViewBitmap(View v) {
  43. v.clearFocus();
  44. v.setPressed(false);
  45. boolean willNotCache = v.willNotCacheDrawing();
  46. v.setWillNotCacheDrawing(false);
  47. // Reset the drawing cache background color to fully transparent
  48. // for the duration of this operation
  49. int color = v.getDrawingCacheBackgroundColor();
  50. v.setDrawingCacheBackgroundColor(0);
  51. if (color != 0) {
  52. v.destroyDrawingCache();
  53. }
  54. v.buildDrawingCache();
  55. Bitmap cacheBitmap = v.getDrawingCache();
  56. if (cacheBitmap == null) {
  57. return null;
  58. }
  59. Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
  60. // Restore the view
  61. v.destroyDrawingCache();
  62. v.setWillNotCacheDrawing(willNotCache);
  63. v.setDrawingCacheBackgroundColor(color);
  64. return bitmap;
  65. }
  66. // 給圖片添加水印
  67. private Bitmap createBitmap(Bitmap src, String str) {
  68. Time t = new Time();
  69. t.setToNow();
  70. int w = src.getWidth();
  71. int h = src.getHeight();
  72. String mstrTitle = "截圖時間:"+t.hour + ":" + t.minute + ":" + t.second;
  73. Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
  74. Canvas canvas = new Canvas(bmpTemp);
  75. Paint p = new Paint();
  76. String familyName = "宋體";
  77. Typeface font = Typeface.create(familyName, Typeface.BOLD);
  78. p.setColor(Color.BLUE);
  79. p.setTypeface(font);
  80. p.setTextSize(22);
  81. canvas.drawBitmap(src, 0, 0, p);
  82. canvas.drawText(mstrTitle, 0, 20, p);
  83. canvas.save(Canvas.ALL_SAVE_FLAG);
  84. canvas.restore();
  85. return bmpTemp;
  86. }
  87. }
Copyright © Linux教程網 All Rights Reserved