2013-09-02 12 views
7

Tworzę animację, w której obraz jest przenoszony z jednego kierunku do drugiego, animacja przesuwa połowę obrazu na zewnątrz ekranu, ale obraz jest pełny, gdy animacja się kończy. Chcę pokazać połowę obrazu po animacji.Jak wyświetlić część obrazu na ekranie na końcu animacji

Obecnie używam pełnego obrazu w widoku obrazu, gdy animacja się rozpoczyna i zastępuję go półobrazem po zakończeniu animacji, ale pokazuje odbicie zmiany obrazu, które wygląda na niezręczne, co jest moim rzeczywistym problemem.

Poniżej znajduje się mój plik xml i klasy.

kod animacji

<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration = "2000" 
    android:fillBefore="true" 
    android:fillEnabled="true" 
    android:fromXDelta = "-300%" 
    android:fromYDelta="200%" 
    android:toXDelta="60%"> 

</translate> 

animimv5.setAnimationListener(new AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
       imv5.setBackgroundResource(R.drawable.img5); 
      } 
      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       imv5.setBackgroundResource(R.drawable.img5_2); 
      } 
     }); 
+1

Sprawdź ten wpis: http://stackoverflow.com/questions/3345084/how-can-i-animate-a-view-in-android-and-have-it-stay-in-the-new-position-size fillAfter = true fillEnabled = true – Juangcg

+0

spróbuj użyć 'fillAfter (true)' – Siddhesh

+0

Do jakiego zestawu SDK kierujesz? Mogę wysłać Ci pełne rozwiązanie za pomocą ObjectAnimatora, ale działa tylko z poziomu interfejsu API 11. – Ivelius

Odpowiedz

1

Można spróbować następujących czynności:

w XML (dodać go do ustawiania parametrów) android:fillAfter lub stosując metodę pokrewnych w źródle klasy setFillAfter(boolean)

Kiedy ustawiony na true transformacja animacji jest stosowana po zakończeniu animacji . Zgodnie z dokumentacją here Po ustawieniu wartości true transformacja animacji jest stosowana po zakończeniu animacji . Wartością domyślną jest false. Jeśli fillEnabled nie jest ustawiona na true, a animacja nie jest ustawiona w widoku, fillAfter ma wartość założoną jako true.

uzyskać dodatkowe wskazówki i objaśnienia sprawdzić Chet's blog post

Nadzieja to pomaga ..;)

0

Proszę bardzo, sir:

MainActivity : 

    public class MainActivity extends Activity { 

    private View animatedView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     animatedView = findViewById(R.id.view); 
    } 

    public void animate(View view) { 
     int targetX = ((View) animatedView.getParent()).getWidth() - animatedView.getWidth()/2; 
     ObjectAnimator.ofFloat(animatedView, "x", 0, targetX).setDuration(1000).start(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

Układ XML:

<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" 
       tools:context=".MainActivity"> 

    <TextView 
      android:id="@+id/view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#CC0000" 
      android:padding="30dp" 
      android:textColor="@android:color/white" 
      android:text="@string/hello_world"/> 
    <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Animate" 
      android:onClick="animate" 
      android:id="@+id/button" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true"/> 

</RelativeLayout> 
Powiązane problemy