0
A
回答
1
我做你的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
相關問題
- 1. ImageView onClickListener更改圖像源
- 2. Parse.com ListView OnClickListener通過動態ImageView
- 3. ImageView onClickListener()
- 4. 更改imageview的位置
- 5. 通過{{#each}}獲取您的職位
- 6. Android ImageView隨機位置與OnClickListener
- 7. PHP:熱門職位通過MySQL查詢
- 8. 如何更改android中imageview的位置?
- 9. Soft Key Board更改ImageView的位置
- 10. Java Android動態更改ImageView的位置
- 11. 更改Android中ImageView位置的問題
- 12. 更改imageView在UITableViewCell中的位置
- 13. 動態更改imageView中的位圖,android
- 14. 通過ImageView定位的Android TextView
- 15. ListView的ImageView中的Android OnClickListener
- 16. 通過ImageView的Android ImageView OnTouchListener
- 17. 更新Jlabel的新職位
- 18. OnClickListener顯示的ImageView /片段
- 19. ImageView:通過通用代碼更改一套imageviews src
- 20. Android - 在運行時更改ImageView位置
- 21. 如何通過單擊按鈕來更改ImageView中的圖片?
- 22. 如何通過nslayout更改ImageView的高度?
- 23. 通過AngularJS指令更改佔位符?
- 24. 更改ImageView
- 25. QLabel職位不更新
- 26. 更新圖片職位
- 27. OnClickListener不適用於ImageView
- 28. ImageView onClickListener()不起作用
- 29. ImageView與OnClickListener不起作用
- 30. android listview gettag nullpointexception imageview onclicklistener
任何代碼?你嘗試過什麼嗎?或者你只是來這裏要求其他人爲你做? – 2014-09-19 08:06:16
我創建了一個我的程序的圖像,但由於我的帳戶信譽較低,我無法上載它 – HassanUsman 2014-09-19 08:12:59
圖像可以幫助您...您有任何想要製作的應用程序的手嗎? 1活動或1行代碼... SOMETHING ...不是圖片(這不是一個藝術論壇) – 2014-09-19 08:38:35