2011-07-05 1 views
1

ユーザーがカードをプレイするシンプルなカードゲームを書いた 3つの画像ビュー(カード)の1つでTAPを実行しています 「テーブル」 (テーブルは別のレイアウトまたは単に画面の別の部分)上のImageViewをドラッグすることによって表示されます。私はtechincを使用するようにしようと試みAndroid:OnTouchEvent for dragを拡張したImageView

http://blahti.wordpress.com/2011/01/17/moving-views-part-2/ で示されているが、それはAbsoluteLayoutを使用して、それは私の現在のレイアウト上の制約 の多くを紹介し、より多くのそれは、アプリの実行デバイスの画面解像度に に依存調整が必要です。 これを避けたいのですが、 -if possibile- 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> 
+0

私は決定的なアプローチを変え、私の問題を解決し、使用を開始Blahtiによって提供される方法。彼に感謝! http://blahti.wordpress.com/2011/01/17/moving-views-part-2/ – Lisitso

答えて

1

linkの例があります。これがあなたに役立つことを願っています。

のres /レイアウト/ 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 /ビーニー/例/ 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; 
    } 
} 
+0

こんにちは。これは良い出発点です。 明確にする必要があることはいくつかあります。指を離した後(ACTION_UP)、ボタンはもう動くことができません。 さらに、複数の要素を同時に移動可能にする必要があります(アプリケーションのドラフトのレイアウトを参照してください)。この例では1つしかなく、私は必要な変更を加えることができません:-(それを改善するための提案コード。 – Lisitso

関連する問題