Toggle animation for shared view elements during activity switching in Android (5.0 +)
Source: Internet
Author: User
<span id="Label3"></p>Also posted on my blog<p><p>Click here to enter</p></p>Background<p><p>The background of this is very simple, often in the use of the picture list when you think, if "the picture in the list to enlarge the entire screen" as the Activity of the motion tweens, it is very perfect. Just like This:</p></p><p><p></p></p><p><p>After the release of the SDK for Android 5.0 Lollipop, This new theme was included in Appcompat-v7 21.</p></p><p><p>The implementation method described here is activityoptionscompat.makescenetransitionanimation, the disadvantage is that only 5.0 can see the effect, under 5.0 can only ensure that the program is not Error.</p></p>Realize<p><p>The first thing to do in Androidmanifest.xml is to activate this function for the Activity that needs this animation, so I write a style for this Byte.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs applescript"><span class="hljs-property">name</span>=<span class="hljs-string">"AnimationActivity"</span>><<span class="hljs-property">item</span><span class="hljs-property">name</span>=<span class="hljs-string">"android:windowContentTransitions"</span>><span class="hljs-constant">true</span></<span class="hljs-property">item</span>></style></code></pre></pre><p><p>First to build a browsing interface, the key is to set the shared view elements of two places</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">vieweractivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">appcompatactivity</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Launch</span></span>(appcompatactivity activity, View transitionview,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>ResId) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (activity, vieweractivity.class); Intent.putextra (<span class="hljs-string"><span class="hljs-string">"resId"</span></span>, resId);<span class="hljs-comment"><span class="hljs-comment">//the shared view element is specified here</span></span>Activityoptionscompat options = activityoptionscompat. makescenetransitionanimation (activity, transitionvie W<span class="hljs-string"><span class="hljs-string">"image"</span></span>); Activitycompat.startactivity (activity, intent, Options.tobundle ()); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_viewer); ImageView ImageView = (ImageView) Findviewbyid (r.id.image); Imageview.setimageresource (getintent (). Getintextra (<span class="hljs-string"><span class="hljs-string">"resId"</span></span>, r.mipmap.pic_1));<span class="hljs-comment"><span class="hljs-comment">//the shared view element is specified here</span></span>Viewcompat.settransitionname (imageView,<span class="hljs-string"><span class="hljs-string">"image"</span></span>); }}</code></pre><p><p>Then start the Top-written browsing Interface.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">ViewerActivity<span class="hljs-preprocessor">.launch</span>(MainActivity<span class="hljs-preprocessor">.this</span>, v, data<span class="hljs-preprocessor">.get</span>(position)<span class="hljs-preprocessor">.resId</span>)<span class="hljs-comment">;</span></code></pre></pre><p><p>I know it's easier to write, so I'm Ready.</p></p><p><p>The welfare of the party: point I jump code address</p></p>More<p><p>The above method is very simple, but only in the 5.0 version to show the effect, for most of Today's mobile phones are still 4.x, it is undoubtedly not online. the <strong>Next article will discuss how to achieve the same effect on 4.x, so stay tuned. </strong></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Toggle animation for shared view elements during activity switching in Android (5.0 +)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.