首页 > 代码库 > 应用内截屏的代码,在Activity中测试可用

应用内截屏的代码,在Activity中测试可用

截屏功能让我十分头疼,想做个无需root的又找不到资料。这里暂且分享一个无需root的,在应用内截屏的代码,本文转自:http://blog.csdn.net/csh159/article/details/7876415

  截图后→  

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/imageView"        android:layout_width="fill_parent"        android:layout_height="350dip"         android:src="@drawable/right"/>    <Button        android:id="@+id/shotButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="shot" /></LinearLayout>

 

 

MainActivity

package com.kale.floattest;import android.R.color;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {    private Button shotButton;    private ImageView imageView;    BitmapDrawable bd;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        shotButton = (Button) findViewById(R.id.shotButton);        imageView = (ImageView) findViewById(R.id.imageView);        shotButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                 imageView.setImageBitmap(shot());            }        });        //startService(new Intent(MainActivity.this, TestService.class));    }    /**     * 截屏方法     *      * @return     */    private Bitmap shot() {        View view = getWindow().getDecorView();        Display display = this.getWindowManager().getDefaultDisplay();        view.layout(0, 0, display.getWidth(), display.getHeight());        view.setDrawingCacheEnabled(true);// 允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap        Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());        return bmp;    }}