2010-11-21 126 views
0

我想要得到一個圖像移動到某些x和y與此應用程序。我已經嘗試與layoutParams通過搜索,但它不工作在這裏是我的完整的應用程序代碼。LayoutParams移動x和y

main.xml中

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:screenOrientation="portrait"> 

    <ImageView android:id="@+id/ImageView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:scaleType="fitXY" android:src="@drawable/myImage1" android:layout_marginTop="5dp"></ImageView> 
    <FrameLayout android:id="@+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/marker" android:src="@drawable/marker"></ImageView> 
    </FrameLayout> 
</LinearLayout> 

MYAPP的Java

ImageView marker; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     marker=(ImageView)findViewById(R.id.marker); 
     FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(35, 90, 0, 0); 
     marker.setLayoutParams(lp); 
     marker.invalidate(); 

     } 
} 

我的最終目標是要在不同的位置

+0

仍然卡住this.Thanks – 2010-11-21 07:14:29

回答

0

在myImage1移動標記圖像可以拿到手機與大小此代碼:

DisplayMetrics metrics = new DisplayMetrics(); 
    ((Activity)c).getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    screenWidth = ((int)metrics.xdpi); 
    screenHeight = ((int)metrics.ydpi); 

您可以使用setPadding,通過上面的代碼可以獲得手機的寬度和高度。我也有定位問題,這是解決方案。

+0

嗯,好吧,我是一個假人,我想你可以詳細說明如何設置圖像的位置一點點。 – 2010-11-21 06:55:55

+0

有些東西只是看起來不正確。在普通模擬器中記錄screenWidth screenHeight給出160和160. – 2010-11-21 07:13:17

+0

你應該如何使用它? – 2010-11-21 07:13:33