2016-07-30 9 views
1

ImageButtonを100%から0%に拡大しようとしています。その部分は機能していますが、ビューを画面の左上隅に向かって動かしているようにも見えます(0、0に向かって)。私はただそれを拡大したいとは思っていません。なぜこれもビューを翻訳していますか?不正確な座標を使用しているAndroidスケールアニメーション

ScaleAnimation実装XMLで

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

<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

私は目を変更した場合この

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

に電子ScaleAnimationコンストラクタそして、同じ位置にとどまるように見えるか、同じ位置に非常に近い4に8とcenterYcenterXを設定します。これは私には意味をなさない。この現象の原因は何ですか?

編集2

私は、ビューの翻訳を引き起こしているものを考え出したが、私はまだそれを修正するかどうかはわかりません。アニメーションの前にいつでもsetx()またはsetY()を使用してビューを移動すると、それはうんざりするようです。これは、アニメーションがまだ新しいビューではなく、ビューの元の位置を使用しているためです。したがって、ビューを平行移動すると、元の位置に向かって移動しています。 ScaleAnimationに正しい座標を使用させるにはどうすればよいですか?

答えて

0

ピボットXタイプを使用してください。ピボットタイプはAnimation.RELATIVE_TO_SELFです。

+0

'Animation.RELATIVE_TO_SELF'と両方のピボット値を両方とも '0.5f'に設定しても何も変更されませんでした – TychoTheTaco

0

ピボットXタイプを使用しようとしましたが、ピボットタイプはAnimation.RELATIVE_TO_SELFです。それは働いている私の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); 
     } 
    } 
} 

で私の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> 

、それはImageViewのの中心にスケールします。

関連する問題