更改职位的ImageView通过onClickListener
答
我做你的requiremens的一个示范项目,这是怎样的代码看起来像:
布局:
<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>
活动代码:
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;
}
}
}
}
最终结果:
我用作图像的默认图标。
+0
上按钮无法正常工作。当我按下这个按钮时,img会下降为什么? – HassanUsman 2014-09-19 10:34:24
+0
我正在运行在这个时候项目和图像上升,当按钮按下si按...嗯...奇怪的......你确定你复制粘贴正确的代码? – 2014-09-19 10:36:14
任何代码?你尝试过什么吗?或者你只是来这里要求其他人为你做? – 2014-09-19 08:06:16
我创建了一个我的程序的图像,但由于我的帐户信誉较低,我无法上载它 – HassanUsman 2014-09-19 08:12:59
图像可以帮助您...您有任何想要制作的应用程序的手吗? 1活动或1行代码... SOMETHING ...不是图片(这不是一个艺术论坛) – 2014-09-19 08:38:35