如何小幅显示ImageView的一个RelativeLayout的外部或外屏幕?如何在屏幕的左上显示橡胶橡胶、外屏、屏幕、如何在

2023-09-06 14:38:21 作者:为谁画相思╮

我设置我的内(按钮,图像等)的所有元素相对的观点。这是我的Andr​​oid应用程序的扉页。结果现在我想以覆盖LITE的旗帜在整个布局中,upperleft角落。搜索结果我的问题是,LITE的旗帜形象斜红橡,那我需要它的左上点设置为(-45,-45)在屏幕上只显示我想要的(附图片的部分是源图像,因此您可以了解图像的哪一部分应在屏幕上可见)。

I've setup a relative view with all my elements inside (buttons, images, etc...). It is the title page of my Android application.Now I would like to overlay "LITE" banner over the whole layout, in the upperleft corner.My problem is that the "LITE" banner image is an oblique red rubber, and that I need to set its topleft point to (-45,-45) on the screen to only display the part of the image I want (attached is the source image so you can understand what part of the image should be visible on the screen).

我曾尝试AbsoluteLayout的RelativeLayout的,与SetLeft和机顶盒编程方式移动,但负值是不能接受的。

I have tried the AbsoluteLayout, the RelativeLayout, to move it programmatically with SetLeft and SetTop, but the negative values are not accepted.

任何想法?

推荐答案

我想与大家分享关于这件事与社区我的经验...

I'd like to share my experience on this affair with the community...

当时的想法是在我的应用程序的主屏幕的左上边角显示一个斜LITE橡皮。

The idea was to display an oblique "LITE" rubber on the topleft corner of the main screen of my app.

罗德阿尔冈金的回答是罚款。然而,它并没有完全解决我的问题,因为我不得不画面的尺寸适应屏幕高度......并在屏幕的方向。恶梦。即使具有相对布局,这是几乎不可能的,因为图象的隐蔽部位从未正确对齐。

Rod Algonquin's answer was fine. However, it did not completely solved my problem, because I had to adapt the picture's dimensions to the screen height...AND to the screen orientation. Nightmare. Even with a relative layout, it was nearly impossible, because the hidden parts of the image were never correctly aligned.

所以我不得不不同的方式工作:图片必须向左移动和顶部,20%。那怎么办?

So I had to work differently : The picture had to be moved left and top, by 20%. How to do that ?

1)在layout.xml文件:

1) In the layout.xml file :

将ImageView的一个RelativeLayout的内给相对布局的ID

配置的ImageView,使其适合其容器的RelativeLayout的宽度和高度(layout_width =WRAP_CONTENT和layout_height =WRAP_CONTENT) Insert the ImageView inside a RelativeLayout Give the relative layout an ID

Configure the ImageView to make it fit its container RelativeLayout's width and height (layout_width="wrap_content" and 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类文件:

2) In your activity.java class file :

    //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。它的工作原理...

Arghhh. It works...

您可以使用此示例code键移动一个ImageView的出来的画面,无论是基于百分比或像素数。这code也可以适应把橡胶/旗帜在topright,BOTTOMLEFT,bottomright角落。

You can use this sample code to move an imageView out of the screen, either based on a percentage, or a pixel count. This code can also be adapted to put the rubber/banner in the topright, bottomleft, bottomright corners.

OK,让我们继续到别的东西...

OK, let's move on to something else...

 
精彩推荐
图片推荐