如何拖动android的多张图片 [英] How to drag multiple images in android

查看:96
本文介绍了如何拖动android的多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以拖动canvas.Now一个图像我想拖多张图片。 是否有可能做到这一点?

I can drag one image on canvas.Now I want to drag more than one image. Is there any possibility to do that?

推荐答案

使用这种code这是非常有益的你,如果合适的有益cloick。

Use this code it is very helpfull for you, if helpfull cloick on right.

package edu.sbcc.cs123.draganddropbasic;

import android.app.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

  @SuppressWarnings("deprecation")
   public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
private ImageView letterView;                       // The     letter that the user drags.
private ImageView emptyLetterView;              // The letter outline that the user is supposed to drag letterView to.
private AbsoluteLayout mainLayout;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mainLayout = (AbsoluteLayout) findViewById(R.id.mainLayout);
    mainLayout.setOnTouchListener(this);
    letterView = (ImageView) findViewById(R.id.letterView);
    letterView.setOnTouchListener(this);

    emptyLetterView = (ImageView) findViewById(R.id.emptyLetterView);
}

private boolean dragging = false;
private Rect hitRect = new Rect();

@Override
/**
 * NOTE:  Had significant problems when I tried to react to ACTION_MOVE on letterView.   Kept getting alternating (X,Y) 
 * locations of the motion events, which caused the letter to flicker and move back and forth.  The only solution I could 
 * find was to determine when the user had touched down on the letter, then process moves in the ACTION_MOVE 
 * associated with the mainLayout.
 */
public boolean onTouch(View v, MotionEvent event) {
    boolean eventConsumed = true;
    int x = (int)event.getX();
    int y = (int)event.getY();

    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        if (v == letterView) {
            dragging = true;
            eventConsumed = false;
        }
    } else if (action == MotionEvent.ACTION_UP) {

        if (dragging) {
            emptyLetterView.getHitRect(hitRect);
            if (hitRect.contains(x, y))
                setSameAbsoluteLocation(letterView, emptyLetterView);
        }
        dragging = false;
        eventConsumed = false;

    } else if (action == MotionEvent.ACTION_MOVE) {
        if (v != letterView) {
            if (dragging) {
                setAbsoluteLocationCentered(letterView, x, y);
            }
        }
    }

    return eventConsumed;

}


private void setSameAbsoluteLocation(View v1, View v2) {
    AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
    setAbsoluteLocation(v1, alp2.x, alp2.y);
}


private void setAbsoluteLocationCentered(View v, int x, int y) {
    setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}


private void setAbsoluteLocation(View v, int x, int y) {
    AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
    alp.x = x;
    alp.y = y;
    v.setLayoutParams(alp);
}
  }

和XML是......

and Xml is......

 <?xml version="1.0" encoding="utf-8"?>
 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  
    android:id="@+id/mainLayout">

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/emptyLetterView" 
    android:src="@drawable/r_empty" 
    android:layout_x="200px" 
    android:layout_y="300px">

</ImageView>

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/letterView" 
    android:src="@drawable/r_filled" >

</ImageView>

这篇关于如何拖动android的多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