Android截图以及加水印Demo .
2012-04-22 23:28
399 查看
实现一个简单的截图功能以及给图片添加水印的功能,直接上代码!
一、代码实现
[java]view plaincopyprint?
<span style="font-size:18px;">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;
}
}</span>
[html] view plaincopyprint? <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/getThum" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_getthum"/> <ImageView android:id="@+id/setThum" android:background="@drawable/no_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/source" android:background="@drawable/v" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </span> <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/getThum" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_getthum"/> <ImageView android:id="@+id/setThum" android:background="@drawable/no_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/source" android:background="@drawable/v" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </span>
3、源码下载地址:http://download.csdn.net/detail/tangcheng_ok/3670937
4、效果演示
![](http://hi.csdn.net/attachment/201110/10/0_13182332250cdp.gif)
相关文章推荐
- Android截图以及加水印Demo
- Android截图以及加水印Demo
- android view的显示/隐藏以及DEMO
- 微信开放平台 调试 方法。android demo调试方法。以及难以调试的解决方法 keystore
- Android调用百度api 获取经纬度以及地图 demo你怎么可以那么坑我????
- Android 地图marker聚合显示的算法,以及完整demo
- Android MVP 在项目中的使用 以及demo
- Android圆盘菜单(修改了网络上现有的demo总是显示第一个的bug,以及增加了点击事件)
- android 各种组件使用以及详解和各种小demo地址集(持续更新中...)
- android——自定义截图(加水印、logo等)
- Android接口回调总结,以及运用到弹窗PopWindow的Demo实现
- Android选择头像(从相机-相册-截图获取),以及闪退的解决
- Android 媒体键监听以及模拟媒体键盘的实现 demo
- android获取手机验证码界面以及倒计时实现demo
- Face++在线接口在Android下的使用,以及简单的Demo实现(判断两张脸是否为同一个人)
- Android软件版本更新流程梳理以及Demo
- Google cardBoard Android的两个jar包,以及Demo
- android 中 intent 是经常要用到的DEMO以及Intent 调用说明
- Android——AIDL基础实现demo以及原理探究
- android 帧动画的使用以及dialog使用帧动画(附demo)