android:如何使用动画显示视图的可见部分?

android:如何使用动画显示视图的可见部分?,android,animation,translate-animation,Android,Animation,Translate Animation,我有一个不可见的视图在另一个视图后面。我希望通过平移动画使此视图可见,并仅显示右侧视图的一部分。 这样地: 我不想使用9补丁图像和调整大小。 此动画在MS PowerPoint中命名为“偷窥”。您可以尝试以下内容 1) 创建一个可绘制的资源rectangle\u curve.xml,如下所示 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rec

我有一个不可见的
视图
在另一个
视图
后面。我希望通过平移动画使此视图可见,并仅显示右侧视图的一部分。 这样地:

我不想使用9补丁图像和调整大小。
此动画在MS PowerPoint中命名为“偷窥”。

您可以尝试以下内容

1) 创建一个可绘制的资源
rectangle\u curve.xml
,如下所示

       <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" android:padding="10dp">
        <solid android:color="#FFFFFF"/>
        <corners
            android:bottomRightRadius="350dp"
            android:bottomLeftRadius="0dp"
            android:topLeftRadius="0dp"
            android:topRightRadius="350dp"/>
        <stroke android:color="#50000000" android:width="2dp"/>
    </shape>
3) 创建一个类来处理展开/折叠动画,如下所示

        public class ResizeAnimation extends Animation {
            private int mWidth;
            private int mInitialWidth;
            private View mView;

            public ResizeAnimation(View view, int width) {
                mView = view;
                mWidth = width;
                mInitialWidth = view.getWidth();
            }

            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                int newWidth = mInitialWidth + (int) ((mWidth - mInitialWidth) * interpolatedTime);
                mView.getLayoutParams().width = newWidth;
                mView.requestLayout();
            }

            @Override
            public void initialize(int width, int height, int parentWidth, int parentHeight) {
                super.initialize(width, height, parentWidth, parentHeight);
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
       }            
     private void animate(View shapeView, boolean isExpand) {  
        int width = isExpand ? 500 : 0; // 500 is the max width in pixels , you ca change it
        ResizeAnimation anim = new ResizeAnimation(shapeView, width);
        anim.setDuration(500);
        shape.startAnimation(anim);
     }
4) 最后,像这样使用它

        public class ResizeAnimation extends Animation {
            private int mWidth;
            private int mInitialWidth;
            private View mView;

            public ResizeAnimation(View view, int width) {
                mView = view;
                mWidth = width;
                mInitialWidth = view.getWidth();
            }

            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                int newWidth = mInitialWidth + (int) ((mWidth - mInitialWidth) * interpolatedTime);
                mView.getLayoutParams().width = newWidth;
                mView.requestLayout();
            }

            @Override
            public void initialize(int width, int height, int parentWidth, int parentHeight) {
                super.initialize(width, height, parentWidth, parentHeight);
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
       }            
     private void animate(View shapeView, boolean isExpand) {  
        int width = isExpand ? 500 : 0; // 500 is the max width in pixels , you ca change it
        ResizeAnimation anim = new ResizeAnimation(shapeView, width);
        anim.setDuration(500);
        shape.startAnimation(anim);
     }
并按如下方式调用此函数

        public class ResizeAnimation extends Animation {
            private int mWidth;
            private int mInitialWidth;
            private View mView;

            public ResizeAnimation(View view, int width) {
                mView = view;
                mWidth = width;
                mInitialWidth = view.getWidth();
            }

            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                int newWidth = mInitialWidth + (int) ((mWidth - mInitialWidth) * interpolatedTime);
                mView.getLayoutParams().width = newWidth;
                mView.requestLayout();
            }

            @Override
            public void initialize(int width, int height, int parentWidth, int parentHeight) {
                super.initialize(width, height, parentWidth, parentHeight);
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
       }            
     private void animate(View shapeView, boolean isExpand) {  
        int width = isExpand ? 500 : 0; // 500 is the max width in pixels , you ca change it
        ResizeAnimation anim = new ResizeAnimation(shapeView, width);
        anim.setDuration(500);
        shape.startAnimation(anim);
     }
对于展开动画:
animate(myBackgroundView,true)

对于折叠动画:
animate(myBackgroundView,false)

编辑: 在第4步的这一行中:
intwidth=isExpand?500:0;
使用1而不是0。 0无法正常工作。我不知道原因

private void animate(View view, boolean isExpand) {
    int width = isExpand ? 200 : 1; // 200 is the max width in pixels.
    //use a factor for same width on all screen size.
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int factor =(int)  metrics.density;
    ResizeAnimation anim = new ResizeAnimation(view, width * factor);
    anim.setDuration(500);
    view.startAnimation(anim);
}