Android custom screenshot function, similar to QQ screenshot, android screenshot
Because the company's business needs to take screenshots of a screen, but the built-in screenshot function is far from the functional requirements of the project. We are a canvas software that requires screenshots like QQ, we can see our custom tools, including paint brushes and buttons. The built-in functions of android are very simple. It is enough to call Intent implicitly. However, it is a system application and the interface is fixed and cannot be customized and modified. There are many implementation methods and methods. The following describes the implementation methods. I am using this idea to rewrite a View component. In OnDraw, I am only responsible for not drawing images (including translucent four rectangles, bright box rectangles, and four dots on the bright box ), the Ontouch method constantly modifies the coordinates of the bright box. And then redraw.
:
I split this image into the shape of the image below. We keep drawing Rectangles and dots in onTouch.
Main Ideas for code implementation:
1. Image Rendering Method:
@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // if (mBitmap! = Null) {// drawNoLight (canvas); canvas. drawBitmap (mBitmap, iconLeft, iconTop, p_picture); // draw the highlighted boundary drawRect (canvas); if (isDown) drawCircle (canvas );}}
2. How to change the image coordinates:
@ Overridepublic boolean onTouchEvent (MotionEvent event) {int action = event. getAction (); float x = event. getX (); float y = event. getY (); switch (action) {case MotionEvent. ACTION_DOWN: startX = x; startY = y; // you need to determine whether it is outside the rectangle or inside the rectangle (whether it is moving or scaling) if (x> lightRect. left + OFFSET & x <lightRect. right-OFFSET & y> lightRect. top + OFFSET & y <lightRect. bottom-OFFSET) {// The status of moving isMove = true; isScale = false;} else if (x <li GhtRect. left-OFFSET | y <lightRect. top-OFFSET | x> lightRect. right + OFFSET | y> lightRect. bottom + OFFSET) {isMove = false; isScale = false;} else {isMove = false; isScale = true; // scale point = getScalePoint (startX, startY);} if (! IsScale) isDown = false; break; case MotionEvent. ACTION_UP: case MotionEvent. ACTION_CANCEL: isDown = true; break; case MotionEvent. ACTION_MOVE: if (isMove) {// move float dx = x-startX; float dy = y-startY; moveLightRect (dx, dy); startX = x; startY = y; isDown = false;} if (isScale) {float dx = x-startX; float dy = y-startY; resetLightRect (dx, dy); startX = x; startY = y ;} break; default: break;} invalidate (); return true ;}
3. Image Capturing methods:
public Bitmap getBitmap (){int x = (int)(lightRect.left - iconLeft) ;int y = (int)(lightRect.top - iconTop) ;int w = lightRect.right - lightRect.left ;int h = lightRect.bottom - lightRect.top ;Bitmap bitmap = Bitmap.createBitmap(mBitmap, x, y, w, h) ;return bitmap ;}
PS: This is just a View that can be used to intercept images. In this case, we need to add some custom buttons to use a layout file to arrange the buttons. Here is a simple example:
<? Xml version = "1.0" encoding = "UTF-8"?> <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal"> <com. example. imagedemo. imageTailor android: id = "@ + id/tailor" android: layout_width = "match_parent" android: layout_height = "match_parent"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginLeft = "8dp" android: layout_marginRight = "8dp" android: layout_gravity = "bottom"> <Button android: id = "@ + id/complete" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "right" android: text = "complete"/> <Button android: id = "@ + id/cancel" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "right" android: text = "cancel"/> </LinearLayout> </FrameLayout>
*: I put key classes and key methods in my resources. If you need them, you can download them and run them directly to see the results. You can also see this Demo. This is mainly the implementation of the ImageTailor. java class. If you have any suggestions, please come up and learn together.
Why can I only take screenshots on my mobile phone or QQ screen capture on QQ?
I don't have the root permission, and I found that even if I have the root permission, QQ is also very difficult, at least my tablet is like this.
How can I use the screenshot function of the mobile phone QQ21 (Android), that is, how can I find it?
Enable settings and help pull down a screenshot function, select the box behind it, and then shake the mobile phone on the QQ interface to take screenshots. Then, when running QQ in the background, the mobile phone must have obtained the root permission for use.