Java Android:拖放多个图像
我有这个“拖放”的东西 因此,基本上我们正在制作一个原型,让用户(孩子)将糖果拖放到罐子中。 下面的代码将只适用于一个糖果(1个图像),第二个图像将不会移动。我不知道为什么 任何答案都将受到高度赞赏,我希望你能解释一下,因为我在android开发方面是个新手。多谢各位 这些是我研究了一段时间的代码Java Android:拖放多个图像,java,android,drag-and-drop,Java,Android,Drag And Drop,我有这个“拖放”的东西 因此,基本上我们正在制作一个原型,让用户(孩子)将糖果拖放到罐子中。 下面的代码将只适用于一个糖果(1个图像),第二个图像将不会移动。我不知道为什么 任何答案都将受到高度赞赏,我希望你能解释一下,因为我在android开发方面是个新手。多谢各位 这些是我研究了一段时间的代码 package com.example.mathventure; import com.example.mathventure.R.drawable; import android.os.Bund
package com.example.mathventure;
import com.example.mathventure.R.drawable;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Rect;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
@SuppressLint("NewApi")
public class AddTutActivity extends Activity {
private static int candyInJar = 3;
private int candyOutJar = 2;
private int totalCandy;
//private String candyId;
private ImageView[] candies = new ImageView[11];
private ImageView[] candiesOut = new ImageView[candyOutJar];
private AbsoluteLayout tutLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_tut);
tutLayout = (AbsoluteLayout) findViewById(R.id.tutLayout);
tutLayout.setOnTouchListener(new touchListener());
candiesOut[0] = (ImageView) findViewById(R.id.candyOut1);
candiesOut[1] = (ImageView) findViewById(R.id.candyOut2);
candiesOut[0].setOnTouchListener(new touchListener());
candiesOut[1].setOnTouchListener(new touchListener());
totalCandy = candyInJar + candyOutJar;
for(int i=0;i<candies.length;i++){
candies[i] = new ImageView(this);
}
candies[0].setBackgroundResource(R.drawable.candy_jar_0);
candies[1].setBackgroundResource(R.drawable.candy_jar_1);
candies[2].setBackgroundResource(R.drawable.candy_jar_2);
candies[3].setBackgroundResource(R.drawable.candy_jar_3);
candies[4].setBackgroundResource(R.drawable.candy_jar_4);
candies[5].setBackgroundResource(R.drawable.candy_jar_5);
candies[6].setBackgroundResource(R.drawable.candy_jar_6);
candies[7].setBackgroundResource(R.drawable.candy_jar_7);
candies[8].setBackgroundResource(R.drawable.candy_jar_8);
candies[9].setBackgroundResource(R.drawable.candy_jar_9);
candies[10].setBackgroundResource(R.drawable.candy_jar_10);
candies[candyInJar] = (ImageView) findViewById(R.id.candyInJar);
}
private boolean dragging = false;
private Rect hitRect = new Rect();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_add_tut, menu);
return true;
}
class touchListener extends Activity implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();
int dragging = 0;
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == candiesOut[0]) {
dragging = 0;
eventConsumed = false;
}
if (v == candiesOut[1]){
dragging = 1;
eventConsumed = false;
}
} else if (action == MotionEvent.ACTION_UP) {
if (dragging == 0) {
candies[candyInJar].getHitRect(hitRect);
if (hitRect.contains(x, y)){
candiesOut[0].setImageDrawable(null);
candies[candyInJar].setImageResource(drawable.candy_jar_4);
}
}
else if(dragging == 1){
candies[candyInJar].getHitRect(hitRect);
if (hitRect.contains(x, y)){
candiesOut[1].setImageDrawable(null);
candies[candyInJar].setImageResource(drawable.candy_jar_5);
}
}
eventConsumed = false;
} else if (action == MotionEvent.ACTION_MOVE) {
if (v != candiesOut[0] && v != candiesOut[1]) {
if (dragging != 1) {
setAbsoluteLocationCentered(candiesOut[dragging], x, y);
}
}
}
return eventConsumed;
}
private void setAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}
private void setAbsoluteLocation(View v, int x, int y) {
AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
alp.x = x;
alp.y = y;
v.setLayoutParams(alp);
}
private void setSameAbsoluteLocation(View v1, View v2) {
AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
setAbsoluteLocation(v1, alp2.x, alp2.y);
}
}
}
package com.example.mathventure;
导入com.example.mathventure.R.drawable;
导入android.os.Bundle;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.graphics.Rect;
导入android.view.Menu;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnTouchListener;
导入android.widget.AbsoluteLayout;
导入android.widget.ImageView;
@SuppressLint(“新API”)
公共类AddTutActivity扩展了活动{
私有静态int-candyInJar=3;
私有int-candyOutJar=2;
私人糖果;
//私有字符串candyId;
private ImageView[]candies=新ImageView[11];
private-ImageView[]candiesOut=new-ImageView[candyOutJar];
私人绝对布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u tut);
tutLayout=(绝对布局)findViewById(R.id.tutLayout);
setOnTouchListener(新的touchListener());
candiesOut[0]=(ImageView)findViewById(R.id.candyOut1);
candiesOut[1]=(ImageView)findViewById(R.id.candyOut2);
candiesOut[0]。setOnTouchListener(新建touchListener());
candiesOut[1].setOnTouchListener(新建touchListener());
totalCandy=candyInJar+CandyYoutjar;
对于(inti=0;i,您可能希望查看此库和示例;
它会让你去创建一个图像,这是拖拉和捏,你可以添加任何你喜欢的简单添加一个拖拉
您可以按原样使用它,将其调整为只能拖动,或者从中吸取一些教训和灵感。我建议您更多地了解Android,否则您会经常感到沮丧。从找出这一行错误的原因开始,类touchListener扩展活动实现OnTouchListener
,它不应该扩展活动.Hi Simon。谢谢你的回答。我知道OnTouchListener的事情。如果你看到代码,我在代码末尾的一个内部类中实现了OnTouchListener。现在的问题是我只能移动一个图像,需要移动5个图像。谢谢。我不想听起来粗鲁,但听起来像“我不想费心去学习如何正确地完成这项工作,有人只要修改我的代码就行了。有很多问题,除非你学会了基础知识,否则你希望如何完成一个应用程序?另一个例子,这行代码做什么?candiesOut[1]。setOnTouchListener(new touchListener())
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddTutActivity"
android:id="@+id/tutLayout">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:fontFamily="123Marker"
android:text="3 candies + 2 candies = ?"
android:textColor="@android:color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="123Marker"
android:text="Below is a sample question on addition. Now, you need to move the 2 candies you wanted to add into the candy jar."
android:textColor="@android:color/black"
android:textSize="25sp"
android:background="#FF3300"/>
<ImageView
android:id="@+id/candyInJar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="178dp"
android:layout_x="149dp"
android:layout_y="167dp"
android:src="@drawable/candy_jar_3" />
<ImageView
android:id="@+id/candyOut1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="94dp"
android:layout_x="489dp"
android:layout_y="347dp"
android:src="@drawable/candy" />
<ImageView
android:id="@+id/candyOut2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="544dp"
android:layout_y="346dp"
android:src="@drawable/candy" />
</AbsoluteLayout>