Android Animation-Drag

Source: Internet
Author: User

First look at the effect


In essence, the animation is a bit inappropriate, the exact word should be the processing of gestures, nonsense not much to see the code

Simpledragsample.java

public class Simpledragsample extends Activity {protected int contenttop; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.layout_simpledrag);D isplaymetrics dm = getresources (). Getdisplaymetrics (); final int screenwidth = Dm.widthPi xels; final int screenheight = Dm.heightpixels; LOG.E ("Width,height", ScreenWidth + "," + screenheight); final Button _button = (button) Findviewbyid (R.id.buttondrag); _b Utton.setontouchlistener (New Ontouchlistener () {int lastx, lasty; Boolean isdraging = false; @Overridepublic Boolean o  Ntouch (View V, motionevent event) {int ea = event.getaction (); switch (EA) {Case MotionEvent.ACTION_DOWN:isDraging = True ; lastx = (int) event.getrawx (); lasty = (int) Event.getrawy (); if (contenttop = = 0) {contenttop = GetWindow (). Findviewby Id (window.id_android_content). GetTop ()//Gets the total height of the status bar and title bar}break;case MotionEvent.ACTION_MOVE:if (isdraging) {int dx = ( int) event.GETRAWX ()-lastx; int dy = (int) Event.getrawy ()-lasty; int L = v.getleft () + dx; int b = v.getbottom () + dy; int r = v.getright () + dx; int t = v.gettop () + dy;//judgment exceeds screen if (l<0) {L = 0; r = v.getwidth ();} if (t<0) {t = 0; b = v.getheight ();} if (r>screenwidth) {r = screenwidth; l = R-v.getwidth ();} if (b>screenheight-contenttop) {b = screenheight-contenttop; t = B-v.getheight ();} V.layout (L, T, R, b); lastx = (int) event.getrawx (); lasty = (int) event.getrawy (); V.postinvalidate ();} Break, Case MotionEvent.ACTION_UP:isDraging = False, break;d efault:break;} return false;}}) ;}}
Layout_simpledrag.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/buttondrag" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:text=" button "         >    </button></ Relativelayout>

The code is relatively not too complex, here is not more explanation, only the development process encountered problems

1.view.getleft, View.getright, View.gettop, view.getbottom are all relative to the parent view, where the parent view of the button is the relativelayout we define, is not included in the top status bar and the title bar.

This article explains in great detail

http://blog.csdn.net/androiddevelop/article/details/8373782

2. Test my Xiaomi Phone (480*854) with the following code to get a value of 320*569

Displaymetrics DM = getresources (). Getdisplaymetrics ()  ; final int screenwidth = Dm.widthpixels  ; final int ScreenHeight = Dm.heightpixels  ;

Workaround:

Note: density is not equal to 1 of the case, you need to set targetsdkversion between 4-9, for example

<USES-SDK android:minsdkversion= "3" android:targetsdkversion= "ten"/>

you need to add the Supports-screens node to the project's Androidmanifest.xml file, specifically as follows:
<supports-screens            android:smallscreens= "true"            android:normalscreens= "true"            Android:largescreens = "true"            android:xlargescreens= "true"            android:resizeable= "true"            android:anydensity= "true"/>
The study found that if no multi-resolution support was set, the Android system would convert the 240x320 low density (120) andx480 above the high -density dimensions to the medium density (160) corresponding size

3. Three ways to get screen resolution

(1) Direct int screenwidth = Getwindowmanager (). Getdefaultdisplay (). getwidth (); int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); (2) Get Displaymetrics object by Getresources (), displaymetrics dm = new Displaymetrics ();d m = Getresources (). Getdisplaymetrics (); screenwidth = Dm.widthpixels;screenheight = Dm.heightpixels; (3) obtain DM = new Displaymetrics () by Getwindowmanager (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); ScreenWidth = Dm.widthpixels;screenheight = Dm.heightpixels;


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.