我有图像,我希望它在布局中全屏显示。然后我想在图像的特定位置放置图标;问题是当我在多种屏幕尺寸上运行应用程序时,图标不在同一个地方。
唯一正常工作的是我在xml布局文件中使用固定宽度和高度的图像。但这对我来说还不够,我希望它能够满足全屏尺寸,尽管图像被拉伸,而且所有屏幕尺寸和密度都是如此。有人解决了这个问题吗?
实际上,我使用以下代码:
public class MainActivity extends Activity {
double [] X= {60,100,140};
double [] Y= {65,105,145};
//Parameter for icons on Image
RelativeLayout.LayoutParams [] params;
//Parameter for the Whole Screen
FrameLayout.LayoutParams ScreenParams;
RelativeLayout rl;
ImageView [] iv;
Context _cox;
RelativeLayout.LayoutParams paramss;
ImageView vvv;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_cox = this;
//Screen Density
double Density = getResources().getDisplayMetrics().densityDpi;
Log.v("Density is = ", ""+Density);
//Screen Height*Width
double _ScreenHeight = getResources().getDisplayMetrics().heightPixels;
double _ScreenWidth = getResources().getDisplayMetrics().widthPixels ;
Log.v("Screen Width before = ", ""+_ScreenWidth);
Log.v("Screen Height before = ", ""+_ScreenHeight);
double ScreenHeight = _ScreenHeight * (Density/160);
double ScreenWidth = _ScreenWidth * (Density/160);
Log.v("Screen Width after = ", ""+ScreenWidth);
Log.v("Screen Height after = ", ""+ScreenHeight);
ScreenParams = new FrameLayout.LayoutParams((int)ScreenWidth,(int)ScreenHeight);
rl = (RelativeLayout) findViewById(R.id.rel1);
iv = new ImageView[X.length];
params = new RelativeLayout.LayoutParams[X.length];
Toast.makeText(_cox, "Density is = "+Density, Toast.LENGTH_SHORT).show();
for(int i = 0 ; i<X.length ; i++)
{
Log.v("X before = ", ""+X[i]);
Log.v("Y before = ", ""+Y[i]);
X[i] = X[i]*(Density/160);
Y[i] = Y[i]*(Density/160);
Log.v("X after = ", ""+X[i]);
Log.v("Y after = ", ""+Y[i]);
iv[i] = new ImageView(this);
iv[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.star));
double xx = 21*(Density/160);
double yy = 21*(Density/160);
params[i] = new RelativeLayout.LayoutParams((int)xx,(int)yy);
params[i].leftMargin = (int) X[i];
params[i].topMargin = (int) Y[i];
iv[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(_cox)
.setTitle("")
.setMessage("Image: ")
.setPositiveButton("sdfsdfddddd",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).setNegativeButton("", null).show();
}
});
iv[i].setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float X = event.getX();
float Y = event.getY();
Toast.makeText(getApplicationContext(), "X = "+X+"\n"+"Y = "+Y, Toast.LENGTH_SHORT).show();
return false;
}
});
int arr[] = new int[2];
arr[0]=(int) X[i];
arr[1]=(int) Y[i];
iv[i].getLocationOnScreen(arr);
Toast.makeText(_cox, "", Toast.LENGTH_SHORT).show();
//Add ImageView item to the layout
rl.addView(iv[i], params[i]);
}
rl.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float X = event.getX();
float Y = event.getY();
Toast.makeText(getApplicationContext(), "X = "+X+"\n"+"Y = "+Y, Toast.LENGTH_LONG).show();
return false;
}
});
}
}
Android xml布局文件是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel1"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content" ___I used fixed length here 250dip___
android:layout_height="wrap_content" ___I used fixed length here 250dip___
android:scaleType="fitXY"
android:src="@drawable/world" />
</RelativeLayout>
答案 0 :(得分:1)
看看这个link也许它会帮助你
这是您的图片的多种尺寸:
ldpi: 120dpi
mdpi: 160dpi
hdpi: 240dpi
xhdpi: 320dpi
转换公式如下:
px = dp * (dpi / 160)
其中px是像素的最终图像尺寸,dp是与密度无关的单位所需的尺寸,dpi是目标密度。
使用mdpi图像的像素大小作为基线,简化此公式:
ldpi = mdpi * 0.75
hdpi = mdpi * 1.5
xhdpi = mdpi * 2.0
回到示例代码,如果你想要一个30dp×30dp的按钮,你应该为每个密度提供图像:
ldpi: 23px x 23px
mdpi: 30px x 30px
hdpi: 45px x 45px
xhdpi: 60px x 60px
答案 1 :(得分:1)
您的代码管理屏幕密度,但不管理屏幕尺寸。
如果您将背景设置为全屏,则会根据屏幕尺寸进行缩放。
您需要计算此比例并将其应用于边距。
但首先,我更改了布局,使背景变为全屏,并与左上角对齐(scaleType:fitStart)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel1"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitStart"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
然后你需要计算应用的比例:
private double getScaleForImage( Drawable d) {
int imgHeight = d.getIntrinsicHeight();
int imgWidth = d.getIntrinsicWidth();
double _ScreenHeight = getResources().getDisplayMetrics().heightPixels;
double _ScreenWidth = getResources().getDisplayMetrics().widthPixels ;
double imgRatio = (1.0f * imgWidth) / imgHeight;
double screenRatio = (1.0f * _ScreenWidth) / _ScreenHeight;
double scale = 1.0f;
if(imgRatio < screenRatio)// the scale can be found according to height
{
scale = (1.0f * _ScreenHeight) / imgHeight;
}
else // the scale can be found according to width
{
scale = (1.0f * _ScreenWidth) / imgWidth;
}
return scale;
}
然后在onCreate方法中:
double scale = getScaleForImage(backgroundDrawable);
并将其应用于您的计算中:
X[i] = X[i]*(Density/160)*scale;
Y[i] = Y[i]*(Density/160)*scale;
请注意,我的示例是使用屏幕大小,因此它假设您的应用程序以全屏模式运行,插入下一个代码以实现它:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
告诉我这是否有帮助。