2016-07-30 117 views
1

我试图将ImageButton从100%缩放到0%。该部分正在工作,但它似乎也正朝着屏幕的左上角(朝向0,0)移动视图。我不希望我只是想扩展它。为什么这也是翻译的观点?使用不正确坐标的Android缩放动画

的ScaleAnimation实施

if (view.getVisibility() == 0) { 
     final ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.ABSOLUTE, centerX, Animation.ABSOLUTE, centerY); 
     scaleAnimation.setInterpolator(interpolator); 

    scaleAnimation.setDuration(5000); 
      view.startAnimation((Animation)scaleAnimation); 

     scaleAnimation.setAnimationListener(new Animation.AnimationListener(){ 

       public void onAnimationEnd(Animation animation) { 
        view.setVisibility(n); 
       } 

       public void onAnimationRepeat(Animation animation) { 
       } 

       public void onAnimationStart(Animation animation) { 
       } 
      }); 
    } 
} 

ImageButton和其父android.support.design.widget.CoordinatorLayout在XML:

<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 

<ImageButton 
    android:id="@+id/buttonToggleResultsWindow0" 
    android:layout_width="35dp" 
    android:layout_gravity="top|end" 
    android:layout_height="35dp" 
    android:visibility="gone" 
    android:hint="Hide results window" 
    android:backgroundTint="@color/accent" 
    android:src="@drawable/ic_filter_tilt_shift_white_24dp" 
    android:onClick="toggleResultsWindow" 
    android:background="@drawable/ripple_circle" 
/> 

编辑1

如果我改变日ËScaleAnimation构造这个

final ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, centerX, Animation.RELATIVE_TO_SELF, centerY); 

并设置centerX至8 centerY 4似乎停留在相同的位置或非常接近相同的位置。这对我来说没有任何意义。什么导致了这种行为?

编辑2

我想通了,是什么原因造成的意见翻译,但我仍然不知道如何解决它。看起来如果我在动画之前的任何时候使用setx()setY()来移动视图,那么它会变得混乱。这是因为动画仍在使用视图的原始位置,而不是新的位置。所以当它翻译视图时,它正在被翻译成原始位置。我怎样才能使ScaleAnimation使用正确的坐标?

回答

0

尝试使用pivotXType和pivotYType为Animation.RELATIVE_TO_SELF

+0

将两者都设置为'Animation.RELATIVE_TO_SELF'且两个值都转换为'0.5f'并没有改变任何东西 – TychoTheTaco

0

尝试使用pivotXType和pivotYType为Animation.RELATIVE_TO_SELF

在我activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ff0000" 
    tools:context="com.sonnv1368.animationscale.MainActivity"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/ic_launcher" /> 
</RelativeLayout> 

在我MainActivity.class

public class MainActivity extends AppCompatActivity { 
    private ImageView imageView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     imageView = (ImageView) findViewById(R.id.imageView); 
     animationImageView(); 
    } 

    private void animationImageView() { 
     if (imageView.getVisibility() == View.VISIBLE) { 
      final ScaleAnimation scaleAnimation = new ScaleAnimation(
        1.0f, 0.0f, 1.0f, 0.0f, 
        Animation.RELATIVE_TO_SELF, 0.5f, 
        Animation.RELATIVE_TO_SELF, 0.5f); 
      scaleAnimation.setDuration(5000); 
      imageView.startAnimation(scaleAnimation); 
     } 
    } 
} 

它工作,它会在扩展的imageview的中心。