我有图像,我希望它在布局中全屏显示。然后我想将图标放在图像的特定位置;问题是当我在多个屏幕尺寸上运行应用程序时,图标不在同一个地方。
唯一可以正常工作的是我在 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>