2

我有圖像,我希望它是全屏的佈局。然後,我想將圖標放在圖像的特定位置;問題是當我在多種屏幕尺寸上運行應用程序時,圖標不在同一個地方。Android - 如何將圖標放在全屏圖像上的所有屏幕尺寸和密度的相同位置?

唯一能夠正常工作的是我在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

爲什麼不將您的圖像設置爲佈局的背景 – Daniel

+0

其實,我做了,但它是一樣的。 – Amt87

回答

1

您的代碼管理屏幕密度,但不管屏幕尺寸。

如果您將背景設置爲全屏,則會根據屏幕尺寸縮放背景。

您需要計算此比例並將其應用於您的利潤率。

但首先,我已經改變佈局,使backgroung採取fulscreen並對齊到左上角(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); 

和您的合作應用它mputation:我的例子是使用的屏幕尺寸,因此假定您的應用程序在全屏模式下運行

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); 

告訴我是否有幫助。

+0

第一印象「TEST」好像很好地工作,非常感謝。我必須做一些修改。 – Amt87

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 
+0

我用了下面的方法,但是我仍然需要在全屏的所有屏幕尺寸的圖像上放置圖標 – Amt87

相關問題