How to Make a Paper Folding Animation in Android

title="AddThis | LinkedIn Button" frameborder="0" role="presentation" scrolling="no" allowtransparency="true" scrollbars="no" src="http://s7.addthis.com/static/linkedin.html#href=http%3A%2F%2Fcases.azoft.com%2Fcreating-paper-folding-animation-in-android%2F&dr=&conf=product%3Dtbx-300%26title%3DHow%252520to%252520Make%252520a%252520Paper%252520Folding%252520Animation%252520in%252520Android%26data_track_clickback%3Dfalse%26username%3Dra-517e66fb6799d4bc%26pubid%3Dra-517e66fb6799d4bc&share=title%3DHow%25252520to%25252520Make%25252520a%25252520Paper%25252520Folding%25252520Animation%25252520in%25252520Android%26imp_url%3D0%26url%3Dhttp%253A%252F%252Fcases.azoft.com%252Fcreating-paper-folding-animation-in-android%252F%26smd%3Drsi%253D%2526rxi%253Dundefined%2526gen%253D0%2526rsc%253D%2526dr%253D%2526sta%253DAT-ra-517e66fb6799d4bc%25252F-%25252F-%25252F56414d345fd26808%25252F1%26passthrough%3Dlinkedin%253Dcounter%25253Dtop&li=counter%3Dtop%26height%3D55%26width%3D57" style="margin: 0px 0px 5px; padding: 0px; border-width: 0px; border-style: initial; width: 57px; height: 55px;">
frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I0_1447120184996" name="I0_1447120184996" src="https://apis.google.com/se/0/_/+1/fastbutton?usegapi=1&size=tall&hl=en-US&origin=http%3A%2F%2Fcases.azoft.com&url=http%3A%2F%2Fcases.azoft.com%2Fcreating-paper-folding-animation-in-android%2F&gsrc=3p&ic=1&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.tkiNpjG4rhI.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCNhbBDeEP99y0jGa8pLoqSI515feQ#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh&id=I0_1447120184996&parent=http%3A%2F%2Fcases.azoft.com&pfname=&rpctoken=42275829" data-gapiattached="true" title="+1" style="margin: 0px; padding: 0px; border-width: 0px; border-style: none; position: static; top: 0px; width: 50px; left: 0px; visibility: visible; height: 60px;">
scrolling="no" frameborder="0" allowtransparency="true" src="http://badge.stumbleupon.com/badge/embed/5/?url=http%3A%2F%2Fcases.azoft.com%2Fcreating-paper-folding-animation-in-android%2F" width="50" height="60" id="iframe-stmblpn-widget-1" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; overflow: hidden;"> 45
Azoft Case Studies How to Make a Paper Folding Animation in Android

How to Make a Paper Folding Animation in Android

By  Alex Vasilkov on April 24, 2014

Creating a Paper-Like Folding Animation in Android

Inspired by Facebook Paper for iOS, in one of our recent projects we decided to implement a similar-style animated effect when opening elements from a list. Initially, we tried to use an existing implementation – the android-flip library that uses OpenGL for rendering animation – but it ended up displaying rather noticeable screen artifacts (image flickering) in latest version of Android. Plus, the library needed considerable modification as it was designed for flipping elements in a list, while our project required animation when opening elements from a list. You can see the difference in the demo video below: Foldable list is what android-flip can do, Unfoldable details is what we actually needed.

allowfullscreen="" frameborder="0" height="280" src="http://www.youtube.com/embed/GzWGhp_NDTg" width="500" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial;">

Considering this scenario, we decided to implement the effect ourselves and, since the minimum supported Android version for the app was 4.0, we used standard Android SDK methods instead of OpenGL: View.setRotationX()View.setScaleX(), etc. When hardware acceleration is enabled (and it is enabled by default if your target API level is >=14), these methods work quite efficiently using the device GPU.

The results came out looking very nice, so we decided to share our approach. While this article covers the basic implementation points, all of the actual code can be downloaded from GitHub: FoldableLayout.

Creating a Paper-Like Folding Animation in Android

Layout implementation

The first element to design was a layout that can fold in half. Our approach was rather bold: the main layout (FoldableItemLayout) simply contains a specialized layout (BaseLayout). During the animation, the BaseLayout writes its contents to cache which is a specially created Bitmap object based on the size of the original layout.

  1. class FoldableItemLayout extends FrameLayout {  
  2.     @Override  
  3.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  4.         Bitmap cacheBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);  
  5.         mBaseLayout.setCacheCanvas(new Canvas(cacheBitmap));  
  6.     }  
  7. }  
  8.   
  9. class BaseLayout extends FrameLayout {  
  10.     private Canvas mCacheCanvas;  
  11.   
  12.     private void setCacheCanvas(Canvas cacheCanvas) {  
  13.         mCacheCanvas = cacheCanvas;  
  14.     }  
  15.   
  16.     @Override  
  17.     public void draw(Canvas canvas) {  
  18.         mCacheCanvas.drawColor(0, PorterDuff.Mode.CLEAR);  
  19.         super.draw(mCacheCanvas);  
  20.     }  
  21. }  

