2013-04-10 18 views
28

Tôi đang sử dụng objectAnimator để tạo hoạt ảnh cho nút từ dưới lên trên trong Android. Bây giờ tôi đang sử dụng mã bên dướiCách cung cấp giá trị phần trăm trong ObjectAnimator trong Android

ObjectAnimator transAnimation = ObjectAnimator.ofFloat(button,"translationY",0,440); 
     transAnimation.setDuration(440); 
     transAnimation.start(); 

Tôi cũng đã thử với mã mẫu bên dưới. Nhưng vẫn còn những vấn đề tồn tại

ObjectAnimator transAnimation = ObjectAnimator.ofFloat(loginLayout, "translationY",0f,0.8f); 
        transAnimation.setDuration(480); 
        transAnimation.start(); 

Nó hoạt động tốt trong các thiết bị màn hình lớn. Nhưng khi nói đến các thiết bị màn hình nhỏ, nó sẽ tắt màn hình. Tôi muốn giữ nó ở trên cùng của màn hình bất kể kích thước màn hình khác nhau. Tôi nghĩ rằng tôi phải đưa ra các giá trị theo tỷ lệ phần trăm (nói 0% đến 100% hoặc 0 đến 100% p). Vì vậy, câu hỏi của tôi là làm thế nào để cung cấp cho các giá trị trong tỷ lệ phần trăm trong objectAnimator trong Android. Tôi cũng nhận thấy một điều rằng objectAnimator này chỉ được giới thiệu trong HoneyComb. Vì vậy, có bất kỳ thư viện tương thích ngược để chạy nó trong các phiên bản thấp. Bất cứ ai có thể hướng dẫn tôi để tìm một giải pháp cho việc này.

Tôi cũng đã thử mở rộng Xem và viết getter và setter cho thuộc tính trong offset(). Tuy nhiên nó không di chuyển hoàn toàn lên đầu màn hình. Đây là mã tôi đã sử dụng.

@SuppressLint("NewApi") public float getXFraction() 
    { 
     int width = context.getWindowManager().getDefaultDisplay().getHeight(); 
     return (width == 0) ? 0 : (getY()); 
    } 

    @SuppressLint("NewApi") public void setXFraction(float xFraction) { 
     int width = context.getWindowManager().getDefaultDisplay().getWidth(); 
     setY((width > 0) ? (width) : 0); 
    } 

Cảm ơn inAdvance

+1

