2014-02-26 36 views
4

我想查找ImageView drawable的實際位置。目前它返回0,因爲ImageView被相對佈局調整大小。但是圖像視圖中的drawable並不是全部填充在相對佈局中。它填滿了整個寬度,並且它的頂部和底部都有空的空間。查找ImageView的實際位置

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relative_layout_main" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    > 

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/drawable"/> 

</RelativeLayout> 

請參閱附件截圖。我正在尋找紅色矩形的x和y以及它的寬度和高度。

enter image description here

+0

難道ü嘗試的imageView.getDrawable()你在屏幕上的座標圖像的getBounds()。左/右/底/頂值.. ?? – Neha

+0

它返回0,0或父位置 –

+0

如果它的匹配父寬度和父母是全屏幕的寬度開始將只有0 0 .. ?? – Neha

回答

6

你必須要等到意見進行了測量。使用OnGlobalLayoutListener

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    pubic void onGlobalLayout() { 
     int height = imageView.getHeight(); 
     int width = imageView.getWidth(); 
     int x = imageView.getLeft(); 
     int y = imageView.getTop(); 

     //don't forget to remove the listener to prevent being called again by future layout events: 
     imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
    } 
}); 

因爲你ImageView填滿整個屏幕,這將可能給設備的寬度爲寬度,窗口的高度爲高度。要獲得ImageView內的圖像的實際寬度和高度,RelativeLayout的高度match_parentImageView設置「設置的身高來wrap_content,並設置ImageViewlayout_gavitycenter_vertical

+0

感謝您的回答我會盡快檢查它,並回復它是如何工作的 –

+0

對不起,它返回的值不正確,因爲它是0,但是頂部有壞值 –

+0

頂部返回是什麼? –

0

您必須致電getTranslationX()getTranslationY()方法才能獲得視圖的真實座標。如果這些方法返回0值,請在您感興趣的視圖中附加一個點擊處理程序,並在onClick中調用上述方法,還可以使用getWidth和getHeight。

+0

謝謝你的回答我會盡快檢查它,並回復它是如何工作的 –

+0

你的答案仍然會返回0,0 –

+0

以及getWidth和hetHeight方法呢?如果你在聆聽者中打電話給他們,他們又回來了什麼? – karvoynistas

0

問題是:你想達到什麼目的? (我的意思是,爲什麼你需要把空的空間放在前面?)

你的ImageView被調整大小填充父母,這就是爲什麼你從推薦的0得到0。所以你的ImageView應該像圖片一樣大。將您的Imageview的高度更改爲wrap_content,並告訴您的圖像儘可能地調整大小。如果我沒有犯錯,你的RelativeLayout應該是綠色的,你應該看到你的圖像在中心(如果圖像小於parent_width,比你應該看到左側和右側的紅色背景)。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relative_layout_main" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:background="@android:color/holo_green_light" //green background for debugging 
> 

<ImageView 
    android:background="@android:color/holo_red_light" //red background for debugging 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"  //change hieght to content 
    android:layout_centerInParent="true"  //center in parent 
    android:scaleType="fitCenter"   //scale image to fit center 
    android:src="@drawable/drawable"/> 

現在你應該把你的ImageView位置@Neha建議

0

嘗試添加一個按鈕,點擊按鈕。在onclick()方法中獲取imageView的高度和寬度。每次你收到零,因爲你沒有對屏幕做任何事情。至少你已經傳遞了一些事件並處理事件。

+0

感謝您的回覆,但我需要此位置添加其他元素,而不是用於onClick方法 –

2

試試這個:這裏drawLeftX值和drawTopŸ值。

ImageView image = (ImageView)findViewById(R.id.imageview); 
Rect rt = image.getDrawable().getBounds(); 

int drawLeft = rt.left; 
int drawTop = rt.top; 
int drawRight = rt.right; 
int drawBottom = rt.bottom; 

你可以得到高度= drawBottom-drawTop寬度= drawRight-drawLeft

希望這會幫助你。

3

我想我有解決方案:

你必須重載onWindowFocusChanged活動的你在哪裏 以確保該意見已充氣的方法:

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    int array[] = new int[2]; 
    image.getLocationOnScreen(array); 
} 

當它完成,在數組變量中,我希望你有正確的座標。

PS:image是你的ImageView。

0

試試這個:

int[] imageCordinates = new int[2]; 
imageView.getLocationOnScreen(imageCordinates); 

int x = imageCordinates[0]; 
int y = imageCordinates[1]; 

//這個代碼將返回只要你想