当前位置:编程问答 > 安卓/Android >

android截屏简单引用

直接给代码吧:
[html]
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文件代码:
[html] 
<?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> 



补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,