`
myhpu2008
  • 浏览: 87377 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android代码截屏

阅读更多

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

public class ScreenShot {
    // 获取指定Activity的截屏,保存到png文件
    private static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        Log.i("TAG", "" + statusBarHeight);

        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }

    // 保存到sdcard
    private static void savePic(Bitmap b, String strFileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFileName);
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 程序入口
    public static void shoot(Activity a) {
        ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
    }
}

 

 

需要注意的是,shoot方法只能在view已经被加载后方可调用。

或者在    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        ScreenShot.shoot(this);
    }中调用

分享到:
评论
4 楼 272954437 2013-07-26  
lz  这个代码截mapview会出现黑屏 有办法解决么?求大神指点
3 楼 vendor_api 2013-07-25  
vendor_api 写道
无语 原模原样的用 
Bitmap b1 = view.getDrawingCache();
值为空
lz  麻烦测试下再放上来

找到原因  这个代码 要在视图展示的时候 才可以
2 楼 vendor_api 2013-07-25  
无语 原模原样的用 
Bitmap b1 = view.getDrawingCache();
值为空
lz  麻烦测试下再放上来
1 楼 ihopethatwell 2012-09-19  
楼主能附上源码?

相关推荐

    Android 2.3截屏JNI代码

    Android 2.3截屏JNI代码 Android 2.3截屏JNI代码 Android 2.3截屏JNI代码

    Android 游戏源代码 Petraszd(含截图)

    Android 游戏源代码 Petraszd(含截图)Android 游戏源代码 Petraszd(含截图)Android 游戏源代码 Petraszd(含截图)Android 游戏源代码 Petraszd(含截图)Android 游戏源代码 Petraszd(含截图)Android 游戏源...

    Android 游戏源代码 Replicaisland(含截图)

    Android 游戏源代码 Replicaisland(含截图)Android 游戏源代码 Replicaisland(含截图)Android 游戏源代码 Replicaisland(含截图)Android 游戏源代码 Replicaisland(含截图)Android 游戏源代码 Replicaisland...

    android截屏代码

    android截图代码 常用的drawingcache方法代码

    Android 游戏源代码 DuckGame(含截图)

    Android 游戏源代码 DuckGame(含截图)Android 游戏源代码 DuckGame(含截图)Android 游戏源代码 DuckGame(含截图)Android 游戏源代码 DuckGame(含截图)Android 游戏源代码 DuckGame(含截图)Android 游戏源...

    Android代码-非常优雅的方案实现屏幕截图,利用android 5.0之后的录屏API实现截屏

    非常优雅的方案实现屏幕截图,利用android 5.0 之后的录屏API获取一帧画面,来实现截屏。 特性 方便后台service调用截屏功能。 打破老旧的截屏方案,只能截当前activity不能截状态栏的问题。 使用 直接编译...

    Android使用截图和录屏功能的代码例子

    Android使用截图和录屏功能的代码例子。演示了如何通过媒体投影管理器MediaProjectionManager实现截图和录屏功能,同时演示了悬浮窗在截图和录屏中的实际运用。

    Android代码-Android截图圆形Canvas.rar

    Android截图圆形Canvas.rar

    android截屏功能实现代码

    android开发中通过View的getDrawingCache方法可以达到截屏的目的,只是缺少状态栏! 原始界面 截屏得到的图片 代码实现 1. 添加权限(AndroidManifest.xml文件里) 代码如下:<uses android:name=”android....

    Android 游戏源代码 Particly(含截图)

    Android 游戏源代码 Particly(含截图)Android 游戏源代码 Particly(含截图)Android 游戏源代码 Particly(含截图)Android 游戏源代码 Particly(含截图)Android 游戏源代码 Particly(含截图)Android 游戏源...

    Android 游戏源代码 Jewels(含截图)

    Android 游戏源代码 Jewels(含截图)Android 游戏源代码 Jewels(含截图)Android 游戏源代码 Jewels(含截图)Android 游戏源代码 Jewels(含截图)Android 游戏源代码 Jewels(含截图)Android 游戏源代码 Jewels...

    Android 游戏源代码 PixelTowers(含截图)

    Android 游戏源代码 PixelTowers(含截图)Android 游戏源代码 PixelTowers(含截图)Android 游戏源代码 PixelTowers(含截图)Android 游戏源代码 PixelTowers(含截图)Android 游戏源代码 PixelTowers(含截图)...

    Android 游戏源代码 Emptyyourmind(含截图)

    Android 游戏源代码 Emptyyourmind(含截图)Android 游戏源代码 Emptyyourmind(含截图)Android 游戏源代码 Emptyyourmind(含截图)Android 游戏源代码 Emptyyourmind(含截图)Android 游戏源代码 Emptyyourmind...

    Android 游戏源代码 Spaceassault(含截图)

    Android 游戏源代码 Spaceassault(含截图)Android 游戏源代码 Spaceassault(含截图)Android 游戏源代码 Spaceassault(含截图)Android 游戏源代码 Spaceassault(含截图)Android 游戏源代码 Spaceassault(含...

    Android 游戏源代码 PlantsVsBugs(含截图)

    Android 游戏源代码 PlantsVsBugs(含截图)Android 游戏源代码 PlantsVsBugs(含截图)Android 游戏源代码 PlantsVsBugs(含截图)Android 游戏源代码 PlantsVsBugs(含截图)Android 游戏源代码 PlantsVsBugs(含...

    Android 游戏源代码 BlockBreaker(含截图)

    Android 游戏源代码 BlockBreaker(含截图)Android 游戏源代码 BlockBreaker(含截图)Android 游戏源代码 BlockBreaker(含截图)Android 游戏源代码 BlockBreaker(含截图)Android 游戏源代码 BlockBreaker(含...

    Android 游戏源代码 TheHardestGame(含截图)

    Android 游戏源代码 TheHardestGame(含截图)Android 游戏源代码 TheHardestGame(含截图)Android 游戏源代码 TheHardestGame(含截图)Android 游戏源代码 TheHardestGame(含截图)Android 游戏源代码 ...

    Android代码-安卓内存清理 改进版

    应用截图 Open source projects ButterKnife WaveView Dagger 2 RxAndroid EventBus Stetho LeakCanary recycler-fast-scroll CircularFillableLoaders hellocharts-android ...

    android 截屏 c 源代码

    每次截取30帧,每秒可达10帧,保存为yuv值

Global site tag (gtag.js) - Google Analytics