温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中布局保存成图片实例代码

发布时间:2020-09-23 01:48:27 来源:脚本之家 阅读:185 作者:邢二创 栏目:移动开发

一、前言

有一个朋友问做过截屏的小功能没,自己没有做过。但是想了一下,实现的过程。实现截屏就是为了截取咱们应用中的部分布局,然后实现将保存在本地,或将其分享,或将其通过第三方的平台分享出去。自己可能是受了截屏这两个字的影响,想当然的去梳理自己的实现思路。

1:截屏,调用系统的截屏功能区实现截屏。

2:对图片进行处理:截屏是截取的手机的全屏,因为我们是需要截取我们的应用的某一部分,所以我们需要去通过剪切裁剪,去裁剪出自己想要保留的一部分(实质也就是保存布局)。

3:后续也许还要处理系统截屏功能本身所有的分享等其他的功能。反正就是会有各种问题。

上面有一个词儿是“保存布局”。意思就是将我们的布局保存成为图片。我听到这个之后,今天到公司的第一件事儿就是自己去实现一下所谓的截屏这个功能。

二、实现流程以及实现代码

0:设置权限

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

1:获取布局

relativeLayout = (RelativeLayout) findViewById(R.id.layout_rl);

2:设置布局相关设置

 // 获取图片某布局
relativeLayout.setDrawingCacheEnabled(true);
relativeLayout.buildDrawingCache();

3.获取图片

 final Bitmap bmp = relativeLayout.getDrawingCache(); // 获取图片
 savePicture(bmp, "test.jpg");// 保存图片

4:保存图片

public void savePicture(Bitmap bm, String fileName) {
    Log.i("xing", "savePicture: ------------------------");
    if (null == bm) {
      Log.i("xing", "savePicture: ------------------图片为空------");
      return;
    }
    File foder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test");
    if (!foder.exists()) {
      foder.mkdirs();
    }
    File myCaptureFile = new File(foder, fileName);
    try {
      if (!myCaptureFile.exists()) {
        myCaptureFile.createNewFile();
      }
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
      //压缩保存到本地
      bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
      bos.flush();
      bos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();

  }

5:释放资源

relativeLayout.destroyDrawingCache();

6、完整代码如下

package com.adwan.savephototolocal;

import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
  private RelativeLayout relativeLayout;
  private Handler mHandler = new Handler();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.id.layout_rl);

  }

  public void save(View view) {
    initView();
  }
  private void initView() {
    // 获取图片某布局
    relativeLayout.setDrawingCacheEnabled(true);
    relativeLayout.buildDrawingCache();

    mHandler.postDelayed(new Runnable() {

      @Override
      public void run() {
        // 要在运行在子线程中
        final Bitmap bmp = relativeLayout.getDrawingCache(); // 获取图片
        savePicture(bmp, "test.jpg");// 保存图片
        relativeLayout.destroyDrawingCache(); // 保存过后释放资源
      }
    },100);
  }

  public void savePicture(Bitmap bm, String fileName) {
    Log.i("xing", "savePicture: ------------------------");
    if (null == bm) {
      Log.i("xing", "savePicture: ------------------图片为空------");
      return;
    }
    File foder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test");
    if (!foder.exists()) {
      foder.mkdirs();
    }
    File myCaptureFile = new File(foder, fileName);
    try {
      if (!myCaptureFile.exists()) {
        myCaptureFile.createNewFile();
      }
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
      //压缩保存到本地
      bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
      bos.flush();
      bos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();

  }

}

三、总结

很显然,如果利用保存布局的方式去解决这个问题,我们就算是第一次做,也用不到三十分钟就能搞定,如果去用调用系统截屏的方案去解决的话,同样是第一次做,估计一天也很危险,同时也会存在有很多未知的问题和局限性。

这个问题虽然很小,但是让我收到的感触确实很大。感触就是在我们解决问题之前,一定要定义好自己的问题。就以这个问题,如果这个问题换个问法。保存布局,而不是截屏。估计我们每个人都能会想到以上的解决方案。所以在在我们定义问题的时候一定要完全弄明白是怎么回事儿。虽然同样是可以解决问题,但是有可能会出现一些杀鸡用牛刀的现象。问题的定义也就是数模的转换。

第二就是处理问题一定要去做出几个不同的预选备案,从而再去选择一个嘴适合自己的去处理问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI