简体   繁体   English

获取父布局的宽度

[英]Get the width of the parent layout

Android Studio 2.0 beta 6

I am trying to use ViewPropertyAnimator to move a ImageView (ivSettings) inside a toolbar so that it is 20dp from the right and 20dp from the top, from is current location. 我正在尝试使用ViewPropertyAnimator在工具栏中移动ImageView (ivSettings) ,使其从右侧20dp,从顶部20dp,从当前位置开始。 And move the ImageView (ivSearch) 20dp from the left and top. 并从左侧和顶部移动ImageView (ivSearch) 20dp。

The imageViews are contained in a Toolbar . imageView包含在Toolbar

This is the initial state and I want to move the icons into the upper corners inside the toolbar. 这是初始状态,我想将图标移动到工具栏内的上角。

在此输入图像描述

The code I am using is this to get the width and then subtract a value to get the ivSettings to be 20dp from the right. 我正在使用的代码是获取宽度然后减去一个值以使ivSettings从右边获得20dp。

final DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final float widthPx = displayMetrics.widthPixels;

ivSearch.animate()
    .setInterpolator(new AccelerateInterpolator())
    .x(20)
    .y(20)
    .setDuration(250)
    .start();

ivSettings.animate()
    .setInterpolator(new AccelerateInterpolator())
    .x(widthPx - 160)
    .y(20)
    .setDuration(250)
    .start();

However, having tried this on different screen size I can't get the exact width calculation. 但是,在不同的屏幕尺寸上试过这个,我无法得到精确的宽度计算。 Is there a better way of doing this? 有没有更好的方法呢?

Many thanks for any suggestions 非常感谢任何建议

You should be able to use the translationX and translationY properties to achieve the effect you want. 您应该能够使用translationXtranslationY属性来实现您想要的效果。 These properties act as offsets from the original X and Y coordinates of the View. 这些属性充当视图的原始X和Y坐标的偏移。

Essentially translationX will displace the a View towards the right for a positive value and left for a negative value from it's X coordinate. 基本上,translationX会将一个View向右移动一个正值,并从它的X坐标向左移一个负值。 Similarly translationY displaces the View towards the bottom for positive and top for negative values from it's Y coordinate. 类似地,translationY将视图向底部移位为正向,顶部为负值从Y坐标移位。

Coming to your question, I am assuming that the final position you want to reach is the top left corner for the search icon, and the top right corner for the settings icon with zero padding for each view. 回到你的问题,我假设你想要到达的最终位置是搜索图标的左上角,而设置图标的右上角是每个视图的零填充。

For the search icon I suggest you start by placing it in the top left corner of the toolbar. 对于搜索图标,我建议您先将其放在工具栏的左上角。 Then set both the translationX and translationY to 20p. 然后将translationX和translationY都设置为20p。 That should place the search icon in the same place as your image. 这应该将搜索图标放在与图像相同的位置。 To move your search icon to the top left all you have to do is animate translationX & Y from 20dp to 0 dp. 要将搜索图标移到左上角,您只需将翻译X和Y从20dp动画为0 dp即可。

Repeat the same for the settings icon, but set translationX to -20dp and translationY to 20dp. 对设置图标重复相同操作,但将translationX设置为-20dp,将translationY设置为20dp。 This way it'll be placed at the same position as your image. 这样它就会被放置在与图像相同的位置。 Animate both values to 0 to achieve your desired animation. 将两个值设置为0都可以实现所需的动画效果。

Here's the animation code for reference. 这是动画代码供参考。

ivSearch.animate()
    .setInterpolator(new AccelerateInterpolator())
    .translationX(0) // Takes value in pixels. From 20dp to 0dp
    .translationY(0) // Takes value in pixels. From 20dp to 0dp
    .setDuration(250)
    .start();

ivSettings.animate()
    .setInterpolator(new AccelerateInterpolator())
    .translationX(0) // Takes value in pixels. From -20dp to 0dp
    .translationY(0) // Takes value in pixels. From 20dp to 0dp
    .setDuration(250)
    .start();

// To get pixels from dp
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());

The great thing about this approach is that you no longer need to know the dimensions of the parent. 这种方法的好处在于您不再需要知道父级的维度。 All you care about is the offsets that you have specified via translationX and translationY . 您关心的只是您通过translationXtranslationY指定的偏移量。

I believe the problem is that the Animator methods are expecting the values to be in raw pixel values. 我相信问题是Animator方法期望值是原始像素值。 It looks like the code would only work as you intend on a medium density screen. 看起来代码只能在中密度屏幕上运行。 Eg A hdpi screen would require 30px instead of 20px to be located at the same location. 例如,hdpi屏幕需要30px而不是20px才能位于同一位置。 On higher density screens the code as it exists would push the icons closer to the edges... 在更高密度的屏幕上,代码存在会使图标更靠近边缘......

That means we need to translate the expected dp value into raw pixel values and use those for animation. 这意味着我们需要将预期的dp值转换为原始像素值并将其用于动画。 There is a handy method for converting dp to px so that it's correct on every device. 有一种方便的方法可以将dp转换为px,以便在每个设备上都正确。 I'm assuming that you want the left side of the gear to be 160dp from the right(Note I'm specifically saying the left side of the gear because the 160dp value should also include the width of the gear image as the x property is from the left of the image). 我假设您希望齿轮的左侧从右侧160dp(注意我特别说齿轮的左侧,因为160dp值还应该包括齿轮图像的宽度,因为x属性是从图像的左侧)。 So 160dp = gear image width + desired right margin distance. 所以160dp =齿轮图像宽度+所需的右边距。 Say you have a resource called ic_gear: Drawable drawable = getResources().getDrawable(R.drawable.ic_gear); 假设您有一个名为ic_gear的资源:Drawable drawable = getResources()。getDrawable(R.drawable.ic_gear); float bitmapWidth = getIntrinsicWidth(); float bitmapWidth = getIntrinsicWidth();

float rawPX20DPValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());

float rawPXLeftOffset = rawPX20DPValue + bitmapWidth;

ivSearch.animate()
    .setInterpolator(new AccelerateInterpolator())
    .x(rawPX20DPValue)
    .y(rawPX20DPValue)
    .setDuration(250)
    .start();


ivSettings.animate()
    .setInterpolator(new AccelerateInterpolator())
    .x(widthPx - rawPXLeftOffset)
    .y(rawPX20DPValue)
    .setDuration(250)
    .start();

Also check on the offsets(padding) of the gear and search icon before the move as that will likely affect the end result as well. 在移动之前还要检查齿轮的偏移(填充)和搜索图标,因为这可能也会影响最终结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM