2
2
3
3
import android .app .ActivityOptions ;
4
4
import android .content .Intent ;
5
+ import android .graphics .BitmapFactory ;
6
+ import android .graphics .drawable .BitmapDrawable ;
7
+ import android .graphics .drawable .TransitionDrawable ;
8
+ import android .os .Build ;
5
9
import android .os .Bundle ;
6
10
import android .support .v4 .app .Fragment ;
7
11
import android .view .LayoutInflater ;
@@ -16,21 +20,28 @@ public class WindowAnimationFragment extends Fragment {
16
20
@ Override
17
21
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
18
22
View view = inflater .inflate (R .layout .animation_layout , null );
19
- view .findViewById (R .id .button ).setOnClickListener (new View .OnClickListener (){
23
+ view .findViewById (R .id .button ).setOnClickListener (new View .OnClickListener () {
20
24
@ Override
21
25
public void onClick (View v ) {
22
- // Using the AnimatedSubActivity also allows us to animate exiting that
23
- // activity - see that activity for details
24
- Intent subActivity = new Intent (v .getContext (),
25
- AnimatedSubActivity .class );
26
- // The enter/exit animations for the two activities are specified by xml resources
27
- Bundle translateBundle =
28
- ActivityOptions .makeCustomAnimation (v .getContext (),
29
- R .anim .slide_in_left , R .anim .slide_out_left ).toBundle ();
30
- getActivity ().startActivity (subActivity , translateBundle );
26
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
27
+ // Using the AnimatedSubActivity also allows us to animate exiting that
28
+ // activity - see that activity for details
29
+ Intent subActivity = new Intent (v .getContext (),
30
+ AnimatedSubActivity .class );
31
+ // The enter/exit animations for the two activities are specified by xml resources
32
+ Bundle translateBundle =
33
+ ActivityOptions .makeCustomAnimation (v .getContext (),
34
+ R .anim .slide_in_left , R .anim .slide_out_left ).toBundle ();
35
+ getActivity ().startActivity (subActivity , translateBundle );
36
+ } else {
37
+ getActivity ().overridePendingTransition (R .anim .slide_in_left , R .anim .slide_out_left );
38
+ getActivity ().startActivity (new Intent (v .getContext (),
39
+ AnimatedSubActivity .class ));
40
+ }
41
+
31
42
}
32
43
});
33
- view .findViewById (R .id .image ).setOnClickListener (new View .OnClickListener (){
44
+ view .findViewById (R .id .image ).setOnClickListener (new View .OnClickListener () {
34
45
@ Override
35
46
public void onClick (View v ) {
36
47
Intent subActivity = new Intent (v .getContext (),
@@ -39,9 +50,39 @@ public void onClick(View v) {
39
50
// v, 0, 0, v.getWidth(), v.getHeight()).toBundle();
40
51
// getActivity().startActivity(subActivity, scaleBundle);
41
52
v .setDrawingCacheEnabled (true );
42
- getActivity ().startActivity (subActivity , ActivityOptions .makeThumbnailScaleUpAnimation (v , v .getDrawingCache (),0 ,0 ).toBundle ());
53
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
54
+ getActivity ().startActivity (subActivity , ActivityOptions .makeThumbnailScaleUpAnimation (v , v .getDrawingCache (), 0 , 0 ).toBundle ());
55
+ } else {
56
+ getActivity ().overridePendingTransition (R .anim .slide_in_left , R .anim .slide_out_left );
57
+ getActivity ().startActivity (new Intent (v .getContext (),
58
+ AnimatedSubActivity .class ));
59
+ }
43
60
}
44
61
});
45
62
return view ;
46
63
}
64
+
65
+ @ Override
66
+ public void onViewCreated (View view , Bundle savedInstanceState ) {
67
+ ImageView imageView = (ImageView ) view .findViewById (R .id .image2 );
68
+ BitmapDrawable [] bitmapDrawable = new BitmapDrawable [2 ];
69
+ bitmapDrawable [0 ] = new BitmapDrawable (getResources (), BitmapFactory .decodeResource (getResources (), R .drawable .image1 ));
70
+ bitmapDrawable [1 ] = new BitmapDrawable (getResources (), BitmapFactory .decodeResource (getResources (), R .drawable .image2 ));
71
+ final TransitionDrawable drawable = new TransitionDrawable (bitmapDrawable );
72
+ imageView .setImageDrawable (drawable );
73
+ imageView .setOnClickListener (new View .OnClickListener () {
74
+ @ Override
75
+ public void onClick (View v ) {
76
+ if (currentDrawable == 0 ) {
77
+ drawable .startTransition (500 );
78
+ currentDrawable = 1 ;
79
+ } else {
80
+ drawable .reverseTransition (500 );
81
+ currentDrawable = 0 ;
82
+ }
83
+ }
84
+ });
85
+ }
86
+
87
+ int currentDrawable = 0 ;
47
88
}
0 commit comments