gpt4 book ai didi

android - 获取焦点时在一个 View/ImageView 上实现 Android 缩放动画?

转载 作者:行者123 更新时间:2023-11-30 03:12:13 25 4
gpt4 key购买 nike

我想在获得焦点时为 View(或 ImageView)实现缩放动画。这是我当前的实现:

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>

最后获得焦点时我无法获得缩放效果,我只是看到它快速缩放并正常收缩到原始状态。为什么此实现无法按我的预期工作,我该如何解决?

最佳答案

好的,现在试试这个更新后的代码:--

这是 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);
}
}

关于android - 获取焦点时在一个 View/ImageView 上实现 Android 缩放动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20754389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com