2

我有图像,我希望它在布局中全屏显示。然后我想将图标放在图像的特定位置;问题是当我在多个屏幕尺寸上运行应用程序时,图标不在同一个地方。

唯一可以正常工作的是我在 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>
4

2 回答 2

1

看看这个 链接也许对你有帮助

这是您的图像的多种尺寸:

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 x 30dp 的按钮,您应该为每个密度提供图像:

   ldpi: 23px x 23px
    mdpi: 30px x 30px
    hdpi: 45px x 45px
    xhdpi: 60px x 60px
于 2013-05-19T12:57:45.053 回答
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);

告诉我这是否有帮助。

于 2013-05-21T12:23:15.503 回答