Skip to content

Commit 94ff956

Browse files
committed
transition drawable
1 parent 4ed94e5 commit 94ff956

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66

77
defaultConfig {
88
applicationId "com.avenwu.deepinandroid"
9-
minSdkVersion 14
9+
minSdkVersion 8
1010
targetSdkVersion 21
1111
versionCode 1
1212
versionName "1.0"

sample/src/main/java/com/avenwu/deepinandroid/WindowAnimationFragment.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import android.app.ActivityOptions;
44
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;
59
import android.os.Bundle;
610
import android.support.v4.app.Fragment;
711
import android.view.LayoutInflater;
@@ -16,21 +20,28 @@ public class WindowAnimationFragment extends Fragment {
1620
@Override
1721
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
1822
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() {
2024
@Override
2125
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+
3142
}
3243
});
33-
view.findViewById(R.id.image).setOnClickListener(new View.OnClickListener(){
44+
view.findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
3445
@Override
3546
public void onClick(View v) {
3647
Intent subActivity = new Intent(v.getContext(),
@@ -39,9 +50,39 @@ public void onClick(View v) {
3950
// v, 0, 0, v.getWidth(), v.getHeight()).toBundle();
4051
// getActivity().startActivity(subActivity, scaleBundle);
4152
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+
}
4360
}
4461
});
4562
return view;
4663
}
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;
4788
}

sample/src/main/res/layout/animation_layout.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:orientation="vertical"
6-
android:background="#fff">
5+
android:background="#fff"
6+
android:orientation="vertical">
77

88
<Button
99
android:id="@+id/button"
@@ -16,4 +16,15 @@
1616
android:layout_width="50dp"
1717
android:layout_height="80dp"
1818
android:src="@drawable/image2" />
19+
20+
<TextView
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_marginTop="20dp"
24+
android:text="Click to transit the image drawable" />
25+
26+
<ImageView
27+
android:id="@+id/image2"
28+
android:layout_width="50dp"
29+
android:layout_height="80dp" />
1930
</LinearLayout>

0 commit comments

Comments
 (0)