首页 文章

如何在Android中的ObjectAnimator中给出百分比值

提问于
浏览
29

我正在使用objectAnimator在Android中从下到上动画一个按钮 . 现在我使用下面的代码

ObjectAnimator transAnimation = ObjectAnimator.ofFloat(button,"translationY",0,440);
        transAnimation.setDuration(440);
        transAnimation.start();

我也试过下面的示例代码 . 但问题仍然存在

ObjectAnimator transAnimation = ObjectAnimator.ofFloat(loginLayout, "translationY",0f,0.8f);
                    transAnimation.setDuration(480);
                    transAnimation.start();

它适用于大屏幕设备 . 但是当谈到小屏幕设备时,它就会离开屏幕 . 无论屏幕大小不同,我都希望将其保留在屏幕顶部 . 我想我必须给出百分比值(比如0%到100%或0到100%p) . 所以我的问题是如何在Android中的objectAnimator中给出百分比值 . 我还注意到这个objectAnimator只在HoneyComb中引入了一件事 . 那么是否有任何向后兼容库可以在低版本中运行它 . 任何人都可以指导我找到解决方案 .

我还尝试在 offset() 中扩展View并为该属性编写getter和setter . 它仍然没有完全移动到屏幕顶部 . 这是我用过的代码 .

@SuppressLint("NewApi") public float getXFraction()
    {
        int width = context.getWindowManager().getDefaultDisplay().getHeight();
        return (width == 0) ? 0 : (getY());
    }

    @SuppressLint("NewApi") public void setXFraction(float xFraction) {
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        setY((width > 0) ? (width) : 0);
    }

提前致谢

4 回答

  • 2

    要在预蜂窝设备上使用 ObjectAnimator ,请将NineOldAndroids库添加到项目中,并将导入更改为使用 com.nineoldandroids.animation.ObjectAnimator .

    要在 ObjectAnimator 中使用百分比值而不是 getXFractionsetXFraction ,您必须添加 getYFractionsetYFraction 这样的方法

    public float getYFraction() {
        final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        return (height == 0) ? 0 : getY() / (float) height;
    }
    
    public void setYFraction(float yFraction) {
        final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        setY((height > 0) ? (yFraction * height) : 0);
    }
    

    然后你可以像这样创建xml文件 project-folder/res/animator/move_bottom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="yFraction"
        android:valueFrom="0"
        android:valueTo="0.8"
        android:valueType="floatType" />
    

    或者在代码中创建动画

    final LoginLayout loginLayout = (LoginLayout) findViewById(R.id.login_layout);
    final ObjectAnimator oa = ObjectAnimator.ofFloat(loginLayout, "yFraction", 0f, 0.8f);
    oa.start();
    
  • 2

    如果要使用ObjectAnimator从屏幕外部创建动画 . 您可以使用Android显示大小执行此操作 .

    @Override
        public void onCreate(Bundle savedInstanceState) {
    
            ...
    
            sampleImage = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
    
            myImage = (ImageView) findViewById(R.id.image_view);
            myImage.setImageBitmap(sampleImage);
    
            //In case API Level is 14 above.
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            float displayWidth = size.x;
    
            //In case API Level is 13 below.       
            //WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            //Display display = wm.getDefaultDisplay();
            //displayWidth = display.getWidth();
    
            animator = ObjectAnimator.ofFloat(myImage, "translationX", displayWidth,  0);
            animator.setDuration(500);
    
        }
    

    对不起,如果我的建议错过了这一点 .

  • 0

    尝试使用ObjectAnimator的自定义属性,而不是使用自定义属性创建自定义视图,您可以创建一个接受“小数”的自定义属性:

    Property<View, Float> property = new Property<View, Float>(Float.TYPE, "translation_x_fraction") {
            @Override public Float get(View view) { 
                return view.getWidth() <= 0 ? 0 : view.getTranslationX() / view.getWidth();
            }
    
            @Override public void set(View view, Float value) {
                view.setTranslationX(view.getWidth() * value);
            }
    }
    
    ObjectAnimator.ofFloat(view, property, 0f, 1f);
    
  • 8

    我这样做了,我花了太多时间,但它会节省你的时间 .

    Arguments:

    • View (您的支持人员查看)

    • Parent width (您的视图的根)

    • Child width (您要在其上应用动画)

    • % (您想要移动对象或视图的百分比)

    不要害怕参数,我在这里通过例子解释 .

    XMLJAVA 代码:

    activity_main.xml

    <LinearLayout
         android:id="@+id/llProgressParent"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_margin="5dp"
         android:layout_gravity="center"
         android:orientation="horizontal"
         android:weightSum="1" >
    
         <View
            android:id="@+id/viewSuppot"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0" >
         </View>
    
         <ImageView
            android:id="@+id/ivProgressChild"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher" />
    
    </LinearLayout>
    

    MainActivity.java

    private LinearLayout llProgressParent;
    private View viewSuppot;
    private ImageView ivProgressChild;
    private int childWidth, parentWidth;
    private int percentage = 75;
    

    onCreate()

    viewSuppot = findViewById(R.id.viewSuppot);
    
    llProgressParent = (LinearLayout)findViewById(R.id.llProgressParent);
    llProgressParent.post(new Runnable() {
          @Override
          public void run() {
              parentWidth = llProgressParent.getMeasuredWidth();
          }
      });
    
    
     ivProgressChild = (ImageView)findViewById(R.id.ivProgressChild);
     ivProgressChild.post(new Runnable() {
          @Override
          public void run() {
              childWidth = ivProgressChild.getMeasuredWidth();
          }
      });
    

    Method:

    private void animateViewByPercentageOrProgrss(final View view,  int parentWidth, int childWidth, int percentage){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ValueAnimator anim = ValueAnimator.ofInt(view.getMeasuredWidth(), (int) (((float) percentage * parentWidth) / 100)-(childWidth/2));
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int val = (Integer) valueAnimator.getAnimatedValue();
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    layoutParams.width = val;
                    view.setLayoutParams(layoutParams);
                }
            });
            anim.setDuration(2000);
            anim.start();
        }
    

    How 来调用 Method

    animateViewByPercentageOrProgrss(viewSuppot, parentWidth, childWidth, percentage);
    

    Its Done .

相关问题