【问题标题】:Android Java determine pixel density in codeAndroid Java 确定代码中的像素密度
【发布时间】:2013-09-09 10:49:27
【问题描述】:

我有以下动画:

    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();

问题是路径设置为固定像素。我想要做的是独立设置路径像素。有什么想法可以实现吗?

我尝试将变量链接到dimensions.xml,但它不起作用,我宁愿将像素路径与像素密度比相乘,但我不知道是否有可以返回密度比值的函数用于设备。

任何帮助表示赞赏。

【问题讨论】:

  • [Context.getResources.getDimensionsPixelSize()](http://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelSize(int)) 从未失败过。也许您可以详细说明为什么这对您不起作用。但这是要遵循的方法,因为它更易于配置。

标签: android animation pixel


【解决方案1】:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

现在使用dm.xdpi()dm.ydpi() 来获取X 和Y 维度上的像素密度。

【讨论】:

    【解决方案2】:

    即使我在尝试实现这一点时也遇到了很大的麻烦。您可以使其与像素无关,以“dip”或“dp”单位指定。

    这个链接应该是一个好的开始:

    http://developer.android.com/guide/practices/screens_support.html

    也可以参考这些帖子:

    What is the correct way to specify dimensions in DIP from Java code?

    Android: how to use dip (density independent pixel) in code?

    【讨论】:

      【解决方案3】:

      Here's 一个可能会有帮助的答案。 从那里的答案看来,这是您正在寻找的行

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

      【讨论】:

      • 谢谢,这就是我一直在寻找的
      【解决方案4】:

      您可以使用此处的 DisplayMetrics 类:DisplayMetrics

      并使用密度场

      显示的逻辑密度。这是密度无关像素单元的缩放因子,其中一个 DIP 是大约 160 dpi 屏幕(例如 240x320、1.5"x2" 屏幕)上的一个像素,提供系统显示的基线。因此,在 160dpi 屏幕上,此密度值为 1;在 120 dpi 屏幕上为 0.75;等等

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 2022-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2012-04-08
        相关资源
        最近更新 更多