2010-09-27 46 views
2

上我使用的是translate動畫轉化爲可見:在基本ImageView動畫:我應該如何的ImageView的出/屏幕

<?xml version="1.0" encoding="utf-8"?> 
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate 
    android:fromXDelta="-75%p" 
    android:toXDelta="0%p" 
    android:duration="1000" /> 
</set> 

。此動畫將從左側滑出圖像,直到它到達屏幕的右側邊緣。我在ImageView上設置了一個OnClickListener,它可以將其從滑出並切換到非常好的狀態。

問題:它似乎ImageView實際上移動它的座標,但它只是看起來它的移動。當ImageView只是部分可見(等待在屏幕上顯示動畫)時,如果我點擊某個區域,如果它被滑出,則動畫開始(OnClickListener被觸發)。

我沒有點擊ImageView

問題: 那麼,像這樣的動畫組件實際上並沒有移動? 我該如何處理這個onClick事件,因爲在用戶看不到ImageView的屏幕上按動會出現動畫會發生意外?

回答

4

你說得對,組件並不真正移動,只有它們渲染的位圖緩衝區。在屏幕上動畫之後,您可以使用View.setVisibility(View.GONE);使其真正消失。

如果您的目標是讓視圖的一小部分仍然可見,以允許用戶將其滑動回屏幕上,則可以考慮使用SlidingDrawer

+0

但如果它是真的消失,我想要的切換效果將不再可供用戶使用(視覺上)。他們不會知道他們可以再次點擊它。一旦動畫完成,它將基本上消失! – binnyb 2010-09-27 21:07:13

+0

也許你應該使用SlidingDrawer小部件? (更新我的回答) – 2010-09-27 22:13:04

+0

我發現'SlidingDrawer'小部件對於你能做什麼非常有限。似乎不可能改變它滑動的方向,使用更復雜的「手柄」,將窗口小部件放置在你想要的位置,等等。 – binnyb 2010-09-28 17:12:11

0

實際上,您可以創建自己的動畫來移動ImageView,這樣您就可以隨時點擊它。

當然,這將取決於你的情況,但說你想通過調整左邊距移動X軸的圖像視圖,你會是這樣的:

 
public class ViewLeftMarginAnimation extends Animation { 
    int startMargin; 
    int targetMargin; 
    View view; 

    public ViewGrowAnimation(View view, int start, int target) { 
     this.view = view; 
     this.start = height; 
     this.targetHeight = toHeight; 
     this.setInterpolator(new DecelerateInterpolator()); 
     this.setDuration(1000); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newMargin = (int) (startMargin + ((targetMargin - startMargin) * interpolatedTime)); 
     RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     p.leftMargin = newMargin; 
     view.setLayoutParams(p); 
     view.requestLayout(); 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, int parentHeight) { 
     super.initialize(width, height, parentWidth, parentHeight); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
}