Home
› Uncategorized
The View class provide the methods to computes the coordinates:
In my experience, the returned x location is correct, but the y location is always error with a fixed offset. The offset various depends on devices and configuration.
To correct it, we can get the offset using the code, after view displayed:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int offsetY = displayMetrics.heightPixels - mainScreen.getMeasuredHeight();
- getLocationInWindow (int[] location): Computes the coordinates of the view in its window.
- getLocationOnScreen (int[] location): Computes the coordinates of the view on the screen.
In my experience, the returned x location is correct, but the y location is always error with a fixed offset. The offset various depends on devices and configuration.
To correct it, we can get the offset using the code, after view displayed:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int offsetY = displayMetrics.heightPixels - mainScreen.getMeasuredHeight();
package com.example.androidoffsetgetlocation; import android.os.Bundle; import android.app.Activity; import android.util.DisplayMetrics; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { LinearLayout mainScreen; ImageView object; TextView textOnCreate, textOnWindowFocusChanged; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainScreen = (LinearLayout)findViewById(R.id.mainscreen); object = (ImageView)findViewById(R.id.object); textOnCreate = (TextView)findViewById(R.id.textview1); textOnWindowFocusChanged = (TextView)findViewById(R.id.textview2); readLocation(textOnCreate, "onCreate()"); } @Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); readLocation(textOnWindowFocusChanged, "onWindowFocusChanged()"); } private void readLocation(TextView tv, String status){ DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int offsetX = displayMetrics.widthPixels - mainScreen.getMeasuredWidth(); int offsetY = displayMetrics.heightPixels - mainScreen.getMeasuredHeight(); int[] locationInWindow = new int[2]; object.getLocationInWindow(locationInWindow); int[] locationOnScreen = new int[2]; object.getLocationOnScreen(locationOnScreen); tv.setText( "\n" + status +"\n" + "getLocationInWindow() - " + locationInWindow[0] + " : " + locationInWindow[1] + "\n" + "getLocationOnScreen() - " + locationOnScreen[0] + " : " + locationOnScreen[1] + "\n" + "Offset x: y - " + offsetX + " : " + offsetY); } }
<LinearLayout 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" android:orientation="vertical" android:id="@+id/mainscreen"> <ImageView android:id="@+id/object" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
[Gameloft] Wonder Zoo S40, S60v3, S60v5, Symbian^3, BellaGameloft has just released this new great game " Wonder Zoo " in 2012, a zoo management g…
-
Andy (Siri for Android) v4.3 APKAndy is a voice controlled access to encyclopedic information on a vast array of topics. Ask anyt…
-
TNA Wrestling iMPACT v1.0.2 (Paid Version) Android Apk Game DownloadTNA Wrestling iMPACT v1.0.2 Requirements: 2.0.1 Strike, stomp, grapple and throw your way to t…
-
Jelly bean keyboard PROAndroid Jelly Bean Keyboard from Android 4.1 ported to work on your device! Jelly Bean Keyboa…
-
Muffin Knight v1.7 (Paid Version) Android Apk Game DownloadFootball Manager Handheld 2012 v3.5 Requirements: Android v2.2+ Overview: The World’s best-sell…
-
Apple iPhone 5 FeaturesIf we believe the rumours, Apple's next generation phone, iPhone 5 will be launched in October…
-
HTC One X (Flagship Device)HTC One X (Flagship Device) The HTC One X is HTC‘s flagship device and currently the most powerful…
Post a Comment
Post a Comment