2016-07-30 49 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的中心。