【问题标题】:Anchor ImageView position to another ImageView which is being scaled将 ImageView 位置锚定到正在缩放的​​另一个 ImageView
【发布时间】:2013-09-04 17:33:18
【问题描述】:

我得到了这个布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rel2"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff" >

  <ImageView
    android:id="@+id/tree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/lemon_large"
    android:scaleType="centerCrop"
    />
  <ImageView 
      android:id="@+id/fruit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/lemonpiece"
      android:layout_alignLeft="@+id/tree"
      android:layout_alignTop="@+id/tree"
      android:layout_marginLeft="95dp"
      android:layout_marginTop="136dp"/>

树正在被缩放,顶部和底部总是被裁剪。这是因为屏幕高度的可变性。我想做的是拥有固定的图像视图,无论它被缩放多少,它总是相对于图像视图在同一个位置。

我尝试了不同的 scaleType 值,但它实际上总是缩放图像,并且在每个屏幕上,图像都位于相对于“树”的其他位置。

我尝试的另一种可能性是将它与屏幕顶部对齐(alignParentTop、alignParentLeft、scaleType="matrix")但这不起作用,因为某些屏幕非常短,然后图片无法正确放入屏幕.换句话说,我需要保持可扩展性并锚定顶部图像。

有什么想法吗?

【问题讨论】:

    标签: android xml scale


    【解决方案1】:

    为Dimensions创建一个Resource XML文件,并放入以下内容,

    image_dimens.xml:

    <dimen name="myValueX">95dp</dimen>
    <dimen name="myValueY">136dp</dimen>
    

    在显示 ImageViews 的 Activity 中:

        int myValueX = (int) getResources().getDimension(R.dimen.myValueX);
        int myValueX = (int) getResources().getDimension(R.dimen.myValueX);
    
        ImageView ivFruit = (ImageView) findViewById(R.id.fruit);
        ImageView ivTree = (ImageView) findViewById(R.id.tree);
        ivFruit.setX(ivTree.getX() + myValueX);
        ivFruit.setY(ivTree.getY() + myValueY);
    

    希望这会有所帮助,祝编程愉快!

    【讨论】:

    • 您好,这行不通。我首先需要知道“树”缩放了多少,然后实际上将缩放比例与固定边距相乘......有什么想法吗?
    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多