美好的一天,我有一个以编程方式放置在SurfaceView左下角的视图,我想要的是当点击一个按钮时,视图滑出屏幕并滑回,但似乎没有发生在动画。这是我的代码,我将把相关部分:
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_Info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#6444"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:id="@+id/name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="name" />
<TextView
android:id="@+id/medium_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="med"/>
<TextView
android:id="@+id/dimensions_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="dimen" />
</LinearLayout>
定位视图:
RelativeLayout.param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 240);
param.leftMargin = 0;
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_info, null);
mylayout = (LinearLayout)view.findViewById(R.id.gallery_Info_root_id); //what i want to animate
view.setLayoutParams(param);
param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
view.getLayoutParams().width = 380;
RelativeLayout lContainerLayout = new RelativeLayout(VideoPlayback.this);
lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT , LayoutParams.MATCH_PARENT ));
lContainerLayout.addView(view);
addContentView(lContainerLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
滑入并滑出动画:
private void slideInView(){
Animation slidein = new MyTranslateAnimation(mylayout, -mylayout.getWidth(), 0, 0, 0);
slidein.setDuration(animationDuration);
slidein.setInterpolator(interpolator);
mylayout.startAnimation(slidein);
}
private void slideOutView(){
Animation slidein = new MyTranslateAnimation(mylayout, 0, -mylayout.getWidth(), 0, 0);
slidein.setDuration(animationDuration);
slidein.setInterpolator(interpolator);
mylayout.startAnimation(slidein);
}
我从here获得的MyTranslateAnimation
public class MyTranslateAnimation extends Animation {
private View view;
private final float fromX;
private final float toX;
private final float fromY;
private final float toY;
public MyTranslateAnimation(View v, float fromX, float toX, float fromY, float toY) {
view = v;
this.fromX = fromX;
this.toX = toX;
this.fromY = fromY;
this.toY = toY;
setDuration(500);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float x = (toX-fromX) * interpolatedTime + fromX;
float y = (toY - fromY) * interpolatedTime + fromY;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
params.setMargins((int)x, (int)y, 0, 0);
view.requestLayout();
}
}
任何人都可以发现我做错的事吗?因为我似乎无法理解为什么我不能将“mylayout”滑入和滑出。提前谢谢。