2012-10-16 30 views
5

Cần để làm phim hoạt hình tiếp theo (trên Android 2.2 trở lên):Trên xuống dưới - dịch hoạt hình

nút 1.Moving từ trên xuống dưới (sau khi nhấp vào anh ta),

2.Moving trở lại từ từ dưới lên trên (Sau khi nhấp lại vào anh ấy).

Hoạt ảnh đầu tiên hoạt động tốt, nhưng không phải thứ hai, btn "nhảy" từ dưới lên trên và không hoạt ảnh.

Code:

public class MainActivity extends Activity { 

static RelativeLayout relativeLayout; 
static Button btn; 
static Boolean isUp = true; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn = (Button) findViewById(R.id.button1); 
    relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout); 

    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      if(isUp){ 
       isUp = false; 
       v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0)); 
      }else{ 
       isUp = true; 
       v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0)); 
      } 
     } 
    }); 
} 


public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset) 
{ 
    TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition); 
    translateAnimation.setDuration(duration); 
    translateAnimation.setInterpolator(new AccelerateInterpolator()); 
    translateAnimation.setStartOffset(startOffset); 

    //Stop animation after finishing. 
    //translateAnimation.setFillAfter(true); 

    translateAnimation.setAnimationListener(new AnimationListener() 
    { 
    public void onAnimationStart(Animation animation) { } 
    public void onAnimationRepeat(Animation animation) { } 
    public void onAnimationEnd(Animation animation) { 
     btn.setY(toYPosition);   
    } 
    }); 

    return translateAnimation; 
    } 
} 

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/relative_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="Button" /> 

</RelativeLayout> 

Trả lời

10

Ok, tôi giải quyết nó.

Có vài issuses bạn nên biết về hoạt hình:

  1. Các paremeters hoạt hình không phải là đơn giản "Từ (vị trí cố định)" -> "Để (vị trí sửa chữa)" như bạn nên suy nghĩ. Có nhiều hơn như "Từ (vị trí hiện tại/0)" -> "Số bước cần thực hiện và theo hướng nào (cầu xin dương/âm cho âm)"

  2. Hoạt ảnh không thay đổi vị trí thực các quan điểm trên màn hình, vì vậy nếu bạn muốn ngăn chặn các hình ảnh động ở vị trí cuối cùng, bạn nên sử dụng:

    animation.setFillAfter(true); 
    
  3. Nếu bạn muốn thay đổi vị trí REAL của quan điểm bạn nên cập nhật các thông số xem trên "onAnimationEnd" (như mã bên dưới), hoặc tính toán vị trí và đặt vị trí Y/X theo cách thủ công (một lần nữa trên "onAnimationEnd"), như:

    animatedView.setY(stopPosition); 
    

Bộ luật:

public class AnimationActivity extends Activity { 

    private boolean isUp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        final float direction = (isUp) ? -1 : 1; 
        final float yDelta = getScreenHeight() - (2 * v.getHeight()); 
        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM; 

        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction); 

        animation.setDuration(500); 

        animation.setAnimationListener(new AnimationListener() { 

         public void onAnimationStart(Animation animation) { 
         } 

         public void onAnimationRepeat(Animation animation) { 
         } 

         public void onAnimationEnd(Animation animation) { 

          // fix flicking 
          // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker 
          TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
          anim.setDuration(1); 
          v.startAnimation(anim); 


          //set new params 
          LayoutParams params = new LayoutParams(v.getLayoutParams()); 
          params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
          params.addRule(layoutTopOrBottomRule); 
          v.setLayoutParams(params); 
         } 
        }); 

        v.startAnimation(animation); 

        //reverse direction 
        isUp = !isUp; 
       } 
      }); 
} 

private float getScreenHeight() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    return (float) displaymetrics.heightPixels; 

} 

}

+0

nó là thực sự hữu ích .Tôi muốn biết rằng làm thế nào để di chuyển một cái nhìn từ trên xuống dưới liên tục và nhận được vị thế của mình mỗi lần nó di chuyển từ mỗi bước sang bước tiếp theo. Làm thế nào để thực hiện nó bằng cách sử dụng hình ảnh động? Có phương pháp nào khác của họ không? Mục đích cuối cùng của tôi là phát hiện va chạm giữa chế độ xem chuyển động và chế độ xem này? cảm ơn bạn đã giúp đỡ – Ajay

-1
public class AnimationActivity extends Activity { 

    private boolean isUp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        final float direction = (isUp) ? -1 : 1; 
        final float yDelta = getScreenHeight() - (2 * v.getHeight()); 
        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM; 

        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction); 

        animation.setDuration(500); 

        animation.setAnimationListener(new AnimationListener() { 

         public void onAnimationStart(Animation animation) { 
         } 

         public void onAnimationRepeat(Animation animation) { 
         } 

         public void onAnimationEnd(Animation animation) { 

          // fix flicking 
          // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker 
          TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
          anim.setDuration(1); 
          v.startAnimation(anim); 


          //set new params 
          LayoutParams params = new LayoutParams(v.getLayoutParams()); 
          params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
          params.addRule(layoutTopOrBottomRule); 
          v.setLayoutParams(params); 
         } 
        }); 

        v.startAnimation(animation); 

        //reverse direction 
        isUp = !isUp; 
       } 
      }); 
} 

private float getScreenHeight() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    return (float) displaymetrics.heightPixels; 

} 
+0

bạn chỉ có thể đặt onClickListener mà không cần truyền tới Nút –

+0

Bạn vừa sao chép mã từ phía trên và dán nó .... – Denny

Các vấn đề liên quan