How to create a view that is bigger than the scree

2020-01-28 08:00发布

Is it possible to create a view that is bigger than the screen?

I need a view that has a bigger width then the screen of the device. I use this view in a rotation animation. During the rotation the parts that were not on the screen before animating the view will become visible.

Is there a way to achieve this effect with the android framework?

Update

I tried to set my parent layout much bigger then the screen and it is working. This will make somethings a little bit uncomfortable but it could work. The next problem now is that my layout still starts at the left side of the screen. I can't think of a method to make the layout to expand itself to the left and the right of the screen.

10条回答
啃猪蹄的小仙女
2楼-- · 2020-01-28 08:46

The simple axml below creates an ImageView that is 400dp wider than the screen (even though the layout_width is set to equal the parent's width) using a negative left and right margin of 200dp.

The ImageView is situated 250dp above the top of the screen using a negative top margin, with 450dp of 700dp vertical pixels visible on the screen.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
        android:background="#FFFF0000"
        android:layout_height="700dp"
        android:layout_marginLeft="-200dp"
        android:layout_marginRight="-200dp"
        android:layout_marginTop="-250dp"
        android:layout_width="match_parent" />
</RelativeLayout>
查看更多
冷血范
3楼-- · 2020-01-28 08:47

You can do it programmatically:

    FrameLayout.LayoutParams rootViewParams = (FrameLayout.LayoutParams) rootView.getLayoutParams();
    rootViewParams.height=displayMetrics.heightPixels+(int)dpToPixels(60);
    rootViewParams.width=displayMetrics.widthPixels+(int)dpToPixels(60);
    rootView.setLayoutParams(rootViewParams);
    rootView.setX(rootView.getX() - dpToPixels(30));
    rootView.setY(rootView.getY() - dpToPixels(30));

MUST BE ONLY IN "public void onWindowFocusChanged(boolean hasFocus)" method.

and

        rootView = (RelativeLayout) findViewById(R.id.rootLayout);

Inside "protected void onCreate(Bundle savedInstanceState)" method.

Where yout .xml file is like this:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    tools:context="com.example.Activity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_margin="30dp"  
    android:layout_centerVertical="true"
    android:layout_height="match_parent">

    // Bla bla bla

</RelativeLayout>

and:

public float dpToPixels(float dp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
查看更多
神经病院院长
4楼-- · 2020-01-28 08:48
Is it possible to create a view that is bigger then screen? 

Why not, you can define the Layout_Width and Layout_height with px(or dip) as much as you wants:

android:layout_width="10000px"
android:layout_height="20000px"
查看更多
对你真心纯属浪费
5楼-- · 2020-01-28 08:54

You need to change the size of the window, by getWindow().setLayout. This will increase the size for your window. Since the root layout can be as big as its parent you can then increase the size of the view you want to be bigger than the screen size. It works for me let me know

查看更多
登录 后发表回答