繁体   English   中英

如何在 ImageView 上的另一个图像上覆盖一个小图标

[英]How to overlay a small icon over another image on ImageView

我有一个 gridView 布局来显示图像,我想在标记的图像上放置一个小图标。

这是我的代码:

ImageView i;
    if (convertView == null) {  
        i = new ImageView(mContext);
        i.setLayoutParams(new GridView.LayoutParams(width, height));
        i.setPadding(10, 10, 10, 10);
        i.setScaleType(ImageView.ScaleType.CENTER_CROP);

    } else {
        i = (ImageView) convertView;
    }

    if(mImageSet == IMAGE_SET_ONE) { 
            boolean cek = true;
            i.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(),mThumbIds[position],width,width)); 

    }

请帮助..

ImageView 小部件具有用于设置背景和前景图像的内置功能。 您可以查看它在 xml 中的样子,可以使用 ImageView 上的 setImageResource 和 setImageBackground 方法设置 src 和 background 字段。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="@color/pink"
        android:src="@drawable/facebook_icon" />

</RelativeLayout>

第一个例子

唯一的问题是 src(覆盖)图像将匹配图像视图的高度或宽度。

因此,为了解决这个问题,你需要在一个相对中使用多个图像视图,并在每个视图上单独设置背景或 src。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerInParent="true"
        android:background="@color/pink" />

        <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true"
        android:src="@drawable/facebook_icon" />

</RelativeLayout>

第二个例子

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:translationZ="10dp">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:srcCompat="@raw/image" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right|bottom"
        android:layout_marginRight="10dp"
        android:background="#f00"
        android:padding="5dp"
        app:srcCompat="@drawable/ic_baseline_photo_camera_24" />
</FrameLayout>

用 srcCompat 替换您的图像

暂无
暂无

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

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