2014-12-04 53 views
2
Animation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5); 
    anim.setFillAfter(true); // Needed to keep the result of the animation 
    anim.setDuration((long) durationPlayer); 
    imageView1.startAnimation(anim); 

這就是我要縮放ImageView的,我想如果可能的話做的就是點擊一個按鈕的刻度值,0.0F和1.0F之間。基本上我需要獲取ImageView的寬度和高度值,但直接檢查這些值只會返回寬度和高度,比例因子爲1.我已使用Google搜索,但找不到任何內容,是否意味着它完全可能?任何其他想法都會有所幫助。獲取imageviews當前刻度

簡而言之,它有可能在尺度動畫中獲取圖像視圖的大小。

回答

0

由於動畫在運行動畫時不會逐漸改變視圖的大小,因此您無法在動畫中間詢問ImageView的高度和寬度。它只會改變視圖自身的方式。

一個簡單的方法是在開始動畫時跟蹤時間戳,然後測量間隔直到用戶單擊按鈕。然後計算間隔的動畫持續時間有多遠,並將其乘以圖像的寬度和高度。像這樣:

long startTime; 
... 
imageView1.startAnimation(anim); 
startTime = SystemClock.uptimeMillis(); 

public void onClick(View v) { 
    if (v.getId() == R.id.my_button) { 
     long millisElapsed = SystemClock.uptimeMillis() - startTime; 
     double percentage = Math.max(0d, Math.min(1d, millisElapsed/(double) durationPlayer)); 
     int width = (int) (imageView1.getWidth() * percentage); 
     int height = (int) (imageView1.getHeight() * percentage); 
    } 
}