Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)


問題描述

Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)

I wrote a simple cards game where the user plays his card  doing TAP on one of the three imageviews (the cards) I'd like to add the possibility of playing the card by dragging the ImageView on the "table" (the table is another layout or simply another part of the screen).

I tryed to use the techinc indicated at http://blahti.wordpress.com/2011/01/17/moving-views-part-2/ but as it uses AbsoluteLayout, it introduces a lot of limitations on my current layout, more it requires adjustments depending to the device screen resolution where the app runs. I'd like to avoid this continue using -if possibile- the RelativeLayout. Maybe the starting point is extenting the ImageView adding the onTouch support but i couldn't reproduce the wished effect (drag) Any idea or suggestion or sample code? Thanks!

Just to give an idea, this is the draft of layout. The 3 cards below should be moved via drag.

<?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>

參考解法

方法 1:

here is nice example from link. i hope this will help you.

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

(by Lisitsouser609239)

參考文件

  1. Android: ImageView that extends onTouchEvent for Drag (CC BY-SA 3.0/4.0)

#imageview #touch-event #Android #extend #drag






相關問題

Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)

Android中的手勢 (Gestures in Android)

GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non-Uniformly)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

IndoorAtlas SDK 2.0:使用 Picasso 和自定義 ImageView (IndoorAtlas SDK 2.0: Using Picasso with custom ImageView)

當圖像的寬度小於顯示器的寬度時,如何在視圖尋呼機內對齊圖像視圖的中心? (How to align center an image view inside a view pager when the image's width is less than the display's?)

正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)

將旋轉的文本放在圖像視圖上 (Put rotated text on an imageview)

如何在底部設置圖像視圖 (how to set imageview in bottom)

我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)







留言討論