Android Shared Element Animation doesn't work for me -
i following instruction on https://developer.android.com/training/material/animations.html , trying implement animation shared element between activities, doesn't work me, searched lot not find answer, there can take look? thank much!
my steps:
1, create directory "transition" under "res" directory, , create file list_to_details.xml there:
<?xml version="1.0" encoding="utf-8"?> <transitionset xmlns:android="http://schemas.android.com/apk/res/android"> <changeimagetransform/> </transitionset>
2, create directory "values-v21" under "res" directory, , create file styles.xml there(setting transition defined above in step 1):
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="apptheme" parent="android:theme.material"> <!-- enable window content transitions --> <item name="android:windowcontenttransitions">true</item> <!-- specify enter , exit transitions --> <item name="android:windowsharedelemententertransition">@transition/list_to_detail</item> <item name="android:windowsharedelementexittransition">@transition/list_to_detail</item> </style> </resources>
3, set application theme "apptheme" defined in new styles file
<application android:name="com.myapp.myapplication" android:allowbackup="false" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/apptheme" >
4, add transitionname shared element in activity , b: a: b:
5, add code start activity b in activity a:
expandedimageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { activityoptions options = activityoptions.makescenetransitionanimation(detaildemoactivity.this, v, "todetail"); intent intent = new intent(detaildemoactivity.this, anotherdetaildemoactivity.class); intent.putextra("original_url", originalurl); activitycompat.startactivity(detaildemoactivity.this, intent, options.tobundle()); } });
6, try app doesn't play animation, guessing reason might have async task in activity b load image, still doesn't work after tried postponeentertransition, tried getsharedelemententertransition, debugger showed null, see code:
@override protected void oncreate(bundle savedinstancestate) { postponeentertransition(); intent intent = this.getintent(); originalurl = intent.getstringextra("original_url"); getbigimageasynctask getbigimagetask = new getbigimageasynctask(); getbigimagetask.execute(); super.oncreate(savedinstancestate); setcontentview(r.layout.another_detail_demo); // have tried settransitionname() here doesn't work } private void schedulestartpostponedtransition(final view sharedelement) { sharedelement.getviewtreeobserver().addonpredrawlistener( new viewtreeobserver.onpredrawlistener() { @override public boolean onpredraw() { sharedelement.getviewtreeobserver().removeonpredrawlistener(this); startpostponedentertransition(); return true; } }); } private class getbigimageasynctask extends asynctask<object, void, bitmap> { @override protected bitmap doinbackground(object... urls) { inputstream input = null; httpurlconnection connection = null; try { url url = new url(originalurl); connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); input = connection.getinputstream(); bitmap mybitmap = bitmapfactory.decodestream(input); return mybitmap; } catch (exception e) { return null; } { if (connection != null) { connection.disconnect(); } if (input != null) { try { input.close(); } catch (exception e) { } } } } @override protected void onpostexecute(bitmap result) { imageview expandedimageview = (imageview) findviewbyid(r.id.another_demo_expanded_image); expandedimageview.setimagedrawable(new bitmapdrawable(result)); schedulestartpostponedtransition(expandedimageview); final transition transition = anotherdetaildemoactivity.this.getwindow().getsharedelemententertransition(); // in fact here object transition null if (transition != null) { // there entering shared element transition add listener transition.addlistener(new transition.transitionlistener() { @override public void ontransitionend(transition transition) { transition.removelistener(this); } @override public void ontransitionstart(transition transition) { } @override public void ontransitioncancel(transition transition) { // make sure remove ourselves listener transition.removelistener(this); } @override public void ontransitionpause(transition transition) { // no-op } @override public void ontransitionresume(transition transition) { // no-op } }); } } }
is there wrong or missed in steps? highly appreciated!
just made work.
two problems affect:
- i need use java code change instead of xml changes(not sure why, guess maybe there's wrong in android sdk parse xml changes)
- i need add window.requestfeature(window.feature_activity_transitions) make work, feature not mentioned in android developer site, mentioned in stakeoverflow question.
Comments
Post a Comment