2014-09-19 64 views
0

我想創造一個當我按下向上按鈕球像一家走向頂部1px的
程序,當我按下按鈕圖像移至底部更改職位的ImageView通過onClickListener

現在告訴我我如何得到這個輸出。

+0

任何代碼?你嘗試過什麼嗎?或者你只是來這裏要求其他人爲你做? – 2014-09-19 08:06:16

+0

我創建了一個我的程序的圖像,但由於我的帳戶信譽較低,我無法上載它 – HassanUsman 2014-09-19 08:12:59

+1

圖像可以幫助您...您有任何想要製作的應用程序的手嗎? 1活動或1行代碼... SOMETHING ...不是圖片(這不是一個藝術論壇) – 2014-09-19 08:38:35

回答

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; 
     } 


    } 
} 
} 

最終結果:

enter image description here

我用作圖像的默認圖標。

+0

上按鈕無法正常工作。當我按下這個按鈕時,img會下降爲什麼? – HassanUsman 2014-09-19 10:34:24

+0

我正在運行在這個時候項目和圖像上升,當按鈕按下si按...嗯...奇怪的......你確定你複製粘貼正確的代碼? – 2014-09-19 10:36:14