为了账号安全,请及时绑定邮箱和手机立即绑定

Android截图

标签:
Android

如何实现android手机同时按下电源键和音量-按键截图?

android 的截图功能的实现是在SystemUI.apk中做的, 源码位于SystemUI\src\com\android\systemui\screenshot包下,

截图功能的实现简单,TakeScreenshotService.java 供其他应用远程访问,GlobalScreenshot.java 实现截图功能。

 /**

     * Takes a screenshot of the current display and shows an animation.

     */

    void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) {

        // We need to orient the screenshot correctly (and the Surface api seems to take screenshots

        // only in the natural orientation of the device :!)

        mDisplay.getRealMetrics(mDisplayMetrics);

        float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};

        float degrees = getDegreesForRotation(mDisplay.getRotation());

        boolean requiresRotation = (degrees > 0);

        if (requiresRotation) {

            // Get the dimensions of the device in its native orientation

            mDisplayMatrix.reset();

            mDisplayMatrix.preRotate(-degrees);

            mDisplayMatrix.mapPoints(dims);

            dims[0] = Math.abs(dims[0]);

            dims[1] = Math.abs(dims[1]);

        }

        // Take the screenshot

        mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);

        if (mScreenBitmap == null) {

            notifyScreenshotError(mContext, mNotificationManager);

            finisher.run();

            return;

        }

        if (requiresRotation) {

            // Rotate the screenshot to the current orientation

            Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,

                    mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);

            Canvas c = new Canvas(ss);

            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);

            c.rotate(degrees);

            c.translate(-dims[0] / 2, -dims[1] / 2);

            c.drawBitmap(mScreenBitmap, 0, 0, null);

            c.setBitmap(null);

            mScreenBitmap = ss;

        }

        // Optimizations

        mScreenBitmap.setHasAlpha(false);

        mScreenBitmap.prepareToDraw();

        // Start the post-screenshot animation

        startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,

                statusBarVisible, navBarVisible);

    }

 mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);  获取到Bitmap

后续做的一些操作解释将图片保存到flash,更新数据库,发送一些notification通知用户目前的截图状态;

,如果我们需要在自己的Apk中实现截图 ,我们可以

private void takeScreenshot() {

synchronized (mScreenshotLock) {

if (mScreenshotConnection != null) {

return;

}

ComponentName cn = new ComponentName("com.android.systemui",

"com.android.systemui.screenshot.TakeScreenshotService");

Intent intent = new Intent();

intent.setComponent(cn);

ServiceConnection conn = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

synchronized (mScreenshotLock) {

if (mScreenshotConnection != this) {

return;

}

Messenger messenger = new Messenger(service);

Message msg = Message.obtain(null, 1);

final ServiceConnection myConn = this;

Handler h = new Handler(mHandler.getLooper()) {

@Override

public void handleMessage(Message msg) {

synchronized (mScreenshotLock) {

if (mScreenshotConnection == myConn) {

unbindService(mScreenshotConnection);

mScreenshotConnection = null;

mHandler.removeCallbacks(mScreenshotTimeout);

}

}

}

};

msg.replyTo = new Messenger(h);

msg.arg1 = msg.arg2 = 0;

// if (mStatusBar != null && mStatusBar.isVisibleLw())

// msg.arg1 = 1;

// if (mNavigationBar != null &&

// mNavigationBar.isVisibleLw())

// msg.arg2 = 1;

try {

messenger.send(msg);

} catch (RemoteException e) {

}

}

}

@Override

public void onServiceDisconnected(ComponentName name) {

}

};

if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) {

mScreenshotConnection = conn;

mHandler.postDelayed(mScreenshotTimeout, 10000);

}

}

}

完成Service的绑定,向Service发送一个Handler消息即可,

同时注意该 service 是不可垮进程访问的 ,所以给修改 mainfest.xml  将TakeScreenshotService 。export属性改为true;

原文链接:http://www.apkbus.com/blog-605334-63494.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消