Đối với những người đang tìm kiếm câu trả lời, [ở đây] (http://stackoverflow.com/a/11015834/1083957) là giải pháp thay thế. – azizbekian

Trả lời

7

Để sử dụng ObjectAnimator trên các thiết bị pre-Honeycomb thêm NineOldAndroids thư viện để dự án của bạn và thay đổi khẩu của bạn để sử dụng com.nineoldandroids.animation.ObjectAnimator.

Để sử dụng giá trị phần trăm trong ObjectAnimator thay vì getXFractionsetXFraction bạn phải thêm getYFractionsetYFraction phương pháp như thế này

public float getYFraction() { 
    final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 
    int height = wm.getDefaultDisplay().getHeight(); 
    return (height == 0) ? 0 : getY()/(float) height; 
} 

public void setYFraction(float yFraction) { 
    final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 
    int height = wm.getDefaultDisplay().getHeight(); 
    setY((height > 0) ? (yFraction * height) : 0); 
} 

Và sau đó bạn có thể tạo xml tập tin project-folder/res/animator/move_bottom.xml như thế này

<?xml version="1.0" encoding="utf-8"?> 
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="500" 
    android:propertyName="yFraction" 
    android:valueFrom="0" 
    android:valueTo="0.8" 
    android:valueType="floatType" /> 

Hoặc tạo hoạt ảnh trong mã

final LoginLayout loginLayout = (LoginLayout) findViewById(R.id.login_layout); 
final ObjectAnimator oa = ObjectAnimator.ofFloat(loginLayout, "yFraction", 0f, 0.8f); 
oa.start(); 
+0

thực sự tôi đã cố gắng này ... nhưng nó vẫn đi ra khỏi màn hình .. và một điều nữa nó đi bất ngờ đến đầu màn hình và sau đó quay trở lại phía dưới.why nó ​​xảy ra? là có bất kỳ phương pháp nào như setFillAfter = "true" trong lớp objectAnimator để dừng nó ở điểm cuối và không quay trở lại vị trí ban đầu. –

+0

Tôi nghiêm túc nghĩ rằng tôi đã thực hiện một số sai lầm trong phương thức củaFloat (target, propertyName, int, int); hai tham số cuối cùng là int, int. Các tham số này quyết định điểm bắt đầu và điểm kết thúc của hoạt ảnh. Nhưng trong đơn vị nào, phương thức này đang chờ đợi các giá trị ... tôi có nghĩa là liệu nó có mong đợi các pixel hoặc trong dp hay không. tại sao nó không hoạt động khi tôi đã tìm ra chiều cao màn hình của thiết bị và truyền giá trị cho phương thức ofFloat(). Nó vẫn vượt ra ngoài màn hình. Tại sao ?? xin vui lòng cho tôi biết suy nghĩ và đề xuất của bạn. Tôi có làm gì sai không? –

+0

Trong phương thức 'ofFloat', hai hoặc nhiều tham số cuối cùng là float. Xem tài liệu chính thức [tại đây] (http://developer.android.com/reference/android/animation/ObjectAnimator.html#ofFloat%28java.lang.Object,%20java.lang.String,%20float...%29) – vasart

2

Nếu bạn muốn tạo hoạt ảnh từ bên ngoài màn hình bằng ObjectAnimator. Bạn có thể làm điều này với kích thước màn hình Android.

@Override 
    public void onCreate(Bundle savedInstanceState) { 

     ... 

     sampleImage = BitmapFactory.decodeResource(getResources(), R.drawable.sample); 

     myImage = (ImageView) findViewById(R.id.image_view); 
     myImage.setImageBitmap(sampleImage); 

     //In case API Level is 14 above. 
     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     float displayWidth = size.x; 

     //In case API Level is 13 below.  
     //WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
     //Display display = wm.getDefaultDisplay(); 
     //displayWidth = display.getWidth(); 

     animator = ObjectAnimator.ofFloat(myImage, "translationX", displayWidth, 0); 
     animator.setDuration(500); 

    } 

Xin lỗi, nếu đề xuất của tôi bị mất điểm.

2

Hãy thử sử dụng một tài sản tùy chỉnh cho ObjectAnimator, thay vì tạo giao diện tùy chỉnh với tài sản tùy chỉnh, bạn có thể tạo ra một tài sản tùy chỉnh mà chấp nhận một 'phần':

Property<View, Float> property = new Property<View, Float>(Float.TYPE, "translation_x_fraction") { 
     @Override public Float get(View view) { 
      return view.getWidth() <= 0 ? 0 : view.getTranslationX()/view.getWidth(); 
     } 

     @Override public void set(View view, Float value) { 
      view.setTranslationX(view.getWidth() * value); 
     } 
} 

ObjectAnimator.ofFloat(view, property, 0f, 1f); 
0

Tôi đã làm theo cách này, tôi đã dành quá nhiều thời gian nhưng nó sẽ tiết kiệm thời gian của bạn.

Arguments:

  1. Xem (hỗ trợ Xem của bạn)
  2. Chánh rộng (Root of View của bạn)
  3. Child rộng (Trên mà bạn muốn áp dụng Animation)
  4. % (Bao nhiêu phần trăm e bạn muốn di chuyển đối tượng hoặc chế độ xem của bạn)

Đừng sợ với các đối số, tôi giải thích ở đây bằng ví dụ.

XMLJAVA mã:

activity_main.xml:

<LinearLayout 
    android:id="@+id/llProgressParent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="5dp" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:weightSum="1" > 

    <View 
     android:id="@+id/viewSuppot" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0" > 
    </View> 

    <ImageView 
     android:id="@+id/ivProgressChild" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 

MainActivity.java:

private LinearLayout llProgressParent; 
private View viewSuppot; 
private ImageView ivProgressChild; 
private int childWidth, parentWidth; 
private int percentage = 75; 

trên onCreate():

viewSuppot = findViewById(R.id.viewSuppot); 

llProgressParent = (LinearLayout)findViewById(R.id.llProgressParent); 
llProgressParent.post(new Runnable() { 
     @Override 
     public void run() { 
      parentWidth = llProgressParent.getMeasuredWidth(); 
     } 
    }); 


ivProgressChild = (ImageView)findViewById(R.id.ivProgressChild); 
ivProgressChild.post(new Runnable() { 
     @Override 
     public void run() { 
      childWidth = ivProgressChild.getMeasuredWidth(); 
     } 
    }); 

Phương pháp:

private void animateViewByPercentageOrProgrss(final View view, int parentWidth, int childWidth, int percentage){ 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     ValueAnimator anim = ValueAnimator.ofInt(view.getMeasuredWidth(), (int) (((float) percentage * parentWidth)/100)-(childWidth/2)); 
     anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator valueAnimator) { 
       int val = (Integer) valueAnimator.getAnimatedValue(); 
       ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); 
       layoutParams.width = val; 
       view.setLayoutParams(layoutParams); 
      } 
     }); 
     anim.setDuration(2000); 
     anim.start(); 
    } 

Làm thế nào để gọiPhương pháp:

animateViewByPercentageOrProgrss(viewSuppot, parentWidth, childWidth, percentage); 

Xong nó.