如何创建比屏幕大的视图?

时间:2021-08-08 21:45:16

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?

有没有办法用android框架实现这个效果?

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 个解决方案

#1


14  

Ok I got an answer. It is not very nice because it uses a deprecated View class but it works at least on my current testing screen resolution other resolutions are tested tomorrow.

好的,我得到了答案。它不是很好,因为它使用了一个弃用的View类,但它至少可以在我当前的测试屏幕分辨率上工作,明天将测试其他分辨率。

I wrapped the view that I wanted to expand beyond the screen in an absolute layout like this:

我用这样的绝对布局包装了我想要扩展到屏幕之外的视图:

<AbsoluteLayout
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_alignParentBottom="true">

  <ImageView
     android:id="@+id/content"
     android:layout_width="600dip"
     android:layout_height="420dip"
     android:scaleType="centerCrop"
     android:layout_x="-200dip"
     android:layout_y="60dip"
     android:src="@color/testcolor" />

</AbsoluteLayout>

The -200 x coordinate makes the view stick 200dip out of the left side of the screen. If I'm animating the view those parts that are outside the screen will gradually become visible.

-200x坐标使视图棒200dip离开屏幕的左侧。如果我正在为视图设置动画,那么屏幕外部的部分将逐渐变为可见。

#2


8  

E.g. setting negative bottom margin together with setting extra large layout_height (large enough for you) solved the similar issue as for me.

例如。设置负底边距和设置超大layout_height(足够大)可以解决类似的问题。

Works fine at least using API 11+ animations/rotations.

至少使用API​​ 11+动画/旋转可以正常工作。

Could look like:

看起来像:

android:layout_marginBottom="-1000dp" android:layout_height="1000dp"

#3


4  

HorizontalScrollView:

http://developer.android.com/reference/android/widget/HorizontalScrollView.html

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.

用于视图层次结构的布局容器,可由用户滚动,允许它大于物理显示。

#4


4  

In case anyone still comes up on this page. The key is your root layout, it will only work with a FrameLayout (or the deprecated absolutelayout). Then you have two options to make your child view bigger.

如果有人仍然出现在这个页面上。关键是你的根布局,它只适用于FrameLayout(或不推荐的absolutelayout)。然后你有两个选项让你的孩子看得更大。

  1. through xml, this is quick and easy but you don't know the actual screen width & height in advance so your off with setting a ridiculously high value for layout_width & layout_height to cover all screens.
  2. 通过xml,这是快速而简单的,但你事先并不知道实际的屏幕宽度和高度,因此你可以设置一个非常高的layout_width和layout_height值来覆盖所有屏幕。

  3. Calculate the screen size programatically and make the view's width/height proportional bigger to this..
  4. 以编程方式计算屏幕尺寸,使视图的宽度/高度与此成比例。

Also be aware that your bigger view still starts in the top left corner of the screen so to account this you will have to give a negative top & left margin that's half of what you are adding to the view's width/height

另外请注意,您的较大视图仍然会从屏幕的左上角开始,因此为了说明这一点,您必须给出一个负的上下左边距,这是您添加到视图宽度/高度的一半

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) viewToMakeBigger.getLayoutParams();
int marginLeft = (int) (viewToMakeBigger.getWidth()*0.1);
int marginTop = (int) (viewToMakeBigger.getHeight()*0.1); 
params.width = (int) (viewToMakeBigger.getWidth()*1.2);
params.height = (int) (viewToMakeBigger.getHeight()*1.2);
params.leftMargin = -marginLeft;    
params.topMargin = -marginTop;
viewToMakeBigger.setLayoutParams(params);

#5


1  

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:

为什么不,您可以根据需要使用px(或dip)定义Layout_Width和Layout_height:

android:layout_width="10000px"
android:layout_height="20000px"

#6


1  

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

你需要通过getWindow()。setLayout来改变窗口的大小。这将增加窗口的大小。由于根布局可以与其父布局一样大,因此您可以增加想要大于屏幕大小的视图的大小。它对我有用,让我知道

#7


1  

You can override the views onMeasure method. This will set your view dimensions to 1000x1000px

您可以覆盖onMeasure方法的视图。这会将您的视图尺寸设置为1000x1000px

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(1000, 1000);
}

#8


1  

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.

下面的简单axml使用200dp的负左右边距创建比屏幕宽400dp的ImageView(即使layout_width设置为等于父级的宽度)。

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.

ImageView位于屏幕顶部上方250dp处,使用负上边距,屏幕上可以看到450dp的700dp垂直像素。

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

#9


0  

You can use ViewSwitcher to handle that. Used with Animation and a OnGestureListener looks pretty good.

您可以使用ViewSwitcher来处理它。与动画和OnGestureListener一起使用看起来非常好。

#10


0  

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.

必须只在“public void onWindowFocusChanged(boolean hasFocus)”方法中。

and

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

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

在“protected void onCreate(Bundle savedInstanceState)”方法中。

