J'ai plusieurs ImageView
dans un RelativeLayout
. maintenant, lorsque l'utilisateur appuie sur une imageView, je veux qu'elle soit déplacée vers un emplacement spécifié avec une animation subtile.
Par exemple; J'ai initialement défini des marges pour LayoutParams
associées à un ImageView
comme layoutparams1.setMargins(90,70,0,0);
, puis je l'ai ajouté à la mise en page.
et quand imageview est tapée, j'aimerais que son nouvel emplacement soit 200,200
, avec animation.
Alors, est-ce possible? si oui, alors comment?
Notez que j'ai à la fois RelativeLayout
et tous ses enfants ImageView
s créés par programme.
Et je suis nouveau dans le développement Android donc une réponse élaborée est attendue.
TranslateAnimation animation = new TranslateAnimation(0, 50, 0, 100);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
imageView.startAnimation(animation);
PDATE: Le problème est que le View
est en fait toujours dans son ancienne position. Nous devons donc le déplacer lorsque l'animation est terminée. Pour détecter quand l'animation est terminée, nous devons créer notre propre animationListener
(à l'intérieur de notre classe activity
):
private class MyAnimationListener implements AnimationListener{
@Override
public void onAnimationEnd(Animation animation) {
imageView.clearAnimation();
LayoutParams lp = new LayoutParams(imageView.getWidth(), imageView.getHeight());
lp.setMargins(50, 100, 0, 0);
imageView.setLayoutParams(lp);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
Ainsi, le onClickEvent
sera renvoyé à son nouvel emplacement. L'animation va maintenant le déplacer encore plus vers le bas, donc vous voudrez peut-être enregistrer les x
et y
dans une variable, de sorte que dans la onAnimationEnd()
vous ne la déplaciez pas un emplacement fixe.
il vaut mieux aussi utiliser ObjectAnimator
. cette vue de déplacement dans une nouvelle position. par exemple :
ImageView splash ;
@Override
public boolean onTouchEvent(MotionEvent event) {
float tx = event.getX();
float ty = event.getY();
int action = event.getAction();
switch(action) {
case MotionEvent.ACTION_DOWN:
tx = event.getX();
ty = event.getY();
// findViewById(R.id.character).setX(tx-45);
// findViewById(R.id.character).setY(ty-134);
ObjectAnimator animX = ObjectAnimator.ofFloat(splash, "x", tx-45);
ObjectAnimator animY = ObjectAnimator.ofFloat(splash, "y", ty-134);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();
break;
default:
}
return true;
}
Dans le code ci-dessous, j'ajoute une vue d'image au centre sur la disposition du cadre de manière dynamique. Après l'ajout, j'augmente la mise à l'échelle et définit l'alpha pour donner un effet de zoom et après l'animation complète, je ne fais que traduire mon image d'une position à une autre position.
Ajouter une vue d'image sur framelayout
imgHeart = new ImageView(getBaseContext());
imgHeart.setId(R.id.heartImage);
imgHeart.setImageResource(R.drawable.material_heart_fill_icon);
imgHeart.setLayoutParams(new FrameLayout.LayoutParams(50, 50, Gravity.CENTER));
mainFrameLaout.addView(imgHeart);
Ajouter une animation sur la vue d'image
imgHeart.animate()
.scaleXBy(6)
.scaleYBy(6)
.setDuration(700)
.alpha(2)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
imgHeart.animate()
.scaleXBy(-6f).scaleYBy(-6f)
.alpha(.1f)
.translationX((heigthAndWidth[0] / 2) - minusWidth)
.translationY(-((heigthAndWidth[1] / 2) - minusHeight))
.setDuration(1000)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// remove image view from framlayout
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}).start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}).start();