【问题标题】:Android: ImageView that extends onTouchEvent for DragAndroid:扩展 onTouchEvent 以进行拖动的 ImageView
【发布时间】:2011-09-28 23:26:10
【问题描述】:

我写了一个简单的纸牌游戏,用户玩他的牌 在三个图像视图(卡片)之一上进行 TAP 我想增加打牌的可能性 通过拖动“表”上的 ImageView (表格是另一种布局,或者只是屏幕的另一部分)。

我尝试使用在 http://blahti.wordpress.com/2011/01/17/moving-views-part-2/ 但是由于它使用 AbsoluteLayout,它引入了很多限制 在我目前的布局上,更多需要调整,具体取决于 到应用程序运行的设备屏幕分辨率。 我想避免这种情况继续使用 -如果可能的话-RelativeLayout。 也许起点是扩展 ImageView 添加 onTouch 支持,但我无法重现 想要的效果(拖动) 任何想法或建议或示例代码? 谢谢!

只是给个想法,这是布局的草稿。 下面的 3 张卡片应该通过拖动来移动。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/player_cards_layout" 
    android:layout_width="fill_parent" android:layout_height="150dip"
    android:gravity="center_horizontal"
    android:background="#55335588"
    android:layout_above="@+id/player_cards_layout" 
>
    <ImageView android:id="@+id/img_pc_card_1"
        android:layout_width="100dip" android:layout_height="150dip"
        android:src="@drawable/icon"
    />
</RelativeLayout>

<RelativeLayout android:id="@+id/player_cards_layout" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:background="#336699"
>

    <ImageView android:id="@+id/img_user_card_1"
        android:layout_width="100dip" android:layout_height="150dip"
        android:src="@drawable/icon"
    />      
    <ImageView android:id="@+id/img_user_card_2" 
        android:layout_width="100dip" android:layout_height="150dip"
        android:src="@drawable/icon"
        android:layout_toRightOf="@+id/img_user_card_1"
    />      
    <ImageView android:id="@+id/img_user_card_3" 
        android:layout_width="100dip" android:layout_height="150dip"
        android:src="@drawable/icon"
        android:layout_toRightOf="@+id/img_user_card_2"
    />
</RelativeLayout>

</RelativeLayout>

【问题讨论】:

标签: android imageview drag extend touch-event


【解决方案1】:

这是来自link 的好例子。我希望这会对你有所帮助。

res/layout/main.xml

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

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/btn"
        android:text="Drag Me"></Button>
</FrameLayout>

src/com/beanie/example/Home.java

package com.beanie.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;

public class Home extends Activity implements OnTouchListener {

private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;

private Button btn;
private FrameLayout layout;
private int status;
private LayoutParams params;

private ImageView image;

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

    layout = (FrameLayout) findViewById(R.id.LinearLayout01);
    // layout.setOnTouchListener(this);

    btn = (Button) findViewById(R.id.btn);
    btn.setDrawingCacheEnabled(true);
    btn.setOnTouchListener(this);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

}

@Override
public boolean onTouch(View view, MotionEvent me) {
    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        status = START_DRAGGING;
        image = new ImageView(this);
        image.setImageBitmap(btn.getDrawingCache());
        layout.addView(image, params);
    }
    if (me.getAction() == MotionEvent.ACTION_UP) {
        status = STOP_DRAGGING;
        Log.i("Drag", "Stopped Dragging");
    } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
        if (status == START_DRAGGING) {
            System.out.println("Dragging");
            image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
            image.invalidate();
        }
    }
    return false;
  }
}

【讨论】:

  • 您好,非常感谢您的快速回复!这是一个很好的起点。我需要澄清几件事:释放手指(ACTION_UP)后,按钮不再可移动。此外,我需要同时拥有多个可移动元素(请参阅我的应用程序草稿布局):在示例中只有一个,我无法进行必要的更改 :-( 我会感谢改进该代码的建议.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多