本文示範兩個實用的小功能:把畫面上的 View 轉成一張圖片(截圖),以及在圖片上疊加文字浮水印。原理不難,直接看程式碼最快。
免費下載位址:https://linux.linuxidc.com/
帳號與密碼皆為 www.linuxidc.com
下載目錄:/pub/Android源碼集錦/2011年/10月/Android截圖以及加水印Demo/
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class GetAppThumbnailActivity extends Activity {
private Button btnThum;
private ImageView imgThum;
private ImageView imgSource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews() {
btnThum = (Button) findViewById(R.id.getThum);
imgThum = (ImageView) findViewById(R.id.setThum);
imgSource = (ImageView) findViewById(R.id.source);
btnThum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = getViewBitmap(imgSource);
Bitmap bitmap1 = createBitmap(bitmap, "haha哈哈");
if (bitmap1 != null) {
imgThum.setImageBitmap(bitmap1);
}
}
});
}
/**
* Draw the view into a bitmap.
*/
private Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
// 給圖片添加水印
private Bitmap createBitmap(Bitmap src, String str) {
Time t = new Time();
t.setToNow();
int w = src.getWidth();
int h = src.getHeight();
String mstrTitle = "截圖時間:" + t.hour + ":" + t.minute + ":" + t.second;
Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
String familyName = "宋體";
Typeface font = Typeface.create(familyName, Typeface.BOLD);
p.setColor(Color.BLUE);
p.setTypeface(font);
p.setTextSize(22);
canvas.drawBitmap(src, 0, 0, p);
canvas.drawText(mstrTitle, 0, 20, p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}
}
getViewBitmap() 的做法是借用 View 自身的繪製快取:先關掉「不快取繪製」的設定、把快取背景色暫時設為全透明,呼叫 buildDrawingCache() 產生快取,再從 getDrawingCache() 取回 Bitmap 並複製一份。最後別忘了還原 View 的原始狀態(destroyDrawingCache、回復 willNotCache 與背景色),否則會影響後續繪製。
createBitmap() 先建立一張同尺寸的 ARGB_8888 空白圖,用 new Canvas(bmpTemp) 綁定上去,接著把原圖 drawBitmap 畫進去,再用 drawText 疊上時間字串。Paint 負責決定顏色、字型與字級,這裡刻意指定「宋體」並設為粗體、藍色、22sp。
想改成自己的浮水印,只要替換 mstrTitle 的內容與 Paint 的樣式即可;若偏好圖片浮水印,把 drawText 換成 drawBitmap 並調整座標就行了。