Where yout .xml file is like this:

你.xml文件是这样的:

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

#1


14  

Ok I got an answer. It is not very nice because it uses a deprecated View class but it works at least on my current testing screen resolution other resolutions are tested tomorrow.

好的,我得到了答案。它不是很好,因为它使用了一个弃用的View类,但它至少可以在我当前的测试屏幕分辨率上工作,明天将测试其他分辨率。

I wrapped the view that I wanted to expand beyond the screen in an absolute layout like this:

我用这样的绝对布局包装了我想要扩展到屏幕之外的视图:

<AbsoluteLayout
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_alignParentBottom="true">

  <ImageView
     android:id="@+id/content"
     android:layout_width="600dip"
     android:layout_height="420dip"
     android:scaleType="centerCrop"
     android:layout_x="-200dip"
     android:layout_y="60dip"
     android:src="@color/testcolor" />

</AbsoluteLayout>

The -200 x coordinate makes the view stick 200dip out of the left side of the screen. If I'm animating the view those parts that are outside the screen will gradually become visible.

-200x坐标使视图棒200dip离开屏幕的左侧。如果我正在为视图设置动画,那么屏幕外部的部分将逐渐变为可见。

#2


8  

E.g. setting negative bottom margin together with setting extra large layout_height (large enough for you) solved the similar issue as for me.

例如。设置负底边距和设置超大layout_height(足够大)可以解决类似的问题。

Works fine at least using API 11+ animations/rotations.

至少使用API​​ 11+动画/旋转可以正常工作。

Could look like:

看起来像:

android:layout_marginBottom="-1000dp" android:layout_height="1000dp"

#3


4  

HorizontalScrollView:

http://developer.android.com/reference/android/widget/HorizontalScrollView.html

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.

用于视图层次结构的布局容器,可由用户滚动,允许它大于物理显示。

#4


4  

In case anyone still comes up on this page. The key is your root layout, it will only work with a FrameLayout (or the deprecated absolutelayout). Then you have two options to make your child view bigger.

如果有人仍然出现在这个页面上。关键是你的根布局,它只适用于FrameLayout(或不推荐的absolutelayout)。然后你有两个选项让你的孩子看得更大。

  1. through xml, this is quick and easy but you don't know the actual screen width & height in advance so your off with setting a ridiculously high value for layout_width & layout_height to cover all screens.
  2. 通过xml,这是快速而简单的,但你事先并不知道实际的屏幕宽度和高度,因此你可以设置一个非常高的layout_width和layout_height值来覆盖所有屏幕。

  3. Calculate the screen size programatically and make the view's width/height proportional bigger to this..
  4. 以编程方式计算屏幕尺寸,使视图的宽度/高度与此成比例。

Also be aware that your bigger view still starts in the top left corner of the screen so to account this you will have to give a negative top & left margin that's half of what you are adding to the view's width/height

另外请注意,您的较大视图仍然会从屏幕的左上角开始,因此为了说明这一点,您必须给出一个负的上下左边距,这是您添加到视图宽度/高度的一半

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) viewToMakeBigger.getLayoutParams();
int marginLeft = (int) (viewToMakeBigger.getWidth()*0.1);
int marginTop = (int) (viewToMakeBigger.getHeight()*0.1); 
params.width = (int) (viewToMakeBigger.getWidth()*1.2);
params.height = (int) (viewToMakeBigger.getHeight()*1.2);
params.leftMargin = -marginLeft;    
params.topMargin = -marginTop;
viewToMakeBigger.setLayoutParams(params);

#5


1  

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:

为什么不,您可以根据需要使用px(或dip)定义Layout_Width和Layout_height:

android:layout_width="10000px"
android:layout_height="20000px"

#6


1  

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

你需要通过getWindow()。setLayout来改变窗口的大小。这将增加窗口的大小。由于根布局可以与其父布局一样大,因此您可以增加想要大于屏幕大小的视图的大小。它对我有用,让我知道

#7


1  

You can override the views onMeasure method. This will set your view dimensions to 1000x1000px

您可以覆盖onMeasure方法的视图。这会将您的视图尺寸设置为1000x1000px

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(1000, 1000);
}

#8


1  

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.

下面的简单axml使用200dp的负左右边距创建比屏幕宽400dp的ImageView(即使layout_width设置为等于父级的宽度)。

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.

ImageView位于屏幕顶部上方250dp处,使用负上边距,屏幕上可以看到450dp的700dp垂直像素。

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

#9


0  

You can use ViewSwitcher to handle that. Used with Animation and a OnGestureListener looks pretty good.

您可以使用ViewSwitcher来处理它。与动画和OnGestureListener一起使用看起来非常好。

#10


0  

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.

必须只在“public void onWindowFocusChanged(boolean hasFocus)”方法中。

and

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

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

在“protected void onCreate(Bundle savedInstanceState)”方法中。

Where yout .xml file is like this:

你.xml文件是这样的:

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