我试图让ImageView跟随另一个ImageView,改变它的坐标。我的运动方法是:
public void move(double zx,double zy){
if (zx < a.getLeft()) {
if (a.getLeft() - zx > 0) {
a.setLeft(a.getLeft() - 1);
} else
a.setLeft(a.getLeft()+1);
}
if (zx > a.getLeft()) {
if (a.getLeft() - zx > 0) {
a.setLeft(a.getLeft() - 1);
} else
a.setLeft(a.getLeft() - 1);
}
if (zy > a.getTop()) {
if (a.getTop() - zy > 0) {
a.setTop(a.getTop() - 1);
} else
a.setTop(a.getTop() + 1);;
}
if (zy < a.getTop()) {
if (a.getTop() - zy > 0) {
a.setTop(a.getTop() - 1);
} else
a.setTop(a.getTop() + 1);
}
}
OnCreate中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fase);
a = (ImageView)findViewById(R.id.a);
b = (ImageView)findViewById(R.id.b);
move(b.getLeft(), b.getTop());
}
&#34;&#34;和&#34; b&#34;是ImageViews,应该遵循b。我正确地声明了两个ImageView,并且该方法应该可以工作。而不是它,ImageView&#34; a&#34;自应用程序启动以来保持相同的位置。怎么了?方法移动是否在错误的位置声明(onCreate)?我也试图设置&#34; a&#34;的垂直位置。在屏幕的另一部分,用于测试它是否可行。但它没有(并且根据logCat它的垂直位置已经改变)。我发现关于移动图像的教程是基于拖动它们的,但我还没有发现任何关于另一种运动的东西,就像这样。
有我的布局(activity_fase.xml):
<RelativeLayout 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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="thegame.app.Fase"
android:background="@drawable/seapaint">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/b"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/a"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/a"
android:focusable="true"
android:clickable="false"
android:layout_marginBottom="79dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_above="@+id/b"
android:layout_alignLeft="@+id/a"
android:layout_alignStart="@+id/key"
android:layout_marginBottom="67dp"
android:src="@drawable/key"
android:layout_marginLeft="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/points"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我是关于Android开发的新手,我不知道相对布局和线性布局之间的区别。
答案 0 :(得分:0)
问题已解决
可以通过按代码设置relativeLayout并将其添加到leftMargin和topMargin的参数来更改ImageView在屏幕任何区域的位置。例如:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // 80只是我选择的一些。有很多种可能性
params.leftMargin = 200;
params.topMargin = 400;
a.setLayoutParams(PARAMS);
现在imageView的坐标是(200,400)。可以进行连续的改变。如果您创建一个每0.05秒调用一次的计时器和一个向leftMargin和topMargin添加1的任务,则图像的位置将成功更改。