Android屏幕截图,view转bitmap

时间:2021-09-13 15:21:30

开发中经常会用到屏幕的截图,其实我们常用的一种方法是将view转换成bitmap,这样可以截取你想要的那一部分的图片了,实现代码如下:


package zhangphil.viewtoimage;

import java.io.File;
import java.io.FileOutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

/**
* 把Android的一个View转换成图片保存
*
* */


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView tv = (TextView) findViewById(R.id.textView);
tv.setBackgroundColor(Color.GREEN);
// tv.setDrawingCacheEnabled(true);

final Runnable runnable = new Runnable() {
@Override
public void run() {
viewSaveToImage(tv);
}
};

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
new Handler().post(runnable);
}
});

}

public void viewSaveToImage(View view) {
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
view.setDrawingCacheBackgroundColor(Color.WHITE);

// 把一个View转换成图片
Bitmap cachebmp = loadBitmapFromView(view);

// 添加水印
Bitmap bitmap = Bitmap.createBitmap(createWatermarkBitmap(cachebmp,
"@ Zhang Phil"));

FileOutputStream fos;
try {
// 判断手机设备是否有SD卡
boolean isHasSDCard = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (isHasSDCard) {
// SD卡根目录
File sdRoot = Environment.getExternalStorageDirectory();
File file = new File(sdRoot, "test.PNG");
fos = new FileOutputStream(file);
} else
throw new Exception("创建文件失败!");

bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);

fos.flush();
fos.close();

} catch (Exception e) {
e.printStackTrace();
}

view.destroyDrawingCache();
}

private Bitmap loadBitmapFromView(View v) {
int w = v.getWidth();
int h = v.getHeight();

Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);

c.drawColor(Color.WHITE);
/** 如果不设置canvas画布为白色,则生成透明 */

v.layout(0, 0, w, h);
v.draw(c);

return bmp;
}

// 为图片target添加水印
private Bitmap createWatermarkBitmap(Bitmap target, String str) {
int w = target.getWidth();
int h = target.getHeight();

Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);

Paint p = new Paint();

// 水印的颜色
p.setColor(Color.RED);

// 水印的字体大小
p.setTextSize(16);

p.setAntiAlias(true);// 去锯齿

canvas.drawBitmap(target, 0, 0, p);

// 在中间位置开始添加水印
canvas.drawText(str, w / 2, h / 2, p);

canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();

return bmp;
}
}

可以直接使用