线性布局加动画。我在我的应用程序中做了类似的事情。
甚至不使用片段。使用动画类,代码在这里:
/*
This class is responsible for showing the sliding animation
*/
public class SlideAnim extends Animation {
int targetWidth;
View slideView;
ImageView imageView;
boolean close;
public SlideAnim(View _v, boolean _close, int _maxWidth, ImageView imageView) {
this.slideView = _v;
this.imageView = imageView;
targetWidth = _maxWidth;
close = _close;
}
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth;
if (!close) {
newWidth = (int) (targetWidth * interpolatedTime);
} else {
newWidth = (int) (targetWidth * (1 - interpolatedTime));
}
slideView.getLayoutParams().width = newWidth;
slideView.requestLayout();
imageView.setImageResource(slideView.getWidth() > 0 ? R.drawable.purple_arrow_right : R.drawable.purple_arrow_left);
}
public void initalize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
public boolean willChangeBounds() {
return true;
}
}
这是我从另一个 Activity 调用动画的方式:
SlideAnim slideAnim = new SlideAnim(trendingListLayout, false, maxListWidth, imageView);
slideAnim.setDuration(500);
slideAnim.reset();
trendingListLayout.clearAnimation();
trendingListLayout.startAnimation(slideAnim);
我正在为 LinearLayout 设置动画:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.eazyigz.views.EazyigzImageView
android:id="@+id/whole_screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<View
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/explore_expander"
android:layout_width="30dp"
android:layout_height="fill_parent"
android:background="@color/eazyigz_bg_primary"
android:orientation="horizontal"
android:visibility="invisible" >
<ImageView
android:id="@+id/explore_expander_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="@drawable/purple_arrow_left" />
</LinearLayout>
<!-- List Layout -->
<LinearLayout
android:id="@+id/explore_list_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:paddingTop="50dp"
android:background="@color/eazyigz_bg_secondary"
android:orientation="vertical"
android:visibility="invisible" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="0dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal|center_vertical"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:text="@string/top_trending"
android:textColor="@color/eazyigz_green"
android:textSize="30sp" />
<ProgressBar
android:id="@+id/explore_spinner"
android:layout_width="50dp"
android:layout_height="45dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:indeterminateDrawable="@drawable/progress_spinner"
android:visibility="visible"
android:layout_gravity="center_horizontal|center_vertical"/>
<ListView
android:id="@+id/explore_list"
style="@style/EazyigzListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#0000"
android:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="50dp"
android:paddingLeft="20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<Button
android:id="@+id/eazyigz_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="@drawable/eazyigz_button"
android:drawablePadding="0dp"
android:gravity="left|center_vertical"
android:padding="5dp"
android:text="@string/playing"
android:textColor="@color/eazyigz_white"
android:textSize="36sp"
android:visibility="gone"/>
<Button
android:id="@+id/eazyigz_create"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="@drawable/eazyigz_button"
android:drawablePadding="0dp"
android:gravity="left|center_vertical"
android:padding="5dp"
android:text="@string/create"
android:textColor="@color/eazyigz_white"
android:textSize="36sp" />
<Button
android:id="@+id/eazyigz_explore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="@drawable/eazyigz_button"
android:drawablePadding="0dp"
android:gravity="left|center_vertical"
android:padding="5dp"
android:text="@string/explore"
android:textColor="@color/eazyigz_white"
android:textSize="36sp" />
<Button
android:id="@+id/eazyigz_listen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/eazyigz_button"
android:drawablePadding="0dp"
android:gravity="left|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="50dp"
android:paddingTop="5dp"
android:text="@string/stations"
android:textColor="@color/eazyigz_white"
android:textSize="36sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</merge>
explore_list_layout是动画。
查看屏幕外观的视频:滑动动画