2012-08-26 4 views
12

là có một cách để hiển thị một ImageView dần từ trên xuống dưới, như thế này:Làm thế nào để hiển thị một ImageView dần xuống từ trên xuống dưới

enter image description here

Xin lỗi vì sự hoạt hình không hấp dẫn.

+0

Bạn muốn 'ImageView' hoạt ảnh theo cách đó? Hoặc bạn muốn làm điều đó khi người dùng làm điều gì đó? – Luksprog

+0

không chắc chắn sự khác biệt là gì nhưng tôi sẽ nói một mình – ibiza

Trả lời

19

Tôi không quen thuộc với các hoạt ảnh trên Android, nhưng một cách (một chút tẻ nhạt) là quấn hình ảnh trong một ClipDrawable và tạo hiệu ứng giá trị level của nó. Ví dụ:

<ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/clip_source" /> 

đâu clip_source là một drawable:

<?xml version="1.0" encoding="utf-8"?> 
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:clipOrientation="vertical" 
    android:drawable="@drawable/your_own_drawable" 
    android:gravity="bottom" /> 

Sau đó, trong mã bạn sẽ có:

// a field in your class 
private int mLevel = 0; 

ImageView img = (ImageView) findViewById(R.id.imageView1); 
mImageDrawable = (ClipDrawable) img.getDrawable(); 
mImageDrawable.setLevel(0); 
mHandler.post(animateImage); 

Các animateImage là một đối tượng Runnable:

private Runnable animateImage = new Runnable() { 

     @Override 
     public void run() { 
      doTheAnimation(); 
     } 
    }; 

và phương pháp doTheAnimation:

private void doTheAnimation() { 
    mLevel += 1000; 
    mImageDrawable.setLevel(mLevel); 
    if (mLevel <= 10000) { 
     mHandler.postDelayed(animateImage, 50); 
    } else { 
     mHandler.removeCallbacks(animateImage); 
    } 
} 
+0

Thú vị! MHandler trong trường hợp đó là gì? – ibiza

+0

@ibiza Đó là một ví dụ 'Handler', 'Handler mHandler = new Handler();'. – Luksprog

+0

Tôi dường như không thể làm cho phương pháp này hoạt động, tôi luôn nhìn thấy hình ảnh đầy đủ – ibiza