我在活动转换时应用显示效果,当startActivity被调用通过显示效果显示的活动时。活动是注册活动,其中包含3个editText,一个imageView和一个用于拍摄图像的按钮。
“问题是每当我选择或点击editText时,显示效果动画正在应用。”
动画应用于根布局,动画在OnCreate方法中启动,我也尝试通过onStart和onResume方法等其他生命周期方法应用动画,但它正在运行。
很高兴接受答复。 这是代码的快照。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Name = (EditText) findViewById(R.id.enter_name_id);
Mobile = (EditText) findViewById(R.id.enter_mobile_id);
Email = (EditText) findViewById(R.id.enter_email_id);
imageView = (ImageView) findViewById(R.id.set_image);
LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
rootview.addOnLayoutChangeListener(new View.OnLayoutChangeListener()
{
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
float finalRadius=
(float)Math.hypot(v.getWidth(),v.getHeight());
int cx1 = (v.getLeft() + v.getRight()) / 2;
int cy1 = (v.getTop() + v.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(v,
cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.start();
}
});
}
答案 0 :(得分:0)
您可以使用globalLayoutListener等待
final LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
rootview .getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rootview .getViewTreeObserver().removeOnGlobalLayoutListener(this);
// remove listener
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
float finalRadius=
(float)Math.hypot(rootview.getWidth(),rootview.getHeight());
int cx1 = (rootview.getLeft() + rootview.getRight()) / 2;
int cy1 = (rootview.getTop() + rootview.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(v,
cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.start();
}
});
您可能不需要检查您支持的旧版本ID低于JELLY_BEAN
答案 1 :(得分:0)
问题在于,无论何时单击图像或编辑文本,都会调用onLayoutChange方法。只需在onCreate中启动动画,而不是将其放在onLayoutChanged中。
root.post(new Runnable() {
@Override
public void run() {
float finalRadius =
(float) Math.hypot(root.getWidth(), root.getHeight());
int cx1 = (root.getLeft() + root.getRight()) / 2;
int cy1 = (root.getTop() + root.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(root,
cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.start();
}
});
请试一试。
答案 2 :(得分:0)
您可以尝试这样
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
...
final LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
ViewTreeObserver observer = rootview.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
showLayout(rootview)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
rootview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
rootview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
}
...
...
}
private void showLayout(@NonNull View v) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
circularReveal(v);
} else {
v.setVisibility(View.VISIBLE);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void circularReveal(@NonNull final View v) {
float finalRadius = (float) Math.hypot(v.getWidth(), v.getHeight());
int cx1 = (v.getLeft() + v.getRight()) / 2;
int cy1 = (v.getTop() + v.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
v.setVisibility(View.VISIBLE);
anim.start();
}
在xml父布局中放置此android:visibility="invisible"