Home  >  Article  >  Java  >  Android implements dragging function ~ organization ~ available for personal testing

Android implements dragging function ~ organization ~ available for personal testing

PHP中文网
PHP中文网Original
2017-08-02 11:26:481985browse

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; 
} 
};

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!

Statement:
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