Search code examples
androidanimationpixel

Android Java determine pixel density in code


I have a following animation:

    ImageView fallingLeave = (ImageView) rootView.findViewById(R.id.lemonpiece1_large);
                   mButtonProxy = AnimatorProxy.wrap(fallingLeave);

                   // Set up the path we're animating along
                   AnimatorPath path = new AnimatorPath();
                   path.moveTo(0,0);
                   path.curveTo(0, 0, 0 , 80, -80, 70);
                   fallingLeave.setRotation(80);
                   path.curveTo(-80, 70, -80, 120, 80, 140);
                   path.curveTo(80, 140, 80, 190, -80, 210);


                   // Set up the animation
                   final ObjectAnimator anim = ObjectAnimator.ofObject(this, "buttonLoc",
                           new PathEvaluator(), path.getPoints().toArray());
                   anim.setDuration(2000);


                   anim.start();

Problem is that path is set fixed in pixels. What I want to do is to set the path pixel independent. Any ideas how to accomplish that?

I tried to link the variable to dimensions.xml, but It didnt work, I would rather MULTIPLY the pixel path with a pixel density ratio, but I dont know whethere there is a function that can return a value of a density ratio for a device.

Any help appreciated.


Solution

  • Here's an answer that will probably be helpful. From the answers there it looks like this is the line you're looking for

    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    int densityDpi = dm.densityDpi;