Because the business needs of the company needs to take a screenshot of a screen, but the function of the screenshot is far from enough to meet the functional requirements of the project, we are doing an artboard software, the need to screen like QQ, you can see our custom tools, there are brushes, buttons and so on. Android comes with a very simple function, only need to intent implicit call is completely enough, but he is the application of the system, the interface is fixed, can not be customized modification. There are many ways to implement the method, and the following record the method I implemented. I am such a way of thinking, rewrite a view component, in the OnDraw is only responsible for not drawing graphics (including translucent four rectangles, bright box rectangle, bright box on the four dots), Ontouch method is to constantly modify the light frame coordinate points. and redraw.
:
I have broken this picture into the shape of the figure below. We keep drawing rectangles and dots in the Ontouch.
Specific code to achieve the main idea:
1, picture drawing method:
@Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//do not rewrite the picture cannot appear if (mbitmap!=null) {//drawnolight ( Canvas); Canvas.drawbitmap (Mbitmap, Iconleft, Icontop, p_picture);//Draw the highlighted border drawrect (canvas); if (Isdown) drawcircle ( canvas);}}
2, Picture coordinates change method:
@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;//need to determine whether it is outside or inside the rectangle (to determine whether to move or scale) if (x> Lightrect.left+offset && x<lightrect.right-offset && y>lightrect.top+offset && y< Lightrect.bottom-offset) {//is the moving state Ismove = true; Isscale = false;} else if (X<lightrect.left-offset | | y<lightrect.top-offset | | x>lightrect.right+offset | | y> Lightrect.bottom+offset) {ismove = false; Isscale = false;} else {ismove = false; Isscale = true;//Zoom point = Getscalepoint (StartX, starty);} if (!isscale) Isdown = false; Break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:isDown = true; break; case M OtionEvent.ACTION_MOVE:if (Ismove) {//move float dx = x-startx; float dy = y-starty; movelightrect (dx, dy); StartX = x; s Tarty = y; isdown = false;} if (isscale) {float dx = x-startx; float dy = y-starty;Resetlightrect (dx, dy); startX = x; starty = y;} Break;d Efault:break;} Invalidate (); return true;}
3, the method of image interception:
Public Bitmap Getbitmap () {int x = (int) (lightrect.left-iconleft), int y = (int) (lightrect.top-icontop), int w = Light rect.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 can achieve image interception, when we need to add some custom buttons to come in, the use of a layout file, the button is placed in. To give 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_pare NT "android:layout_height=" Match_parent "/> <linearlayout android:layout_width=" Match_ Parent "android:layout_height=" wrap_content "android:layout_marginleft=" 8DP "Android:layout_marginrig ht= "8DP" android:layout_gravity= "Bottom" > <button android:id= "@+id/complete" and Roid: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_co" Ntent "android:layout_height="Wrap_content" android:layout_gravity= "right" android:text= "Cancel"/> </linearlayout> ; </FrameLayout>
*: Key classes and Key methods I put in my resources, need friends can download direct run to see the effect. You can also see this demo. The main imagetailor.java is the implementation of this class. Do you have any suggestions to come and learn together.
Android Custom screenshot function, similar to QQ screenshot