中心向量与动画

时间:2016-06-09 10:59:26

标签: android android-animation

我有一个矢量,我试图在点击它之后扩展到更大,但它的位置总是在视口区域的左上角但是我想把它放在中心,动画应该让它更大底部的中心。我正在使用枢轴,但它没有帮助。这是我的代码:

的活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_baloon);
  androidImageView = (ImageView) findViewById(R.id.animated);
  if (androidImageView != null) {
    androidImageView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (isBig) {
          animateDown();
          isBig = false;
        } else {
          animateUp();
          isBig = true;
        }
      }
    });
  }
}

private void animateUp() {
  ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(androidImageView, "scaleX", 1.5f);
  ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(androidImageView, "scaleY", 1.5f);

  scaleDownX.setDuration(300);
  scaleDownY.setDuration(300);

  AnimatorSet scaleDown = new AnimatorSet();


  scaleDown.play(scaleDownX).with(scaleDownY);
  scaleDown.start();
}

private void animateDown() {
  ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(androidImageView, "scaleX", 1.0f);
  ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(androidImageView, "scaleY", 1.0f);

  scaleDownX.setDuration(300);
  scaleDownY.setDuration(300);

  AnimatorSet scaleDown = new AnimatorSet();

  scaleDown.play(scaleDownX).with(scaleDownY);
  scaleDown.start();
}

布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black"
    >
<RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="@android:color/white"
        >

<ImageView
            android:id="@+id/animated"
            android:layout_width="100dp"
            app:srcCompat="@drawable/animated_baloon"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            />
</RelativeLayout>

矢量:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="48dp"
    android:viewportHeight="327.5"
    android:viewportWidth="262.36"
    android:width="48dp"

    >
<group
        android:name="android"
        android:pivotX="163.75"
        android:pivotY="131.18"
        >
    <path
            android:fillColor="#FDC500"
            android:pathData="M-0.01,32.82a32.81,32.81 0,1 1,65.61 0c0,28.94 -32.8,49.04 -32.8,49.04S-0.01,61.77 -0.01,32.82Z"
            />
</group>

1 个答案:

答案 0 :(得分:0)

您需要使用视口高度和宽度,而不是android:heightwidth

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="24dp"
    android:viewportHeight="81.875"
    android:viewportWidth="32.795"
    android:width="24dp"
    >
 <!--  <group name="dot_name"
      pivotX="viewportHeight / 2"
      pivotY="viewportWidth / 2">  Calculate the values by yourself, write the calucalted values in there. This moves the reference point to the middle --> 
<group name="dot_name"
      pivotX="40.9375"
      pivotY="viewportWidth / 2"> 


<path
    android:fillColor="#FF000000"
    android:pathData="M-0.01,32.82a32.81,32.81 0,1 1,65.61 0c0,28.94 -32.8,49.04 -32.8,49.04S-0.01,61.77 -0.01,32.82Z"
/>
</group>
</vector>