继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android截图

明月笑刀无情
关注TA
已关注
手记 104
粉丝 17
获赞 57

如何实现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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP

热门评论

我的手机有截图功能,但是没有SystemUI文件

查看全部评论