Pregunta

I created the following accessor in order to add a simple tween to an imageview's position. I'm using a RelativeLayout.

public class ImageViewAccessor implements TweenAccessor<ImageView> {

     public static final int X = 1;
     public static final int Y = 2;
     public static final int XY = 3;

    @SuppressLint("NewApi")
    public int getValues(ImageView target, int tweenType, float[] returnValues) {
         switch (tweenType) {
             case X: returnValues[0] = target.getX(); return 1;
             case Y: returnValues[0] = target.getY(); return 1;
             case XY:
                 returnValues[0] = target.getX();
                 returnValues[1] = target.getY();
                 return 2;
             default: assert false; return 0;
         }
     }

    @SuppressLint("NewApi")
     public void setValues(ImageView target, int tweenType, float[] newValues) {    
         switch (tweenType) {
             case X: target.setX(newValues[0]); break;
             case Y: target.setY(newValues[1]); break;
             case XY:
                 target.setX(newValues[0]);
                 target.setY(newValues[1]);
                 break;
             default: assert false; break;
         }
     }
}

I'm registering the accessor using the following code:

Tween.registerAccessor(ImageViewAccessor.class, new ImageViewAccessor());
Tween.to(logo, ImageViewAccessor.Y, 1f).target(50).start();

But I'm getting a crash "No TweenAccessor was found for the target" on the Tween.to(logo, ImageViewAccessor.Y, 1f).target(50).start(); so I have 2 questions.

  • Can I use this framework to add simple tweens to an imageview (I want the image to hover about 5 px up then 5 px down)?
  • Also, Why is this error showing, if I have registered an accessor just before calling the function?

EDIT: Also, ImageView.getX() and ImageView.getY() are present in api level 11. But i dont know if should use them or i should use layaoutparamenters.topMargin

¿Fue útil?

Solución

I know this question is pretty old, but I'll answer anyway for people searching this later.

The error is in

Tween.registerAccessor(ImageViewAccessor.class, new ImageViewAccessor());

The first parameter is the class that you want to access, and the second one is the Accessor that should be used to access that class.

Sou you wrote that you want to access the ImageViewAccessor, but actually you want to access the ImageView class using the ImageViewAccessor.

It should be like this:

Tween.registerAccessor(ImageView.class, new ImageViewAccessor());
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top