Interpolator 和 属性动画结合使用 : private static class WorkspaceOvershootInterpolator implements Interpolator { private static final float DEFAULT_TENSION = 1.3f; private float mTension; public WorkspaceOvershootInterpolator() { mTension = DEFAULT_TENSION; } public void setDistance(int distance) { mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION; } public void disableSettle() { mTension = 0.f; } public float getInterpolation(float t) { // _o(t) = t * t * ((tension + 1) * t + tension) // o(t) = _o(t - 1) + 1 t -= 1.0f; return t * t * ((mTension + 1) * t + mTension) + 1.0f; } }
TextView tv = (TextView) this.findViewById(R.id.tv); ObjectAnimator oa = ObjectAnimator.ofFloat(tv, "scaleX", 0f, 1f); oa.setDuration(3000); oa.setInterpolator(new WorkspaceOvershootInterpolator()); oa.addListener(new AnimatorListenerAdapter() { public void onAnimationEnd(Animator animation) { Log.i("Animation", "end"); } }); oa.start();
如何移动一个view
1.动画
2.调用layout
public static class LayoutParams extends FrameLayout.LayoutParams { public int x, y; public boolean customPosition = false; /** * {@inheritDoc} */ public LayoutParams(int width, int height) { super(width, height); } public void setWidth(int width) { this.width = width; } public int getWidth() { return width; } public void setHeight(int height) { this.height = height; } public int getHeight() { return height; } public void setX(int x) { this.x = x; } public int getX() { return x; } public void setY(int y) { this.y = y; } public int getY() { return y; } } protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); final FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) child.getLayoutParams(); if (flp instanceof LayoutParams) { final LayoutParams lp = (LayoutParams) flp; if (lp.customPosition) { child.layout(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height); } } } }
3.设置LayoutParam
//---------------------------------------------------------- ViewGroup parentitem = (ViewGroup)this.getChildAt(2); View item = parentitem.getChildAt(0); //是哪一种 LayoutParams 由 item 的父视图的类型决定 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) item.getLayoutParams(); layoutParams.leftMargin = 100; item.setLayoutParams(layoutParams); item.invalidate(); item.requestLayout();
4.scroller