In addition, we needed to use two extra Views (PartView) – for the upper and lower image halves – which would display the corresponding data in cache that represents the upper and lower halves of the image (Bitmap). Both Views encompass the entire area of the main layout, but display only the required parts. To achieve this effect, we calculated the Bitmap limits – and in the onDraw() method we made Canvas draw the required part via thedrawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) method.

Then we managed to rotate these extra Views by setting the setRotationX() method to the corresponding angle, achieving independent rotation of the lower and upper parts of the images. To pull this off, we add a new parameter for the FoldableItemLayout – with the name FoldRotation.

The FoldRotation parameter values in diapason (-180, 180]:

  1. FoldRotation = 0 – Both parts have zero rotation. In this case we can skip the Bitmapcache and display the original layout in real time.
  2. 0 < FoldRotation < 90 – The lower part rotates to the FoldRotation angle; upper part has zero rotation.
  3. -90 < FoldRotation < 0 – Only the upper part rotates.
  4. 90 ≤ FoldRotation < 180 – The lower part is no longer seen. In this case theFoldableItemLayout containing the next layout should appear over the currentFoldableItemLayout.
  5. -180 < FoldRotation ≤ -90 – the upper part is no longer seen. In this case theFoldableItemLayout containing a previous layout should appear over the currentFoldableItemLayout.
  6. FoldRotation = 180 – Both parts are hidden.

Now having a secondary layout that allowed us to “fold” the elements it contained, we were then able to make a FoldableListLayout – a ListView-like layout that creates list elements and wraps them into FoldableItemLayout by using BaseAdapter. Again, we used theFoldRotation parameter, but in this case it determined the elements position in a list.

For example, at FoldRotation = 30 the first element (FoldableItemLayout) in the list has theFoldRotation value of 30, and the second – FoldRotation = 150. No more than two elements will be displayed simultaneously. The FoldRotation parameter value range depends on the number of elements: if the list contains one element, it would be [0, 0], two – [0, 180], three v [0, 360], etc.

Opening animation

After having learned to scroll between several elements with folding animation, we then tackled the major challenge: generating an opening animation of elements from any starting point. We did it using the already implemented  FoldableListLayout and made it switch between two elements: cover layout and details layout. Both elements should be on a screen, but the details element should be hidden. When a user clicks on a cover element, the app remembers its current position, replaces it with an empty placeholder View of the same size (so as not to disrupt other elements on the screen), and moves the cover element to the lower half of the specially created layout. Later this layout would be used as the first element of the FoldableListLayout, while the second element would be the details element that was replaced by an empty placeholder View the same way as the cover element.

Adjustments

Seeing that the cover element had to unfold from its initial on-screen position, ourFoldableListLayout therefore needed to move between the cover element and details element positions during the animation. That's why it's vital to remember the initial position and size of each element during the animation’s initialization. Since the cover and details can differ in size, we needed to scale them simultaneously during animation so the widths of both elements coincided.

We are almost there; just one more thing: After scaling, the cover height may appear smaller than the details lower half, which means you will need to hide the remaining part of the image.

The screenshot below shows a gray area appearing at the beginning of the animation and the lower part of the details that doesn't fit the cover size.

After scaling, the cover height may appear smaller than the details lower half, which means you will need to hide the remaining part of the image.

Introducing an extra parameter for FoldableItemLayoutm – let's name it RollingDistance – solved the issue. This parameter was responsible for shifting the image vertically from the fold line. Using this parameter, it’s possible to shift a part of the details imperceptibly during the first part of the animation and then unfold it full-size in the second part.

The paper folding animation is now ready to run, and all that’s left is to add some darkening for a realistic effect, or flecks for a glossy look. Feel free to use the library on GitHub, where you can also find a usage example. Pull requests are welcome as always.

Rating: 4.6/ 5 (36 votes cast)
Rating:  +25 (from 31 votes)
How to Make a Paper Folding Animation in Android4.6 out of 5 based on 36 ratings

Content created by Alexey Vasilkov
name="oauth2relay896378818" id="oauth2relay896378818" src="https://accounts.google.com/o/oauth2/postmessageRelay?parent=http%3A%2F%2Fcases.azoft.com#rpctoken=2130290738&forcesecure=1" tabindex="-1" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; color: rgb(54, 54, 54); font-family: Arial; line-height: 16px; width: 1px; height: 1px; position: absolute; top: -100px;">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值