這可能看起來像是一個非常常見的問題,但我似乎無法將視圖移動到屏幕中心。無法將視圖移動到屏幕中心(以編程方式,而不是佈局屬性) - Android
我有3次:
- 的背景圖像(中心作物)
- 一個黑/白格以顯現在屏幕的中心,用於調試目的。同樣中心作物
- 一個正方形,我想移動到屏幕中心正方形的中心,左上角。
我首先以像素爲單位獲取屏幕大小,將寬度和高度除以2得到中心的座標。然後通過center.x和center.y移動我的方形視圖 我期望廣場正好移動到屏幕的中心。
我還縮放了方形視圖,使其大小始終與背景圖像相同。
我想以編程方式解決這個問題(不只是以佈局屬性爲中心),因爲這只是第一步,我打算能夠在屏幕上的任意位置放置正方形。我試圖把它放在中心,因爲它似乎是最容易做的事情。
代碼
public class FullScreenActivity extends AppCompatActivity {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}
}
類白衣邏輯
public class AnimalPopupActivity extends FullScreenActivity {
private final String TAG = "AnimalPopup";
ImageView backgroundView;
ImageView square;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal_popup);
backgroundView = (ImageView)findViewById(R.id.background);
square = (ImageView) findViewById(R.id.square);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidthInPixels = metrics.widthPixels;
int screenHeightInPixels = metrics.heightPixels;
Log.i(TAG, "screen width pixels: "+screenWidthInPixels);
Log.i(TAG, "screen height pixels: "+screenHeightInPixels);
Point center = new Point(screenWidthInPixels/2, screenHeightInPixels/2);
Log.i(TAG, "center: "+center);
double ratioH = (double)screenWidthInPixels/100d;
Log.i(TAG, "ratioH: "+ratioH);
double ratioV = (double)screenHeightInPixels/100d;
Log.i(TAG, "ratioV: "+ratioV);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)(10*ratioH), (int)(10*ratioV));
layoutParams.leftMargin = center.x;
layoutParams.topMargin = center.y;
square.setLayoutParams(layoutParams);
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bentaye.app.games.AnimalPopupActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:srcCompat="@drawable/animal_popup_background" />
<ImageView
android:id="@+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="@drawable/animal_popup_grid" />
<ImageView
android:id="@+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/square" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
下面是我得到的是Nexus 5(1080×1920:420dpi)
用下面的日誌:
屏幕寬度像素:1080個
屏幕高度像素:1794
中心:點(540,897)
ratioH:10.8
ratioV:17。94
這裏是我所得到的在Nexus 10(2560 * 1600:xhdpi)
用下面的日誌:
屏幕寬度像素:1600
屏幕高度像素:2464
中心:點(800, 1232)
ratioH:16.0
ratioV:24.64
的2是關閉的幾個像素,我不是一個Android專家和清楚我做錯了什麼,但不能明白什麼。
任何幫助歡迎。乾杯!
編輯:我認爲這是計算考慮到狀態欄。這也解釋了爲什麼Nexus5上的差異更大,因爲狀態欄相對於屏幕高度更大。還有爲什麼這種差異僅影響垂直對齊。 一旦我把它整理出來,我會回來發佈代碼。
爲什麼是我的解決方案刪除了我最後一次編輯? – Bentaye