之前写过一篇关于的博客,主要用于“出发城市”和“到达城市”交换有动画效果,但是在实际应用中发现,在某些机型上实现该效果之后,若是该界面出现卡顿现象的话,之前移动的view又会回到原处(具体是什么原因还不清楚,请知道的老大告知一下),这样就可能会出现出发城市和达到城市颠倒问题。
在多番研究和请教高手之后,终于找到了一种比较完美的解决方案,以下是高人的原话:
“在framelayout中叠加两层图层 一个实际的数据层一个是动画层 当用户点击 切换按钮是时候 你把 事件数据层给隐藏掉 把动画层给展示出来 ,当动画结束之后 你把动画层给隐藏掉 把数据层展示出来并且 值交换”
我翻译一下:
就是有两个出发城市和到达城市,两个出发城市是重叠的,那个达到城市是重叠的,交换前是显示数据层的出发城市和达到城市,在点击交换之后,隐藏数据层(再将数据层的出发城市和到达城市数据交换),显示动画层,给动画层的出发城市到达城市添加移动动画,移动到目的位置之后再次显示数据层,隐藏动画层,这样就达到了出发城市和到达城市交换的且有动画的效果了
部分代码:
其中_txtFromStation,_txtToStation是动画层控件;txtFromStation,txtToStation是数据层控件
<RelativeLayout
android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2" android:orientation="vertical" ><!-- 出发城市 -->
<TextView
android:id="@+id/rl_02" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="@string/from_station_hint" android:textColor="@color/font_title" android:textSize="@dimen/text_size_normal" /><TextView
android:id="@+id/choose_fromStation" android:layout_width="match_parent" android:layout_height="39dp" android:layout_above="@+id/ly_split_01" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/rl_02" android:background="@drawable/not_corner_selector" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="成都" android:textColor="@color/black" android:textSize="@dimen/text_size_xlarge" > </TextView><TextView
android:id="@+id/_choose_fromStation" android:layout_width="match_parent" android:layout_height="39dp" android:layout_above="@+id/ly_split_01" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/rl_02" android:background="@drawable/not_corner_selector" android:gravity="center_vertical" android:paddingLeft="5dp" android:textColor="@color/black" android:textSize="@dimen/text_size_xlarge" android:visibility="gone" > </TextView><LinearLayout
android:id="@+id/ly_split_01" style="@style/train_query_hor_split_style" android:layout_below="@+id/rl_02" android:orientation="vertical" /> <!-- 到达城市 --><TextView
android:id="@+id/rl_03" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_below="@+id/ly_split_01" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="@string/to_station_hint" android:textColor="@color/font_title" android:textSize="@dimen/text_size_normal" /><TextView
android:id="@+id/choose_toStation" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@+id/ly_split_01" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/rl_03" android:background="@drawable/not_corner_selector" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="上海" android:textColor="@color/black" android:textSize="@dimen/text_size_xlarge" > </TextView><TextView
android:id="@+id/_choose_toStation" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@+id/ly_split_01" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/rl_03" android:background="@drawable/not_corner_selector" android:gravity="center_vertical" android:paddingLeft="5dp" android:textColor="@color/black" android:textSize="@dimen/text_size_xlarge" android:visibility="gone" > </TextView> </RelativeLayout>
private void changeStationAnim() {
_txtFromStation.setText(fromStation); _txtToStation.setText(toStation);String temp = fromStation;
fromStation = toStation; toStation = temp;_txtFromStation.setVisibility(View.VISIBLE);
_txtToStation.setVisibility(View.VISIBLE); txtFromStation.setVisibility(View.GONE); txtToStation.setVisibility(View.GONE);txtFromStation.setText(fromStation);
txtToStation.setText(toStation);int move = PubFun.dip2px(TrainQueryActivity.this, 40);
slideview(_txtFromStation, 0, move); slideview(_txtToStation, 0, -move); }
public void slideview(final View view, final float p1, final float p2) {
TranslateAnimation animation = new TranslateAnimation(0, 0, p1, p2); animation.setInterpolator(new OvershootInterpolator()); animation.setDuration(1000); animation.setStartOffset(300); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) { }@Override
public void onAnimationEnd(Animation animation) { _txtToStation.setVisibility(View.GONE); _txtFromStation.setVisibility(View.GONE); txtToStation.setVisibility(View.VISIBLE); txtFromStation.setVisibility(View.VISIBLE); // int left = view.getLeft(); // int top = view.getTop() + (int) (p2 - p1); // int width = view.getWidth(); // int height = view.getHeight(); // view.clearAnimation(); // view.layout(left, top, left + width, top + height); } }); view.startAnimation(animation); }