【问题标题】:android merging 2 imagesandroid合并2张图片
【发布时间】:2012-05-23 15:53:41
【问题描述】:

我已经为此工作了好几个小时,但无法解决。 我想在一个 XML 文件中合并 2 个图像 - 所以这将是我的形状的基本 xml 文件。 我希望这个形状包括一个作为框架的 png 和一个作为框架内的图片的 png。那张图片会动态变化。

我尝试使用标签来构建我的形状,但是当我将它包含到我的主 xml 文件中时它失败了,因为我只能以根用户身份使用合并。 尝试做同样的事情,但仍然没有得到我想要的......

我该怎么做才能合并这两个 png,并能够在框架 png 内设置内部 png 位置??

编辑: 一些代码:

<ImageView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent"             
    android:src="@drawable/userprofilepicture"
    android:scaleType="fitXY" 
    android:contentDescription="@string/SmallLogo" />


<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/SmallLogo"
    android:scaleType="fitXY"
    android:padding="30dp"
    android:src="@drawable/questionmark" />

</FrameLayout>

这是我的框架和内部图片 xml 源.. 在图形布局中查看它时,它看起来不错。 当将它包含在我的主 xml 文件中时,由于某种原因它会翻转图像...... ???

这是我的主要 xml 的一部分:

        <TableRow>

        <ImageView
            android:layout_gravity="center_vertical"
            android:layout_marginTop="2.5dp"
            android:contentDescription="@string/SmallLogo"
            android:src="@drawable/logosmall" />

        <ImageView
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="2.5dp"
            android:contentDescription="@string/slogen"
            android:src="@drawable/slogen" />

        <include layout="@drawable/userprofilepicture"
            android:layout_width="30dp"
            android:layout_height="30dp"/>

    </TableRow>

【问题讨论】:

    标签: android xml merge


    【解决方案1】:

    创建一个 FrameLayout 自定义类。此类将具有框架布局的所有 XML 属性。

    com.package.example;
    
    public class PictureFrame extends FrameLayout {
    
        public PictureFrame(Context c, AttributeSet attrs) {
            super(c, attrs);
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //This adds the image views as you've styled them in the XML file
            inflater.inflate(R.layout.myFrameImages, this);
        }
    
        public void setInnerImage(int resource) {
            ImageView ivInner = (ImageView) findViewById(R.id.innerImage);
            ivInner.setImageResource(resource);
        }
    
    }
    

    您的 XML 文件看起来像

    <ImageView
         android:src="@drawable/myFrameImage"
         android:layout_height="fill_parent"
         android:layout_width="fill_parent"
         android:scaleType="fitXY"/>
    <ImageView
         android:src="@drawable/myInnerImage"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:scaleType="fitXY"
         android:layout_margin="10dp"
         android:id="@+id/innerImage"/>
    

    您也可以创建自己的 XML 属性,用于在 XML 中声明时,因此可以直接在 xml 中声明内部图像。如果您需要这方面的帮助,您必须与我联系或发布其他问题。

    【讨论】:

    • 我已将我的代码添加到我的问题中。如果可能,请查看
    • 好的,当我不包括在内时,只需将框架布局复制粘贴到我的 main.xml 中就可以了,谢谢!
    【解决方案2】:

    您是否考虑过使用9-patch 和自定义布局?这是example。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2016-10-24
      • 2018-08-22
      相关资源
      最近更新 更多