简体   繁体   中英

How to add a transparent image on the top of the image view when it is selected?

I have a set of image views in a row,when the user clicks on the image, the background is getting changed.I want to add a transparent image on the top of my image view with a small tick in the middle to indicate that it is selected.

final ImageView iv_image=new ImageView(this);
    LayoutParams iv_image_params=new LayoutParams(
            Math.round(100*multiplier),
            Math.round(100*multiplier));
    iv_image_params.setMargins(5, 5, 10, 5);
    iv_image.setId(Integer.parseInt(id));

    try {
        Bitmap bmp = BitmapFactory.decodeFile(DashBoard.file_path+image);
        iv_image.setImageBitmap(bmp);
        iv_image.setBackground(getResources().getDrawable(R.drawable.border_red_image_based));
        iv_image.setSelected(false);

    } catch (Exception e) {
    }
    iv_image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (iv_image.isSelected()==false) {
                iv_image.setBackground(getResources().getDrawable(R.drawable.border_only_image_based));
                iv_image.setSelected(true);

            } else {
                iv_image.setBackground(getResources().getDrawable(R.drawable.border_red_image_based));
                iv_image.setSelected(false);

            }

        }
    });

    ll_view.addView(iv_image, iv_image_params);

*border_only_image_based: is my selector where I am just setting white border for an image view to indicate that it is selected.

normal image:

在此处输入图片说明

the image I want to be on the top of that image view:

在此处输入图片说明

Something like below should be helpful.

<RelativeLayout
    android:id="@+id/rl_profile_pic_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp">

    <ImageView
        android:id="@+id/img_profile_pic_non_editable"
        android:layout_width="110dp"
        android:layout_height="120dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:background="@drawable/your_actual image"
        android:scaleType="center" />

    <ImageView
        android:id="@+id/img_camera_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:src="@drawable/your_tick_icon"
        android:visibility="gone" />
</RelativeLayout>

Where the first image view is your actual image. And second image is the tick mark you are going to place above it. Add a clicklistener to make tick mark visible or invisible. This code is based on my requirement. Please modify other properties as required.

Try to use tick mark image as icon size like 36x36,48x48,72x72,96x96

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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