当前位置:   article > 正文

Android将View保存为图片到SD卡_android view保存为图片

android view保存为图片

第一步:

申请权限

  1. <!-- 往SDCard写入数据权限 -->
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  3. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
第二步:

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="56dp"
  11. android:background="@color/mainColor"
  12. android:gravity="center"
  13. android:text="自动详情图"
  14. android:textColor="#fff"
  15. android:textSize="20sp" />
  16. <EditText
  17. android:id="@+id/etContent"
  18. android:layout_width="match_parent"
  19. android:layout_height="56dp"
  20. android:hint="输入文字"
  21. android:textColorHint="#C1C1C1" />
  22. <Button
  23. android:layout_width="match_parent"
  24. android:layout_height="56dp"
  25. android:onClick="Button"
  26. android:text="Button" />
  27. <RelativeLayout
  28. android:id="@+id/rlContent"
  29. android:layout_width="match_parent"
  30. android:layout_height="70dp"
  31. android:layout_marginTop="10dp"
  32. android:background="#D73ADC"
  33. android:gravity="center">
  34. <TextView
  35. android:id="@+id/tvContent"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="文字"
  39. android:textColor="#fff"
  40. android:textSize="18sp" />
  41. </RelativeLayout>
  42. <ImageView
  43. android:id="@+id/ivPreview"
  44. android:layout_width="match_parent"
  45. android:layout_height="70dp"
  46. android:layout_marginTop="10dp"
  47. android:background="#C0C0C0"
  48. android:gravity="center" />
  49. </LinearLayout>
第三步:

Java代码

  1. package com.xiong.autodetail.activity;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.ImageView;
  9. import android.widget.RelativeLayout;
  10. import android.widget.TextView;
  11. import com.squareup.picasso.Picasso;
  12. import com.xiong.autodetail.R;
  13. import com.xiong.autodetail.utils.FileUtil;
  14. import com.xiong.autodetail.utils.ToastUtil;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. public class MainActivity extends BaseActivity {
  18. private EditText etContent;
  19. private TextView tvContent;
  20. private RelativeLayout rlContent;
  21. private ImageView ivPreview;
  22. private Context context = this;
  23. final private static int REFRESH_COMPLETE = 0;//Message标记
  24. final private static int SET_TEXT_TIME = 1;//设置文本延时时间(单位毫秒),避免图片上文字生成不完整
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. initView();
  30. }
  31. //初始化控件
  32. private void initView() {
  33. etContent = (EditText) findViewById(R.id.etContent);
  34. tvContent = (TextView) findViewById(R.id.tvContent);
  35. rlContent = (RelativeLayout) findViewById(R.id.rlContent);
  36. ivPreview = (ImageView)findViewById(R.id.ivPreview);
  37. }
  38. //按钮响应事件
  39. public void Button(View view) {
  40. if(etContent.length() > 0){
  41. String content = etContent.getText().toString();
  42. tvContent.setText(content);
  43. mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, SET_TEXT_TIME);
  44. }else{
  45. ToastUtil.show("未输入!");
  46. }
  47. }
  48. //Hadle
  49. private Handler mHandler = new Handler() {
  50. public void handleMessage(android.os.Message msg) {
  51. switch (msg.what) {
  52. case REFRESH_COMPLETE:
  53. save(getBitmap(rlContent));
  54. etContent.setText("");
  55. break;
  56. }
  57. }
  58. };
  59. //将View转为Bitmap
  60. public Bitmap getBitmap(View view) {
  61. view.setDrawingCacheEnabled(true);
  62. Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
  63. view.setDrawingCacheEnabled(false);
  64. return bitmap;
  65. }
  66. //将Bitmap保存为图片
  67. private void save(Bitmap bitmap) {
  68. try {
  69. String fileName = tvContent.getText().toString().substring(0, 2) + ".png";
  70. FileOutputStream stream = new FileOutputStream(FileUtil.getDownPath() + fileName);
  71. bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  72. stream.flush();
  73. stream.close();
  74. ToastUtil.show("操作完成");
  75. previewImg(FileUtil.getDownPath() + fileName);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. //预览图片
  81. private void previewImg(String filePath){
  82. Picasso.with(context).load(new File(filePath)).into(ivPreview);
  83. }
  84. }


工具类方法:

  1. public static String getDownPath() {
  2. String path = Environment.getExternalStorageDirectory() + "/000_AAA/";
  3. isDownFolder(path);
  4. return path;
  5. }
  6. //判断文件夹是否存在,如果不存在则创建文件夹
  7. public static void isDownFolder(String downFolder) {
  8. File file = new File(downFolder);
  9. if (!file.exists()) {
  10. file.mkdir();
  11. }
  12. }

Picasso是一个第三方框架,这里只是用来加载预览图的。

效果图:






声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号