Uno*_*uny 2 android android-custom-view
我为自动动画创建了自定义布局,无论它可见还是消失。这是我的代码:
public class DidiRelativeLayout extends RelativeLayout {
private AnimUtils animUtils;
private int animVisible, animGone, durVisible, durGone;
public DidiRelativeLayout(Context context) {
super(context);
animUtils = AnimUtils.getInstance(context);
}
public DidiRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
animUtils = AnimUtils.getInstance(context);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DidiRelativeLayout, 0, 0);
try {
animVisible = ta.getInteger(R.styleable.DidiRelativeLayout_anim_visible, -1);
animGone = ta.getInteger(R.styleable.DidiRelativeLayout_anim_gone, -1);
durVisible = ta.getInteger(R.styleable.DidiRelativeLayout_anim_visible_duration, AttributeConst.DEFAULT_DURATION_ANIMATION);
durGone = ta.getInteger(R.styleable.DidiRelativeLayout_anim_gone_duration, AttributeConst.DEFAULT_DURATION_ANIMATION);
} finally {
ta.recycle();
}
}
public DidiRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
animUtils = AnimUtils.getInstance(context);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DidiRelativeLayout, defStyleAttr, 0);
try {
animVisible = ta.getInteger(R.styleable.DidiRelativeLayout_anim_visible, -1);
animGone = ta.getInteger(R.styleable.DidiRelativeLayout_anim_gone, -1);
durVisible = ta.getInteger(R.styleable.DidiRelativeLayout_anim_visible_duration, AttributeConst.DEFAULT_DURATION_ANIMATION);
durGone = ta.getInteger(R.styleable.DidiRelativeLayout_anim_gone_duration, AttributeConst.DEFAULT_DURATION_ANIMATION);
} finally {
ta.recycle();
}
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
try {
switch (visibility) {
case VISIBLE:
startAnimation(getAnim(animVisible, durVisible));
break;
case INVISIBLE:
case GONE:
startAnimation(getAnim(animGone, durGone));
break;
}
}
catch (Exception e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
}
它工作正常,但是当活动从某处恢复时,onVisibilityChanged自动调用。我该如何避免呢?谢谢。
更新:自定义布局的完整代码
是的,这是因为当您的活动再次可见时会调用onResume,因此您可以创建一个自定义方法并在需要时调用,替换为:
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
try {
switch (visibility) {
case VISIBLE:
startAnimation(getAnim(animVisible, durVisible));
break;
case INVISIBLE:
case GONE:
startAnimation(getAnim(animGone, durGone));
break;
}
}
catch (Exception e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
对此:
public void changeVisibility(int visibility) {
try {
switch (visibility) {
case VISIBLE:
startAnimation(getAnim(animVisible, durVisible));
break;
case INVISIBLE:
case GONE:
startAnimation(getAnim(animGone, durGone));
break;
}
}
catch (Exception e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
}
然后调用您的代码:
yourView.changeVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1163 次 |
| 最近记录: |