【发布时间】:2015-01-28 02:21:27
【问题描述】:
我有四个动画要应用于视图,两个用于缩放以填充活动,两个用于缩小视图然后隐藏它。我需要使用两个动画,否则动画会缩放 X 和 Y,因此它们会同时完成,并且 Y 的缩放速度比 X 快得多。
我用来扩大视图的两个动画可以按需要工作,但是用于缩小背景的第二组动画根本不起作用。
放大
onsite_background.setVisibility(View.VISIBLE);
Animation scaleYAnim = AnimationUtils.loadAnimation(context, R.anim.fill_y);
Animation scaleXAnim = AnimationUtils.loadAnimation(context, R.anim.fill_x);
AnimationSet scaleAnim = new AnimationSet(false);
scaleAnim.addAnimation(scaleXAnim);
scaleAnim.addAnimation(scaleYAnim);
scaleAnim.setFillEnabled(true);
scaleAnim.setFillAfter(true);
onsite_background.startAnimation(scaleAnim);
scaleAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
Log.v("onAnimationEnd", "scaleAnim");
mSignOuthButton.setTextColor(Color.WHITE);
}
});
扩展 XML
//fill_y.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:duration="450"
android:fromYScale="1.0"
android:fromXScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="110%p"
android:toXScale="1.0" />
</set>
//fill_x.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:duration="300"
android:fromYScale="1.0"
android:fromXScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="1.0"
android:toXScale="110%p" />
</set>
缩小(破碎)
Animation shrinkYAnim = AnimationUtils.loadAnimation(context, R.anim.shrink_y);
Animation shrinkXAnim = AnimationUtils.loadAnimation(context, R.anim.shrink_x);
AnimationSet shrinkAnim = new AnimationSet(false);
shrinkAnim.addAnimation(shrinkXAnim);
shrinkAnim.addAnimation(shrinkYAnim);
shrinkAnim.setFillEnabled(true);
shrinkAnim.setFillAfter(true);
onsite_background.startAnimation(shrinkAnim);
shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
mSignOuthButton.setTextColor(getResources().getColor(R.color.rc_grey));
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
Log.v("onAnimationEnd", "shrinkAnim");
onsite_background.clearAnimation();
onsite_background.setVisibility(View.GONE);
}
});
缩小 XML
// shrink_y.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:duration="350"
android:fromXScale="110%p"
android:fromYScale="110%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="34dp"
android:toXScale="110%p"/>
</set>
// shrink_x.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:duration="300"
android:fromXScale="110%p"
android:fromYScale="110%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="110%p"
android:toXScale="139dp"/>
</set>
@Madhu 要求的活动
//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
tools:context="com...MainActivity">
<ImageView
android:id="@+id/onsite_background"
android:layout_width="1px"
android:layout_height="44dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/onsite_rect"
android:visibility="gone" />
<Button
android:id="@+id/start_work_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:minHeight="45dp"
android:minWidth="150dp"
android:text="@string/main_work_button_inactive"
android:textColor="#fff"
android:textSize="19sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/start_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<Button
android:id="@+id/log_out_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#0000"
android:paddingBottom="@dimen/button_inset_vertical_material"
android:paddingLeft="@dimen/button_inset_horizontal_material"
android:paddingRight="@dimen/button_inset_horizontal_material"
android:paddingTop="@dimen/button_inset_vertical_material"
android:text="@string/log_out_string"
android:textColor="@color/rc_grey"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
【问题讨论】:
-
发布您的布局 xml