"Android" uses the redraw of custom view to implement drag movement to get the dimensions of the component

Source: Internet
Author: User
Tags gety

Here's an app that shows how to use the redraw of a custom view to implement a drag move to get the dimensions of the component.

For example, touch drag, or tap the screen to move the picture. If you encounter text, a hint pops up.


This is achieved using a redraw of the custom view. is to click on the screen once, this custom view will be redrawn once. Although there is only one image in this custom view.

1, first define each font file in Res\values\strings.xml, after the following changes:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name=    "app_name" > Drag move </string >    <string name= "action_settings" >Settings</string>    <string name= "TextView1" > Touch white screen anywhere to move pictures </string></resources>
2, after modifying the Mainactivity.java layout file res\layout\activity_ Main.xml, in the frame layout is placed inside a textview, this textview deviate from the head 100sp, give an ID, in the Mainactivity.java will be through the Java code, get its absolute size and position in the device. Different devices have different resolutions for the SP. Instead, custom view,imagelayout are generated by code in a moment. There is no need to decorate here.

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/framelayout1"    android:layout_width= "match_parent"    android:layout_height= "match_parent" >    <textview        Android:id= "@+id/textview1"        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android:layout_margintop= "100sp"        android:text= "@string/textview1"        android:textsize= "24sp"/> </FrameLayout>
3. Customize a custom view,imagelayout like "Android" Custom view, canvas canvases and paint brushes (click to open link). After the src new Imagelayout.java, modify its code as follows, initialize the brush, with the image file, the image file resource specified as the own app icon. Define an X, y that can be manipulated by other classes, methods, and in the OnDraw method, this image is drawn according to this x, Y.

Finally, the last drawn image is recycled.

Package Com.touch_move;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.view.view;public class Imagelayout extends View {public float x;public float y;private Paint paint;public Bitmap b Itmap;public Imagelayout (Context context) {Super (context);p aint = new paint (); bitmap = Bitmapfactory.decoderesource ( This.getresources (), r.drawable.ic_launcher);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Canvas.drawbitmap (bitmap, x, y, paint); if ( Bitmap.isrecycled ()) {bitmap.recycle ();}}}
4. Then is the method of Mainactivity.java modification, this method is divided into three parts, the first part is to register the various components and add events, nothing to say, the second part is the implementation of the custom layout imagelayout touch Listener, and finally the size of the individual components can not be obtained in the OnCreate method The size of the component is determined by the Android mechanism, when the component is not fully generated and the size will be 0.

Note that when you get the component, you get the size of the picture Imagelayout.bitmap in the custom layout, not the custom layout imagelayout, and the custom layout is the entire frame layout, so you click anywhere in the app, and the touch event takes effect.

In the implementation of touch events, because the default is the touch point is the upper left corner, drawing the avatar, so to make the picture size of the compilation, so that the center of the image generated at the cursor click.

The return value of the touch event is false, otherwise the touch event is returned two times.

Package Com.touch_move;import Android.os.bundle;import Android.view.motionevent;import android.view.View;import Android.view.view.ontouchlistener;import Android.widget.framelayout;import Android.widget.textview;import Android.widget.toast;import Android.app.activity;public class Mainactivity extends Activity implements Ontouchlistener {private framelayout framelayout1;private TextView textview1;private imagelayout imagelayout;private int textview1left;private int textview1top;private int textview1width;private int textview1height;private int bitmapwidth;private int bitmapheight; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); frameLayout1 = (framelayout) Findviewbyid ( R.ID.FRAMELAYOUT1); textView1 = (TextView) Findviewbyid (r.id.textview1); imagelayout = new Imagelayout (this); Imagelayout.setontouchlistener (this); Framelayout1.addview (imagelayout);} @Overridepublic Boolean OnTouch (View view, Motionevent MotionEvent) {imagelayout.x = Motionevent.getx ()-bitmapwidth/2;imagelayout.y = Motionevent.gety ()-Bitmapheight/2;image Layout.invalidate (); if (Textview1left < Motionevent.getx ()) && (Motionevent.getx () < (Textview1left + Textview1width) && (Textview1top < Motionevent.gety ()) && (Motionevent.gety () < (Textview1top + Textview1height)) {Toast.maketext (this, "the picture and the text hit together ...", Toast.length_short). Show (); return false;} @Overridepublic void Onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus); bitmapwidth = ImageLayout.bitmap.getWidth (); bitmapheight = ImageLayout.bitmap.getHeight (); textview1left = Textview1.getleft (); Textview1top = Textview1.gettop (); textview1width = Textview1.getwidth (); textview1height = TextView1.getHeight ();}}
Use the Imagelayout.invalidate () method to re-execute all the methods of the Imagelayout.java of the custom layout class, which is to redraw the custom layout once. This enables the movement of this component.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Android" uses the redraw of custom view to implement drag movement to get the dimensions of the component

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: info-contact@alibabacloud.com 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.