亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述Android View转换成图片保存相关的知识,希望能为你提供帮助。
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() {
【Android View转换成图片保存】@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;
}
}
推荐阅读
- 移动app传统测试流程优化
- 使用安卓生成二维码
- instruments无法连接,设备查询不到,找不到工程,查询不到对应app
- app开屏广告
- bzoj1710[Usaco2007 Open]Cheappal 廉价回文
- Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administr
- MappingJackson2对日期格式转化
- 给Lumia 520/521/525/526/720刷Android系统
- 将刷了Android的Lumia恢复为WP系统