2014-06-09 54 views
1

我已經設置了一個相對視圖,其中包含所有我的元素(按鈕,圖像等)。這是我的Android應用程序的標題頁。
現在我想在整個佈局上,在左上角覆蓋「LITE」橫幅。

我的問題是,「LITE」橫幅圖像是一個傾斜的紅色橡膠,並且我需要在屏幕上將其設置爲(-45,-45)以僅顯示我想要的圖像部分是源圖像,因此您可以瞭解圖像的哪一部分應該在屏幕上可見)。如何在RelativeLayout之外或屏幕之外稍微顯示ImageView?如何在屏幕上顯示橡膠

the banner

我曾嘗試AbsoluteLayout的RelativeLayout的,與SetLeft和機頂盒編程方式移動,但負值不被接受。

有什麼想法?

+0

爲什麼不將它添加到佈局之前削減的形象呢? –

+0

@Rod_Algonquin:因爲我與iPad應用程序共享這些圖像。這在XCode上很容易實現。我寧願以編程方式,但如果我沒有得到答案,那就是我必須做的。 – philippe

+0

我之前使用過負邊距來達到類似的效果。看到這個職位:http://stackoverflow.com/questions/10673503/is-it-a-bad-practice-to-use-negative-margins-in-android – Chris

回答

1

我想與大家分享關於這件事與社區我的經驗...

的想法是在上肢上顯示一個傾斜的「LITE」橡膠我的應用程序的主屏幕的ner。

Rod Algonquin的回答很好。但是,它並沒有完全解決我的問題,因爲我不得不將圖片的尺寸調整到屏幕高度...以及屏幕方向。惡夢。即使使用相對佈局,也幾乎不可能,因爲圖像的隱藏部分從未正確對齊過。

所以我不得不以不同的方式工作:圖片必須左移和上移20%。怎麼做 ?

1)在layout.xml文件:

  • 插入ImageView的一個RelativeLayout的內部
  • 給相對佈局的ID
  • 配置的ImageView以使其適合其容器的RelativeLayout的寬度和高度(layout_width = 「WRAP_CONTENT」 和layout_height = 「WRAP_CONTENT」)

    <RelativeLayout 
        android:id="@+id/accueil_litebannerlayout" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
        <ImageView 
         android:id="@+id/accueil_litebanner" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:scaleType="fitXY" 
         android:src="@drawable/lite_banner" /> 
    </RelativeLayout> 
    

2)在您的activity.java類文件:

//get screen dimensions 
     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     int ScreenWidth = size.x; 
     int ScreenHeight = size.y; 
     //set the desired height of the rubber, based on screen's height  
     int myLayoutWidthAndHeight=ScreenHeight/4; 

    //get rubber PNG image dimensions 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds=true; 
     BitmapFactory.decodeResource(getResources(), 
       R.drawable.lite_banner,options); 
     int imageHeight = options.outHeight; 
     int imageWidth = options.outWidth; 

    //redux_factor has to be calculated, because if the image is reduced, then the translation has to be adapted 
     double redux_factor=1; 
     if (myLayoutWidthAndHeight<imageWidth) { 
      redux_factor=(double)myLayoutWidthAndHeight/imageWidth; 
     } 
    //determine by how many pixels left and top (same) the image will have to be translated 
     double translation_percents=.22; 
     double myCroppedMargin_double=imageWidth*translation_percents*redux_factor; 
     int myCroppedMargin=(int) Math.round(myCroppedMargin_double); 

    //get the image layout 
     RelativeLayout litebannerlayout=(RelativeLayout) this.findViewById(R.id.accueil_litebannerlayout); 
    //change its parameters (width, height, leftMargin, topMargin) 
     RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(myLayoutWidthAndHeight,myLayoutWidthAndHeight); 
     params.setMargins(-myCroppedMargin, -myCroppedMargin, 0,0); 
     litebannerlayout.setLayoutParams(params); 

Arghhh。它的工作原理...

您可以使用此示例代碼將圖像視圖移出屏幕,基於百分比或像素數。此代碼也可以用於將橡膠/橫幅放在右上角,左下角,右下角。

OK,讓我們繼續到別的東西......

8

您可以使用相對佈局和屬性android:clipToPadding="false"來獲得期望的效果。

例如:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:clipToPadding="false" 
android:background="@android:color/white" 
android:paddingLeft="50dip" 
> 

<ImageView 
    android:id="@+id/myId" 
    android:layout_width="60dip" 
    android:layout_height="60dip" 
    android:layout_marginLeft="-70dp" 
    android:layout_marginTop="-20dp" 
    android:clipChildren="false" 
    android:src="@drawable/button_normal" /> 

</RelativeLayout> 

結果:

enter image description here

+0

謝謝你太多了!偉大的答案,與示例代碼和圖像。++ – philippe

+0

我已經在這個問題上工作了幾個小時,並不得不張貼另一個答案,因爲代碼需要...感謝無論如何羅布! – philippe

+0

@philippe隨時... –