易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Android截图以及加水印Demo
实现一个简单的截图功能以及给图片添加水印的功能,直接上代码!
Android截图以及加水印Demo下载地址:
免费下载地址在
http://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;
}
}
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;
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图