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. PHP:熱門職位通過MySQL查詢
- 5. ListView的ImageView中的Android OnClickListener
- 6. Java Android動態更改ImageView的位置
- 7. 更改Android中ImageView位置的問題
- 8. 更改imageView在UITableViewCell中的位置
- 9. 更新Jlabel的新職位
- 10. ImageView:通過通用代碼更改一套imageviews src
- 11. Android - 在運行時更改ImageView位置
- 12. 如何通過nslayout更改ImageView的高度?
- 13. 如何通過單擊按鈕來更改ImageView中的圖片?
- 14. 更改ImageView
- 15. android listview gettag nullpointexception imageview onclicklistener
- 16. OnClickListener不適用於ImageView
- 17. QLabel職位不更新
- 18. Android通過圖像大小動態更改imageview大小
- 19. WordPress的 - 我怎樣才能通過AJAX獲取更多的職位?
- 20. 更改ImageView源不會正確更改ImageView的圖像
- 21. 通過他們的號碼檢索某些職位?
- 22. 問題與ImageView的onClickListener在Android
- 23. 使用onClickListener切換Imageview的圖像
- 24. Android ImageView的onClickListener不起作用
- 25. onClickListener on imageview開始新的意圖
- 26. 在butterknife中ImageView的onclicklistener不起作用
- 27. Mysql獲得過去3年的職位
- 28. 更改ListView中的ImageView
- 29. Android更改ImageView的方向
- 30. 更改ImageView的質量
任何代碼?你嘗試過什麼嗎?或者你只是來這裏要求其他人爲你做? – 2014-09-19 08:06:16
我創建了一個我的程序的圖像,但由於我的帳戶信譽較低,我無法上載它 – HassanUsman 2014-09-19 08:12:59
圖像可以幫助您...您有任何想要製作的應用程序的手嗎? 1活動或1行代碼... SOMETHING ...不是圖片(這不是一個藝術論壇) – 2014-09-19 08:38:35