我可以缩放 RelativeLayout 吗?
Can I scale a RelativeLayout?
我在 RelativeLayout
XML
文件中创建了一个 ImageButton
。
但是,当我签署我的应用程序时,它在每台设备上的显示方式不同。如何让它匹配所有屏幕尺寸?
注意:我尝试使用比例因子和draw
方法对其进行缩放,但没有成功。
这是我的 Activity
代码:
public class StartActivity extends Activity {
private ImageButton Play;
private ImageButton Rate;
private ImageButton Board;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_start);
Rate = (ImageButton) findViewById(R.id.imageButtonRate);
Rate.setY(55);
Rate.setX(40);
Board = (ImageButton) findViewById(R.id.imageButtonBoard);
Board.setY(55);
Board.setX(75);
Play = (ImageButton) findViewById(R.id.imageButtonPlay);
Play.setY(60);
Play.setOnClickListener(imageButtonPlayHandler);
}
View.OnClickListener imageButtonPlayHandler = new View.OnClickListener() {
public void onClick(View view) {
final Intent mainIntent = new Intent(StartActivity.this, MainActivity.class);
StartActivity.this.startActivity(mainIntent);
StartActivity.this.finish();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_start, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
您遇到的问题是屏幕尺寸不同和具有不同的像素密度。换句话说,您可以有 2 phone 具有相同物理尺寸的屏幕,但其中一个的像素比另一个多(因此具有更高的像素密度)。
由于代码如下,您不清楚:
Rate.setY(55);
这里“55”是像素数,在不同的屏幕上看起来不一样。在一个 phone 上,与具有两倍像素密度的相同尺寸的屏幕相比,它看起来只有一半的尺寸。那是因为它有固定数量的像素。
您需要使用 DisplayMetrics
and/or 等不同的 drawable
文件夹来存放适当大小的图像。如果您不动态计算屏幕尺寸并相应地调整图像,您将需要不止一张图像。
Android 这样做是因为它不应该假定图像在所有屏幕上都应该简单地 "scale up" 或 "scale down"。如果它做了那个假设,那么在很多情况下你必须做相反的事情 - 告诉 Android 到 不要 自动缩放,因为它看起来很奇怪或者具有不可接受的性能或图像质量。
我在 RelativeLayout
XML
文件中创建了一个 ImageButton
。
但是,当我签署我的应用程序时,它在每台设备上的显示方式不同。如何让它匹配所有屏幕尺寸?
注意:我尝试使用比例因子和draw
方法对其进行缩放,但没有成功。
这是我的 Activity
代码:
public class StartActivity extends Activity {
private ImageButton Play;
private ImageButton Rate;
private ImageButton Board;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_start);
Rate = (ImageButton) findViewById(R.id.imageButtonRate);
Rate.setY(55);
Rate.setX(40);
Board = (ImageButton) findViewById(R.id.imageButtonBoard);
Board.setY(55);
Board.setX(75);
Play = (ImageButton) findViewById(R.id.imageButtonPlay);
Play.setY(60);
Play.setOnClickListener(imageButtonPlayHandler);
}
View.OnClickListener imageButtonPlayHandler = new View.OnClickListener() {
public void onClick(View view) {
final Intent mainIntent = new Intent(StartActivity.this, MainActivity.class);
StartActivity.this.startActivity(mainIntent);
StartActivity.this.finish();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_start, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
您遇到的问题是屏幕尺寸不同和具有不同的像素密度。换句话说,您可以有 2 phone 具有相同物理尺寸的屏幕,但其中一个的像素比另一个多(因此具有更高的像素密度)。
由于代码如下,您不清楚:
Rate.setY(55);
这里“55”是像素数,在不同的屏幕上看起来不一样。在一个 phone 上,与具有两倍像素密度的相同尺寸的屏幕相比,它看起来只有一半的尺寸。那是因为它有固定数量的像素。
您需要使用 DisplayMetrics
and/or 等不同的 drawable
文件夹来存放适当大小的图像。如果您不动态计算屏幕尺寸并相应地调整图像,您将需要不止一张图像。
Android 这样做是因为它不应该假定图像在所有屏幕上都应该简单地 "scale up" 或 "scale down"。如果它做了那个假设,那么在很多情况下你必须做相反的事情 - 告诉 Android 到 不要 自动缩放,因为它看起来很奇怪或者具有不可接受的性能或图像质量。