在Android上正确处理多个屏幕尺寸的问题已经有数千次讨论过。但是我找不到解决问题的办法。简而言之,我需要将自定义进度条与imageView对齐。我有3套imageView - ldpi(240x400),mdpi(320x480),hdpi(480x800)。我对齐的Java自定义查看下面的代码:然而将视图与不同分辨率的图像对齐
//get screen density
float density = getBaseContext().getResources().getDisplayMetrics().density;
//set the progress bar position according to screen density
if (density == 1.0f)
{
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)(height/13.94), (int)(height/13.94));
params.setMargins((int)(width/2.30), 0, 0, (int)(height/2.75));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}else if (density == 1.5f){
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)Math.round(height/14.13), (int)Math.round(height/14.13));
params.setMargins((int)Math.round(width/2.27), 0, 0, (int)Math.round(height/2.91));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}else if (density == 0.75f){
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)(height/14.88), (int)(height/14.88));
params.setMargins((int)(width/2.27), 0, 0, (int)(height/2.69));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}
一切工作罚款不同的屏幕尺寸上,当我试图检查480X854分辨率自定义视图的垂直对齐方式是不正确的。在相同的屏幕尺寸上使用480x800进行检查,并再次正常工作。我比去大跳跃和检查GalaxyTab和水平和垂直路线是错误的。现在我的第一个虽然是位图的宽度和高度是图像的不是实际调整大小的图像视图。所以我花了很多时间试图获得imageview的实际大小,甚至花费了viewTreeObserver,但结果都是相同的 - 正确的,不变的(未缩放的?)位图大小。所以积极的是,问题不在这里,我无法进一步解决。有没有人有一个想法,为什么对齐无法正常工作?
PS:作为在布局XML文件中的图像视图我有2个配置为长和notlong但这图像具有在两个相同的描述:
<ImageView
android:src="@drawable/cloking"
android:id="@+id/imageViewClk"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/imageViewProcess"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:layout_marginTop="60dp"
android:scaleType="fitXY">
</ImageView>
但是保持宽高比正是我所需要的!假设图像为237x424(hdpi版本),我知道我的自定义项目应该放在我的图像上(您在代码中看到计算结果)。然而,对于800而言,这是正确的,但对于854来说是正确的。我看到两种可能性:要么调整图像大小,要么以像素为单位获得“真实”大小(尽管我裁定只有一个但不是100% ),或图像没有调整大小(不需要因为它适合和dpi资源是正确的),但正确计算的定位像素在某种程度上不正确地用于布局中... –