【发布时间】:2017-08-22 08:09:24
【问题描述】:
我想以编程方式获取 ImageView 的位置。该位置是相对于屏幕而不是父级的 imageView 的像素。实际上,我在搜索它们时找到了一些解决方案,虽然布局方向是从左到右,但是当我将方向更改为从右到左时,它给了我奇怪的值(这是 isseu)。 当活动支持 rtl 时,我如何获得位置。
我找到了一些解决方案:
1) private int getRelativeTop(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());}
2) image.getLocationOnScreen(int[] locaiton);
更新
在我的活动中,我有三个 imageviews ,我将(翻译动画) image3 从 image1 移动到 image2 。开始从位置 image1 移动到位置 image2,当我使用 ltr 时,它的动画是正确的,但是当我更改 supportrtl="true" 时,我根本看不到动画。
这是xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/rootParent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="@+id/piece_FLOAT"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/piece1"
android:translationZ="@dimen/activity_horizontal_margin" />
<ImageView
android:id="@+id/piece_1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:src="@drawable/piece1" />
<ImageView
android:id="@+id/piece_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:src="@drawable/piece1" />
</LinearLayout>
这是java类
public class AnimateActivity extends AppCompatActivity {
ImageView imageViewfrom, imageViewto;
ImageView imageViewFLOAT;
LinearLayout L_33;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_animate);
imageViewfrom = (ImageView)findViewById(R.id.piece_1);
imageViewto = (ImageView)findViewById(R.id.piece_2);
imageViewFLOAT = (ImageView)findViewById(R.id.piece_FLOAT);
assert imageViewfrom != null;
assert imageViewto != null;
imageViewfrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int[] ToLocation = new int[2];
imageViewto.getLocationOnScreen(ToLocation);
float xTO = ToLocation[0];//imageViewto.getX(); //ToLocation[0];
float yTO = ToLocation[1];//imageViewto.getY();//ToLocation[1];
int[] FromLocation = new int[2];
imageViewfrom.getLocationOnScreen(FromLocation);
float xFROM = FromLocation[0];//imageViewfrom.getX();///FromLocation[0];
float yFROM = FromLocation[1]; //imageViewfrom.getY();//FromLocation[1];
Log.e("xFrom =" + xFROM, "xTo =" + xTO );
Log.e("yFrom =" + yFROM, "yTo =" + yTO );
// Log.e("offset =" + topOffset, "xTo =" + 0);
ValueAnimator animatorX = ObjectAnimator.ofFloat(imageViewFLOAT, "x", xFROM, xTO).setDuration(1500);
ValueAnimator animatorY = ObjectAnimator.ofFloat(imageViewFLOAT, "y", yFROM, yTO).setDuration(1500);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animatorX, animatorY);
animatorSet.start();
// Animation an = new TranslateAnimation(xFROM, xTO, yFROM , yTO);
// an.setDuration(1500);
// an.setFillAfter(true);// to keep the state after animation is finished
// imageViewFLOAT.startAnimation(an);// to start animation obviously
}
});
}
}
当我使用 ltr 时,它会非常有效,但会添加一些像素。当我使用 rtl 时没有看到动画。为什么会这样?
感谢您的帮助。
【问题讨论】:
-
是的,
getLocationOnScreen就是你想要的 -
我使用这种方法,但它给了我屏幕边框之外的价值。不知道为什么?
-
那么你得到了什么?
-
当 ltr imageView_top = 42, imageView_left = 168, 但是当 rtl imageView_top = 426, imageView_left = 168
-
在我的活动中,我有三个 imageviews ,我将(翻译动画) image3 从 image1 移动到 image2 。开始从位置 image1 移动到位置 image2,当我使用 ltr 时,它的动画是正确的,但是当我更改 supportrtl="true" 时,我根本看不到动画。
标签: android layout imageview right-to-left