布局文件
当前位置:   article > 正文

Android将View转换为图片_android 将view转成图片

android 将view转成图片

权限管理

  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".activity.TextActivity">
  8. <RelativeLayout
  9. android:layout_centerVertical="true"
  10. android:id="@+id/view"
  11. android:layout_width="match_parent"
  12. android:layout_height="500dp"
  13. android:background="#FF3600"
  14. >
  15. <ImageView
  16. android:id="@+id/imageView"
  17. android:layout_width="match_parent"
  18. android:layout_height="300dp"
  19. />
  20. <TextView
  21. android:textSize="16dp"
  22. android:layout_marginBottom="5dp"
  23. android:layout_centerHorizontal="true"
  24. android:layout_alignParentBottom="true"
  25. android:text="世间万物中,表里如一者,又有几何."
  26. android:textColor="#FFFFFF"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content" />
  29. </RelativeLayout>
  30. <Button
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:id="@+id/button"
  34. android:text="点击!!!"
  35. />
  36. </RelativeLayout>

Activity文件

  1. package com.zhongqin.zhangshuai.activity;
  2. import android.Manifest;
  3. import android.annotation.TargetApi;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.graphics.Bitmap;
  7. import android.graphics.Canvas;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.os.Handler;
  13. import android.os.Message;
  14. import android.provider.MediaStore;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.ImageView;
  18. import com.bumptech.glide.Glide;
  19. import com.zhongqin.zhangshuai.R;
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. public class TextActivity extends BaseActivity {
  27. private Handler handler = new Handler() {
  28. @Override
  29. public void handleMessage(Message msg) {
  30. super.handleMessage(msg);
  31. String picFile = (String) msg.obj;
  32. String[] split = picFile.split("/");
  33. String fileName = split[split.length - 1];
  34. try {
  35. //图片创建
  36. MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), picFile, fileName, null);
  37. // 最后通知图库更新
  38. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + picFile)));
  39. toastMessage("图片保存图库成功");
  40. } catch (FileNotFoundException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. };
  45. private View view;
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_text);
  50. view = findViewById(R.id.view);
  51. Glide.with(this).load("https://kedou-1300812733.cos.ap-beijing.myqcloud.com/admin/2020/02/25/b51d6098accc42828f482c0c6cfe9532.png").into((ImageView)findViewById(R.id.imageView));
  52. findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. //必须View渲染完毕处理
  56. view.getHandler().post(new Runnable() {
  57. @Override
  58. public void run() {
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  60. checkAndRequestPermission(); //权限申请
  61. } else {
  62. //小于6.0,不用申请权限,直接执行
  63. saveMyBitmap("aaa",createViewBitmap(view));
  64. }
  65. }
  66. });
  67. }
  68. });
  69. }
  70. @TargetApi(Build.VERSION_CODES.M)
  71. private void checkAndRequestPermission() {
  72. List<String> lackedPermission = new ArrayList<String>();
  73. if (!(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
  74. lackedPermission.add(Manifest.permission.READ_EXTERNAL_STORAGE);
  75. }
  76. if (!(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
  77. lackedPermission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  78. }
  79. // 权限都已经有了,那么直接调用SDK
  80. if (lackedPermission.size() == 0) {
  81. saveMyBitmap("aaa",createViewBitmap(view));
  82. } else {
  83. // 请求所缺少的权限,在onRequestPermissionsResult中再看是否获得权限,如果获得权限就可以调用SDK,否则不要调用SDK。
  84. String[] requestPermissions = new String[lackedPermission.size()];
  85. lackedPermission.toArray(requestPermissions);
  86. requestPermissions(requestPermissions, 1024);
  87. }
  88. }
  89. private boolean hasAllPermissionsGranted(int[] grantResults) {
  90. for (int grantResult : grantResults) {
  91. if (grantResult == PackageManager.PERMISSION_DENIED) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. @Override
  98. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  99. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  100. if (requestCode == 1024 && hasAllPermissionsGranted(grantResults)) {
  101. saveMyBitmap("aaa",createViewBitmap(view));
  102. } else {
  103. toastMessage("缺少权限");
  104. }
  105. }
  106. public void saveMyBitmap(final String bitName, final Bitmap bitmap) {
  107. new Thread(new Runnable() {
  108. @Override
  109. public void run() {
  110. String filePath = Environment.getExternalStorageDirectory().getPath();
  111. File file = new File(filePath + "/DCIM/Camera/" + bitName + ".png");
  112. try {
  113. file.createNewFile();
  114. FileOutputStream fOut = null;
  115. fOut = new FileOutputStream(file);
  116. bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  117. Message msg = Message.obtain();
  118. msg.obj = file.getPath();
  119. handler.sendMessage(msg);
  120. Log.e("TAGTAGTAG","over:"+file.getPath());
  121. fOut.flush();
  122. fOut.close();
  123. } catch (IOException e) {
  124. Log.e("TAGTAGTAG","!!!:"+e.getMessage());
  125. e.printStackTrace();
  126. }
  127. }
  128. }).start();
  129. }
  130. //创建BitMap
  131. public Bitmap createViewBitmap(View v) {
  132. Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
  133. Canvas canvas = new Canvas(bitmap);
  134. v.draw(canvas);
  135. return bitmap;
  136. }
  137. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/262011?site
推荐阅读
相关标签