Android custom screenshot function, similar to QQ 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. top + OFFSET & y
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:
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal">
</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.