A screen is required for the company's business needs. But the screenshot function is far from enough for the functional requirements of the project, we are doing an artboard software. After screenshots like QQ are needed, we can see our own defined tools. There are brushes, buttons and so on. Android comes with a very easy-to-do feature, and it's just enough to intent an implicit call. But he is the application of the system, the interface is fixed. Changes cannot be customized. There are many ways to implement it, and the following record is the way I implement it. 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 small dots), Ontouch method is to constantly change the light frame coordinate points. And then draw again.
:
I have broken this picture into the shape of the figure below.
We keep drawing rectangles and dots in the Ontouch.
Detailed code implementation of the main ideas:
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 modification 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 infer whether it is on the outside or inside of the rectangle (inference 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 to achieve the image interception, when we need to add some of their own defined button in. A layout file is used. Put the button 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.i Magetailor android:id= "@+id/tailor" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "/> <linearlayout android:layout_width=" match_parent "android:layout_height=" WR Ap_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"/> &L T Button android:id= "@+id/cancel" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "Android:layout_graVity= "Right" android:text= "Cancel"/> </LinearLayout> </FrameLayout>
*: Key classes and Key methods I put in my resources, the required friends can download direct execution to see the effect. 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's own definition screenshot function, similar to QQ screen screenshot