Skip to content

Commit 40ae938

Browse files
committed
Added animation support, @AnimationRes can now be used on Animation fields. Fixes issue 88.
1 parent 110e5fd commit 40ae938

6 files changed

Lines changed: 134 additions & 9 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/model/AndroidRes.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public enum AndroidRes {
4444

4545
STRING(Res.STRING, StringRes.class, "getString", "java.lang.String"), //
4646
STRING_ARRAY(Res.ARRAY, StringArrayRes.class, "getStringArray", "java.lang.String[]"), //
47-
ANIMATION(Res.ANIM, AnimationRes.class, "getAnimation", "android.content.res.XmlResourceParser"), //
47+
ANIMATION(Res.ANIM, AnimationRes.class, "getAnimation", "android.content.res.XmlResourceParser", "android.view.animation.Animation"), //
4848
BOOLEAN(Res.BOOL, BooleanRes.class, "getBoolean", "java.lang.Boolean", "boolean"), //
4949
COLOR_STATE_LIST(Res.COLOR, ColorStateListRes.class, "getColorStateList", "android.content.res.ColorStateList"), //
5050
DIMENSION(Res.DIMEN, DimensionRes.class, "getDimension", "java.lang.Float", "float"), //
@@ -95,7 +95,6 @@ public int idFromElement(Element element) {
9595
} catch (Exception e) {
9696
throw new RuntimeException(e);
9797
}
98-
9998
}
10099

101100
}

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/ResProcessor.java‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import java.lang.annotation.Annotation;
1919

2020
import javax.lang.model.element.Element;
21+
import javax.lang.model.type.TypeMirror;
2122

2223
import com.googlecode.androidannotations.annotations.Id;
2324
import com.googlecode.androidannotations.model.AndroidRes;
2425
import com.googlecode.androidannotations.rclass.IRClass;
2526
import com.googlecode.androidannotations.rclass.IRClass.Res;
2627
import com.googlecode.androidannotations.rclass.IRInnerClass;
2728
import com.sun.codemodel.JBlock;
29+
import com.sun.codemodel.JClass;
2830
import com.sun.codemodel.JCodeModel;
2931
import com.sun.codemodel.JExpr;
3032
import com.sun.codemodel.JFieldRef;
@@ -63,14 +65,23 @@ public void process(Element element, JCodeModel codeModel, ActivitiesHolder acti
6365
}
6466

6567
JBlock methodBody = holder.beforeSetContentView.body();
66-
67-
if (holder.resources == null) {
68-
holder.resources = methodBody.decl(holder.refClass("android.content.res.Resources"), "resources_", JExpr.invoke("getResources"));
68+
69+
TypeMirror fieldTypeMirror = element.asType();
70+
String fieldType = fieldTypeMirror.toString();
71+
72+
// Special case for loading animations
73+
if ("android.view.animation.Animation".equals(fieldType)) {
74+
JClass animationUtils = holder.refClass("android.view.animation.AnimationUtils");
75+
methodBody.assign(JExpr.ref(fieldName), animationUtils.staticInvoke("loadAnimation").arg(JExpr._this()).arg(idRef));
76+
} else {
77+
if (holder.resources == null) {
78+
holder.resources = methodBody.decl(holder.refClass("android.content.res.Resources"), "resources_", JExpr.invoke("getResources"));
79+
}
80+
81+
String resourceMethodName = androidValue.getResourceMethodName();
82+
83+
methodBody.assign(JExpr.ref(fieldName), JExpr.invoke(holder.resources, resourceMethodName).arg(idRef));
6984
}
70-
71-
String resourceMethodName = androidValue.getResourceMethodName();
72-
73-
methodBody.assign(JExpr.ref(fieldName), JExpr.invoke(holder.resources, resourceMethodName).arg(idRef));
7485

7586
}
7687

‎AndroidAnnotations/functional-test-1-5/AndroidManifest.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
<activity android:name=".ExtraInjectedActivity_" />
3131
<activity android:name=".ApplicationInjectedActivity_" />
3232
<activity android:name=".prefs.PrefsActivity_" />
33+
<activity android:name=".res.ResActivity_" />
34+
3335

3436
</application>
3537

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
20+
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
21+
android:interpolator="@android:anim/accelerate_interpolator"
22+
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (C) 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.googlecode.androidannotations.test15.res;
17+
18+
import android.app.Activity;
19+
import android.content.res.XmlResourceParser;
20+
import android.view.animation.Animation;
21+
22+
import com.googlecode.androidannotations.annotations.EActivity;
23+
import com.googlecode.androidannotations.annotations.res.AnimationRes;
24+
import com.googlecode.androidannotations.test15.R;
25+
26+
@EActivity(R.layout.main)
27+
public class ResActivity extends Activity {
28+
29+
@AnimationRes(R.anim.fadein)
30+
XmlResourceParser xmlResAnim;
31+
32+
@AnimationRes
33+
Animation fadein;
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (C) 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.googlecode.androidannotations.test15.res;
17+
18+
import static org.fest.assertions.Assertions.*;
19+
20+
import org.junit.Before;
21+
import org.junit.runner.RunWith;
22+
23+
import android.content.res.Resources;
24+
import android.view.animation.AnimationUtils;
25+
26+
import com.xtremelabs.robolectric.RobolectricTestRunner;
27+
28+
@RunWith(RobolectricTestRunner.class)
29+
public class ResActivityTest {
30+
31+
private ResActivity_ activity;
32+
33+
@Before
34+
public void setup() {
35+
activity = new ResActivity_();
36+
activity.onCreate(null);
37+
}
38+
39+
/**
40+
* Cannot be tested right now, because there is no Robolectric shadow class
41+
* for {@link AnimationUtils}.
42+
*/
43+
// @Test
44+
public void animNotNull() {
45+
assertThat(activity.fadein).isNotNull();
46+
}
47+
48+
/**
49+
* Cannot be tested right now, because the Robolectric shadow class for
50+
* {@link Resources} doesn't implement {@link Resources#getAnimation(int)}
51+
*/
52+
// @Test
53+
public void xmlResAnimNotNull() {
54+
assertThat(activity.xmlResAnim).isNotNull();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)