今天做分享,需求是截图分享,做了也是一个运动类的产品,那好,我们就直接开始做,考虑了一下,因为是全屏的分享,所有很自然而然的想到了View的getDrawingCache()方法来获取Bitmap,看到网上有人说截取不了WebView上的图片,倒是没有去尝试,因为我们的应用不需要,不过有时间还是要去试试,占占坑,这篇博客只是记录一下知识点,没什么技术含量

我们写个小Sample就好了

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical"

android:padding="15dp">

<ImageView

android:id="@ id/ivPlay"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<Button

Ok,但是这样并没有保存在内存卡上,我们需要保存一下,做图片上传的功能对吧,所有,我们再来写一个保存图片的方法

/** * 保存到内存卡 * * @param bitName * @param mBitmap */ public void saveBitmapForSdCard(String bitName, Bitmap mBitmap) { //创建file对象 File f = new File("/sdcard/" bitName ".png"); try { //创建 f.createNewFile(); } catch (IOException e) { } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } //原封不动的保存在内存卡上 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } }

把我们的bitmap通过流保存,同时获取本地的时间命名,我们的点击事件就是这样:

/** * 点击事件 * * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnAllWindow: Bitmap bitmap = captureScreenWindow(); ivPlay.setImageBitmap(bitmap); long time = System.currentTimeMillis(); saveBitmapForSdCard("img" time, bitmap); break; } }

最后的结果

android全屏截图(Android全屏截图的方法)(1)

OK,这只是一个很简单的截图功能,当然,还有很多其他的方法,大家自己可以去研究一下,全部代码

MainActivity

package com.liuguilin.screenshotssample;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//截取全屏

private Button btnAllWindow;

private ImageView ivPlay;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

/**

* 初始化

*/

private void initView() {

btnAllWindow = (Button) findViewById(R.id.btnAllWindow);

btnAllWindow.setOnClickListener(this);

ivPlay = (ImageView) findViewById(R.id.ivPlay);

}

/**

* 点击事件

*

* @param v

*/

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btnAllWindow:

Bitmap bitmap = captureScreenWindow();

ivPlay.setImageBitmap(bitmap);

long time = System.currentTimeMillis();

saveBitmapForSdCard("img" time, bitmap);

break;

}

}

/**

* 截取全屏

*

* @return

*/

public Bitmap captureScreenWindow() {

getWindow().getDecorView().setDrawingCacheEnabled(true);

Bitmap bmp = getWindow().getDecorView().getDrawingCache();

return bmp;

}

/**

* 保存到内存卡

*

* @param bitName

* @param mBitmap

*/

public void saveBitmapForSdCard(String bitName, Bitmap mBitmap) {

//创建file对象

File f = new File("/sdcard/" bitName ".png");

try {

//创建

f.createNewFile();

} catch (IOException e) {

}

FileOutputStream fOut = null;

try {

fOut = new FileOutputStream(f);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

//原封不动的保存在内存卡上

mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

try {

fOut.flush();

} catch (IOException e) {

e.printStackTrace();

}

try {

fOut.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

要注意添加一下权限哦

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

貌似代码就这么点,Demo也可以不用上传了,想一起学习Android的可以加群:555974449

什么?小伙伴你需要?那我上传好了:http://download.csdn.net/detail/qq_26787115/9637229

,