2013-12-24 28 views
1

我想實現一個縮放動畫查看(或ImageView),當它獲得焦點。這是我目前的執行:獲得焦點時,在一個View/ImageView上實現Android縮放動畫嗎?

public class ScaleFocusImageView extends ImageView { 
    private Context mContext; 

    public ScaleFocusImageView(Context context, AttributeSet attr) { 
     super(context, attr); 
     mContext = context; 
    } 

    @Override 
    protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect) { 
     super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 
     if(gainFocus) { 
      Animation a = AnimationUtils.loadAnimation(mContext, R.anim.scale_up); 
      this.startAnimation(a); 
     } 

    } 
} 

這裏是我的動畫XML「scale_up.xml」:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale 
    android:fromXScale="1.0" 
    android:toXScale="1.5" 
    android:fromYScale="1.0" 
    android:toYScale="1.5" 
    android:pivotX="50%p" 
    android:pivotY="50%p" 
    android:duration="175"/> 
</set> 

得到集中在年底的時候,我不能獲得規模效應,我剛纔看到它正常快速縮放並縮小到原來的大小。爲什麼這個實現不能如我所期望的那樣工作,我該如何解決它?

+0

的動畫作品所以實際上你想要什麼?希望動畫應該留在焦點上imageview? –

+0

我希望在焦點時縮放視圖,並在僞裝消失時變爲正常尺寸,謝謝 – gladman

+0

確定讓我檢查我的機器 –

回答

1

好了,現在試試這個更新的代碼: -

這是ImageView的類: -

public class ScaleFocusImageView extends ImageView { 
private Context mContext; 
boolean flag; 

public ScaleFocusImageView(Context context, AttributeSet attr) { 
    super(context, attr); 
    mContext = context; 
} 

@Override 
protected void onFocusChanged(boolean gainFocus, int direction, 
     Rect previouslyFocusedRect) { 
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 
    if (gainFocus) { 
     zoom(1f, 1f, new PointF(getWidth()/2, getHeight()/2)); 
    } else { 
     zoom(2f, 2f, new PointF(getWidth()/2, getHeight()/2)); 
    } 

} 

/** zooming is done from here */ 
@SuppressLint("NewApi") 
public void zoom(Float scaleX, Float scaleY, PointF pivot) { 
    setPivotX(pivot.x); 
    setPivotY(pivot.y); 
    setScaleX(scaleX); 
    setScaleY(scaleY); 
} 
} 
+0

動畫的作品,但爲什麼當視圖獲得焦點的位置移動?我在演示中使用了LinearLayout。 – gladman

+0

好吧,現在再試試我已經編輯了答案 –

+0

還沒有,當它獲得焦點時,視圖縮放並向右移動,然後再回到正常並且再次移回原始軌跡。 – gladman

相關問題