Android之j截取全屏代码
来源:互联网 发布:游戏加密软件 编辑:程序博客网 时间:2024/05/16 13:01
分享一个简单的截屏代码。
1.主Activity
- package com.zhf.ScreenshotDemo;
- import Android.app.Activity;
- 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 ScreenshotDemoActivity extends Activity {
- private Button shotButton;
- private ImageView imageView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.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
- BitmapDrawable bd=new BitmapDrawable(shot());
- imageView.setBackgroundDrawable(bd);
- // 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;
- }
- }
2.布局
- <?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>
1. 截屏函数似乎应该在onCreate方法之后调用,否则截取不到
2. 截取的图片比屏幕要小,看起来像是少了一个标题栏的高度
- Android之j截取全屏代码
- 【Android代码片段之四】设置全屏
- Java截屏代码,截取全屏
- android全屏代码
- Android全屏设置代码
- android全屏设置代码
- android 实现全屏代码
- android全屏代码
- Android 全屏代码
- Android设置全屏代码
- Android设置全屏代码
- android 实现全屏代码
- android全屏代码
- android代码全屏与非全屏
- Android 之 Activity全屏
- android代码设置全屏显示
- Android代码设置全屏显示
- android设置全屏壁纸代码
- 使用本地git管理xcode代码:
- 子类化:增强Edit控件为日期输入控件
- Eclipse如何自动生成注释
- 我的编程
- Android系统移植技术详解
- Android之j截取全屏代码
- Android权限之sharedUserId和签名
- 重写javascript中window.confirm的行为
- strts2自定义拦截器使用小记
- android----设置TextView控件文字居中
- 知易游戏开发教程cocos2d-x移植版
- 不通过第三个变量来交换两个变量的值
- JSP的内置对象及方法
- 奥巴马成功连任美国总统