Android 截屏技术

    android:layout_marginTop="16dp" />



<Button

    android:id="@+id/bt2"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_alignLeft="@+id/bt"

    android:layout_below="@+id/bt1"

    android:layout_marginTop="30dp"

    android:text="截屏技术2222" />



<Button

    android:id="@+id/bt3"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"

    android:layout_below="@+id/bt2"

    android:layout_marginTop="33dp"

    android:text="截屏技术3333" />

MainActivity.java



package com.example.screenshoot;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.graphics.Rect;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private Button bt1, bt2, bt3;

View rootView;

@Override

protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);

	setContentView(R.layout.activity_main);

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

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

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

	bt1.setOnClickListener(this);

	bt2.setOnClickListener(this);

	bt3.setOnClickListener(this);

	rootView= findViewById(R.id.rootView);

}



@Override

public void onClick(View v) {

	switch (v.getId()) {

	case R.id.bt1:// View的getDrawingCache()方法      

		View view = this.getWindow().getDecorView();

		//或者View view=v.getRootView();

		view.setPressed(false);//这样就不会截取到button被按下的状态

		view.setDrawingCacheEnabled(true);

		view.buildDrawingCache();

		Bitmap b1 = view.getDrawingCache();



		// 获取状态栏高度

		Rect frame = new Rect();

		this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

		int statusBarHeight = frame.top;

		Log.i("TAG", "" + statusBarHeight);



		// 获取屏幕长和高

		int width = getResources().getDisplayMetrics().widthPixels;

		int height =getResources().getDisplayMetrics().heightPixels;

		// 去掉标题栏

		// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);

		Bitmap bitmap = Bitmap.createBitmap(b1, 0, statusBarHeight, width,

				height - statusBarHeight);

		saveMyBitmap("noTitle", bitmap);// 去掉标题栏的

		saveMyBitmap("hadTitle", b1);

		view.destroyDrawingCache();



		break;

	case R.id.bt2:

		// TODO Auto-generated method stub

		Bitmap newb = Bitmap.createBitmap(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels, Config.ARGB_8888);

		Canvas canvas = new Canvas(newb);

		rootView.draw(canvas);



		File file = new File(Environment.getExternalStorageDirectory()

				+ "/" + "secondMethod.png");



		FileOutputStream f = null;

		try {

			f = new FileOutputStream(file);

		} catch (FileNotFoundException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		boolean b = newb.compress(Bitmap.CompressFormat.PNG, 100, f);

		try {

			f.flush();

		} catch (IOException e) {

			e.printStackTrace();

		}

		try {

			f.close();

		} catch (IOException e) {

			e.printStackTrace();

		}

		if (b) {

			// 截图成功

			Toast.makeText(this, "截图成功", 1).show();

		}



		break;



	case R.id.bt3:

        saveMyBitmap("thirdMethod", getViewBitmap(v));;

		break;

	default:

		break;

	}



}



/**

 * 保存bitmap到sd卡(本项目的file文件夹下)

 * 

 * @param bitName

 * @param mBitmap

 */

public void saveMyBitmap(String bitName, Bitmap mBitmap) {

	File f = new File(getFilesDir(), bitName + ".png");

	try {

		f.createNewFile();

	} catch (IOException e) {

		// TODO Auto-generated catch block

		// DebugMessage.put("在保存图片时出错:"+e.toString());

		e.printStackTrace();

	}

	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();

	}

}



public static Bitmap getViewBitmap(View v) {



	v.clearFocus(); //



	v.setPressed(false); //



	// 能画缓存就返回false



	boolean willNotCache = v.willNotCacheDrawing();



	v.setWillNotCacheDrawing(false);



	int color = v.getDrawingCacheBackgroundColor();



	v.setDrawingCacheBackgroundColor(0);



	if (color != 0) {



		v.destroyDrawingCache();



	}



	v.buildDrawingCache();



	Bitmap cacheBitmap = v.getDrawingCache();



	if (cacheBitmap == null) {



		return null;



	}



	Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);



	// Restore the view



	v.destroyDrawingCache();



	v.setWillNotCacheDrawing(willNotCache);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值