android截屏简单引用

直接给代码吧:

package com.xy.shot;

import android.R.color;
import android.app.Activity;  
import android.graphics.Bitmap;  
import android.graphics.Color;
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;  
    private boolean mFlag=false;
    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) {  
                //Bitmap-->Drawable  
            	
            	if(mFlag){
            		imageView.setBackgroundColor(color.black); 
            		mFlag=!mFlag;
            	}else{
            		bd=new BitmapDrawable(shot());  
            		 imageView.setBackgroundDrawable(bd); 
            		 mFlag=!mFlag;
            	}
                
               
//              imageView.setImageBitmap(shot());  
            }  
        });  
    }  
  
    /** 
     * 截屏方法 
     * @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;  
    }  
} 

xml文件代码:

<?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" />

    <Button
        android:id="@+id/shotButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="shot" />

</LinearLayout>


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值