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条回答
Luminary・发光体
2楼-- · 2020-01-28 08:36

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.

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.

查看更多
走好不送
3楼-- · 2020-01-28 08:36

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

查看更多
Root(大扎)
4楼-- · 2020-01-28 08:37

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

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

Could look like:

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

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-01-28 08:37

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.

查看更多
Evening l夕情丶
6楼-- · 2020-01-28 08:44

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(1000, 1000);
}
查看更多
Emotional °昔
7楼-- · 2020-01-28 08:46

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.

  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. Calculate the screen size programatically and make the view's width/height proportional bigger to this..

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);
查看更多
登录 后发表回答