Home > Java > javaTutorial > body text

Android implements dragging function ~ organization ~ available for personal testing

PHP中文网
Release: 2017-08-02 11:26:48
Original
1984 people have browsed it

To develop Android applications, you need to install eclipse, jdk and sdk first, and then create an Android phone simulator. Not much to say here.

The dragging function of Android appears in many scenarios, so how to implement this function has been modified and debugged by me.

The code is as follows:

First create two instances of imageView1 and imageView2 on the view interface, and then paste the following code into the java class file

private int screenWidth; 
private int screenHeight; 
private ImageView img1; 
private ImageView img2;
img1 = (ImageView) findViewById(R.id.imageView1); 
img2 = (ImageView) findViewById(R.id.imageView2);
DisplayMetrics dm = getResources().getDisplayMetrics(); 
screenWidth = dm.widthPixels; 
screenHeight = dm.heightPixels - 50;
img1.setOnTouchListener(movingEventListener); 
img2.setOnTouchListener(movingEventListener);
private OnTouchListener movingEventListener = new OnTouchListener() { 
int lastX, lastY; 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
        lastX = (int) event.getRawX(); 
        lastY = (int) event.getRawY(); 
        break; 
    case MotionEvent.ACTION_MOVE: 
        int dx = (int) event.getRawX() - lastX; 
        int dy = (int) event.getRawY() - lastY;
        int left = v.getLeft() + dx; 
        int top = v.getTop() + dy; 
        int right = v.getRight() + dx; 
        int bottom = v.getBottom() + dy; 
        // ÉèÖò»Äܳö½ç 
        if (left < 0) { 
            left = 0; 
            right = left + v.getWidth(); 
        }
        if (right > screenWidth) { 
            right = screenWidth; 
            left = right - v.getWidth(); 
        }
        if (top < 0) { 
            top = 0; 
            bottom = top + v.getHeight(); 
        }
        if (bottom > screenHeight) { 
            bottom = screenHeight; 
            top = bottom - v.getHeight(); 
        }
        v.layout(left, top, right, bottom);
        lastX = (int) event.getRawX(); 
        lastY = (int) event.getRawY();
        break; 
    case MotionEvent.ACTION_UP: 
        break; 
    } 
    return true; 
} 
};
Copy after login

Is it successful? , welcome to php Chinese website to learn programming

Article address: http://www.php.cn/java-article-375823.html

______________ PHP Chinese website_______________

The above is the detailed content of Android implements dragging function ~ organization ~ available for personal testing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!