【问题标题】:android: how to visible portion of a view with animation?android:如何用动画显示视图的可见部分?
【发布时间】:2019-03-20 16:31:49
【问题描述】:

我在另一个View 后面有一个不可见的View。我想通过翻译动画使该视图可见,并仅显示右侧视图的一部分。 像这样:

我不想使用 9 个补丁图像并调整它的大小。 此动画在 MS-PowerPoint 中名为“peek in”。

【问题讨论】:

    标签: android animation translate-animation


    【解决方案1】:

    你可以试试这样的

    1)创建一个drawable资源rectangle_curved.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>
    

    2) 将此设置为view 的背景,该view 正在展开和折叠。这里我使用了一个Framelayout,它正在扩展和折叠

         <FrameLayout
            android:id="@+id/shape"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/rectangle_curved"/>
    

    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;
                }
           }            
    

    4) 最后,像这样使用它。创建一个函数来处理动画如下

         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 步的这一行中:int width = 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-25
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多