当前位置: 代码迷 >> Android >> android截屏简略引用
  详细解决方案

android截屏简略引用

热度:61   发布时间:2016-05-01 15:14:07.0
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>


  相关解决方案