实现屏幕截图的代码-Android

时间:2022-09-05 15:22:49

转自:http://www.eoeandroid.com/thread-95111-1-1.html

今天实现了下屏幕截图 但是方法不是很优雅 还请大牛给予指点

界面很简单:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rootLayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="save"
android:id="@+id/btn"
/>
</LinearLayout>

界面视图:
实现屏幕截图的代码-Android

下面是代码:
package com.babeeta.drawview;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
/** Called when the activity is first created. */

Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
View rootView = findViewById(R.id.rootLayout);

Bitmap newb = Bitmap.createBitmap( 320, 480, Config.ARGB_8888 );

Canvas canvas = new Canvas(newb);

rootView.draw(canvas);

File file = new File(Environment.getExternalStorageDirectory()+ "/" + "1.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);

if(b){
//截图成功

}
}
});
}
}

成功后保存到SD卡中 效果如下:
实现屏幕截图的代码-Android