Android custom screenshot function, similar to QQ screenshot

Source: Internet
Author: User

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)( - iconTop) ;int w = lightRect.right - lightRect.left ;int h = lightRect.bottom - ;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 = "" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal">

*: 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.