Android 通过onClickListener更改ImageView的位置
我想创建一个程序,在该程序中,当我按下向上按钮时,球图像将移向顶部 当我按下Down按钮时,图像移动到底部Android 通过onClickListener更改ImageView的位置,android,eclipse,android-layout,android-activity,Android,Eclipse,Android Layout,Android Activity,我想创建一个程序,在该程序中,当我按下向上按钮时,球图像将移向顶部 当我按下Down按钮时,图像移动到底部 现在告诉我如何获得此输出。我为您的需求做了一个演示项目,代码如下所示: 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_par
现在告诉我如何获得此输出。我为您的需求做了一个演示项目,代码如下所示: 布局:
<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">
<ImageView
android:id="@+id/ballImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:id="@+id/upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up" />
<Button
android:id="@+id/downBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down" />
<Button
android:id="@+id/leftBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<Button
android:id="@+id/rightBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</LinearLayout>
</RelativeLayout>
最终结果是:
我将图像用作默认图标。有代码吗?你试过什么吗?或者你只是来这里请求其他人为你做这件事?我为我的程序创建了一个图像,但我无法上传它,因为我的accountImages的名声不太好。图像帮不上忙…你有想要制作的应用程序吗?1个活动或1行代码…某物…不是图片(这不是艺术论坛)图像是图像视图或在画布内?向上按钮工作不正常。当我按下这个按钮时,img会下降为什么?我正在运行项目,当按下按钮时图像会上升…嗯…奇怪…你确定你复制粘贴的代码正确吗?
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MyActivity extends Activity implements View.OnClickListener{
private Button upBtn;
private Button downBtn;
private Button leftBtn;
private Button rightBtn;
private ImageView ballImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_my);
init();
}
public void init(){
ballImage = (ImageView)findViewById(R.id.ballImage);
upBtn = (Button) findViewById(R.id.upBtn);
upBtn.setOnClickListener(this);
downBtn = (Button) findViewById(R.id.downBtn);
downBtn.setOnClickListener(this);
leftBtn = (Button) findViewById(R.id.leftBtn);
leftBtn.setOnClickListener(this);
rightBtn = (Button) findViewById(R.id.rightBtn);
rightBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.upBtn:{
Toast.makeText(this,"UP",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]);
ballImage.setY(locations[1]-1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
case R.id.downBtn:{
Toast.makeText(this,"DOWN",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]);
ballImage.setY(locations[1]+1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
case R.id.leftBtn:{
Toast.makeText(this,"LEFT",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]-1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
case R.id.rightBtn:{
Toast.makeText(this,"RIGHT",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]+1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
}
}
}