1

ユーザーが 3 つのイメージ ビュー (カード) の 1 つで TAP を実行してカードをプレイする単純なカード ゲームを作成しましたレイアウトまたは単に画面の別の部分)。

http://blahti.wordpress.com/2011/01/17/moving-views-part-2/に示されている技術を使用しようとしました が、AbsoluteLayout を使用しているため、現在のレイアウトに多くの制限が生じます。アプリが実行されているデバイスの画面解像度に応じて調整が必要です。可能であれば、RelativeLayoutを使用し続けることを避けたいと思います。たぶん、開始点は onTouch サポートを追加して ImageView を拡張することですが、希望する効果を再現できませんでした (ドラッグ) アイデア、提案、またはサンプル コードはありますか? ありがとう!

参考までに、これはレイアウトのドラフトです。下の 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>
4

1 に答える 1

1

これはリンクからの良い例です。これがお役に立てば幸いです。

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/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;
  }
}
于 2011-07-05T16:48:21.833 に答える