올해는 머신러닝이다.
Fragment animation 정의하기 팁#1 본문
You need to use the new android.animation
framework (object animators) withFragmentTransaction.setCustomAnimations
as well as withFragmentTransaction.setTransition
.
Here's an example on using setCustomAnimations
from ApiDemos' FragmentHideShow.java:
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
and here's the relevant animator XML from res/animator/fade_in.xml:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@interpolator/accelerate_quad"
android:valueFrom="0"
android:valueTo="1"
android:propertyName="alpha"
android:duration="@android:integer/config_mediumAnimTime" />
Note that you can combine multiple animators using <set>
, just as you could with the older animation framework.
EDIT: Since folks are asking about slide-in/slide-out, I'll comment on that here.
Slide-in and slide-out
You can of course animate the translationX
, translationY
, x
, and y
properties, but generally slides involve animating content to and from off-screen. As far as I know there aren't any translation properties that use relative values. However, this doesn't prevent you from writing them yourself. Remember that property animations simply require getter and setter methods on the objects you're animating (in this case views), so you can just create your own getXFraction
and setXFraction
methods on your view subclass, like so:
public class MyFrameLayout extends FrameLayout {
...
public float getXFraction() {
return getX() / getWidth(); // TODO: guard divide-by-zero
}
public void setXFraction(float xFraction) {
// TODO: cache width
final int width = getWidth();
setX((width > 0) ? (xFraction * width) : -9999);
}
...
}
Now you can animate the 'xFraction' property, like so:
res/animator/slide_in.xml:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:valueFrom="-1.0"
android:valueTo="0"
android:propertyName="xFraction"
android:duration="@android:integer/config_mediumAnimTime" />
Note that if the object you're animating in isn't the same width as its parent, things won't look quite right, so you may need to tweak your property implementation to suit your use case.
'Android > Tip&Tech' 카테고리의 다른 글
Multipart form-date 내용 (0) | 2011.12.26 |
---|---|
Android: how to create transparent or opeque background (0) | 2011.12.22 |
[펌]Sliding Toggle 버튼 만들기 (0) | 2011.12.15 |
java android 명명규칙 및 일반규칙 (0) | 2011.12.09 |
[펌]안드로이드 로딩화면 추천 구축방법 (0) | 2011.12.08 |