M
M
MonteKarlo2016-05-07 10:38:51
Animation
MonteKarlo, 2016-05-07 10:38:51

Android Studio: how to animate a programmatically created ImageView?

I created an ImageView programmatically: gave it a picture and coordinates. But when I add an animation of rotation around its axis, for some reason the rotation occurs around the coordinates (0,0) of the screen.
If you create an ImageView in the designer and apply the same animation, then everything rotates correctly around its axis.
What needs to be added?
Programmatically created an ImageView:

ImageView im = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
im.setImageResource(R.drawable.image1);
lParams.height = h;
lParams.width = w;
im.setX((int) (dispW / 2 - w / 2));
im.setY((int) (h * 9.5));
this.addContentView(im, lParams);
@IdRes int x = 10000;
im.setId(x);

Animation code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fillAfter="true">
<rotate
    android:pivotX="50%"
    android:pivotY="50%"
    android:startOffset="0"
    android:toDegrees="90"
    android:fromDegrees="0"/>
</set>

Enable animation on button click:
Animation rotateR = AnimationUtils.loadAnimation(this,R.anim.rotate_r);
ImageView im = (ImageView)findViewById(10000);
im.startAnimation(rotateR);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MonteKarlo, 2016-05-09
@MonteKarlo

As a result, I decided on my own: I
created an animation programmatically and calculated the coordinates of the animation center in it

ImageView im = (ImageView)findViewById(10000);
Animation ar = new RotateAnimation(0,360,im.getX()+im.getWidth()/2,im.getY()+im.getHeight()/2);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question