我正在制作一个Android应用程序,我希望将图像从一个单元格移动到另一个单元格。我正在关注here链接。问题是我正在使类扩展活动 虽然它扩展了视野并在那里实施。见下文
我的班级
public class GameActivity extends Activity
implements OnTouchListener
示例中的类
public class TouchExampleView extends View
在OnDraw()方法上显示错误。我可以理解它在视图类中的定义,但我怎么能实现它。
答案 0 :(得分:0)
活动档案。
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});
XML文件: -
<?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">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageview1"
android:src="@drawable/image1" />
<ImageView
android:layout_width="100sp"
android:layout_height="100sp"
android:id="@+id/imageview2"
android:src="@drawable/image2" />
</RelativeLayout>
看到这个