gpt4 book ai didi

java - ImageView 的 onTouch 移动?

转载 作者:行者123 更新时间:2023-12-01 14:33:57 24 4
gpt4 key购买 nike

我对 Android 开发还比较陌生。

但我想知道是否可以移动这些图像,以便它们可以在网格中相互交换。因为它是一个棋盘游戏,我正在尝试实现。

这就是它的样子:/image/rnyta.png

图像数组的代码:

    private Integer[] mThumbIds = {
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg,
R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.empty, R.drawable.peg, R.drawable.peg, R.drawable.peg,
R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.peg,
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
R.drawable.blank, R.drawable.blank, R.drawable.peg, R.drawable.peg, R.drawable.peg, R.drawable.blank, R.drawable.blank,
};

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="40dp"
android:columnWidth="50dp"
android:numColumns="7"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />

</LinearLayout>

最佳答案

如果您不需要拖放,这应该很简单。

private View firstClicked = null;

private OnClickListener swapImages = new OnClickListener() {

@Override
public void onClick(View v) {
if(firstClicked != null){
LayoutParams p1 = firstClicked.getLayoutParams();
LayoutParams p2 = v.getLayoutParams();
v.setLayoutParams(p1);
firstClicked.setLayoutParams(p2);
firstClicked = null;
}
else
firstClicked = v;
}
};

这样您只需交换它们的位置,而不是其中的图像。

编辑:但如果这干扰了你的游戏逻辑,你只需交换而不是布局参数,你只需交换它们的图像。

编辑:此代码交换您的绘图,您需要创建自己的逻辑

package com.example.stackoverflow;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.drawable.Drawable;


public class Game extends Activity {

private ImageView firstClicked = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Game.this, "" + position, Toast.LENGTH_SHORT).show();
if(firstClicked != null){
ImageView v2 = (ImageView)v;
Drawable d2 = v2.getDrawable();
Drawable d1 = firstClicked.getDrawable();
v2.setImageDrawable(d1);
firstClicked.setImageDrawable(d2);
firstClicked = null;
}
else {
firstClicked = (ImageView)v;
}
}

});
}
}

希望这对您有所帮助并祝您工作愉快。

关于java - ImageView 的 onTouch 移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com