2014-12-04 45 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); 
    } 
}