简体   繁体   English

如何使用 Android 在滚动视图中放大/更改子 imageview 尺寸以使图像全尺寸

[英]How to Enlarge/change the child imageview dimensions to make the image full size in Scroll view with Android

I have an ImageView inside a Scrollview, below is my fragment code, I need the image to be made full screen or change the width and height of the image view when the user clicks on the image, please help me how this can be achieved or any other better solution.我在 Scrollview 中有一个 ImageView,下面是我的片段代码,我需要将图像设为全屏或在用户单击图像时更改图像视图的宽度和高度,请帮助我如何实现或任何其他更好的解决方案。

ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="@color/white"
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/htab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <ImageView
            android:id="@+id/product_image"
            android:layout_width="match_parent"
            android:layout_height="192dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="3dp"
            android:adjustViewBounds="true"
            android:gravity="center_horizontal"
            android:scaleType="center" />

        <TextView
            android:id="@+id/product_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="5dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="center_horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textColor="@color/holo_gray_dark"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/category_discount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="5dp"
            android:background="@color/white"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="center_horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textColor="@color/holo_gray_dark"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/product_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/product_name"
            android:layout_gravity="left"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:textColor="@android:color/background_dark"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/remove_item"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_gravity="center_vertical|center_horizontal"
                android:background="@color/red"
                android:gravity="center_vertical|center_horizontal"
                android:text="-"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/iteam_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:freezesText="true"
                android:gravity="center"
                android:marqueeRepeatLimit="marquee_forever"
                android:minWidth="70dp"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="0"
                android:textColor="@android:color/background_dark"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/add_item"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_gravity="center_vertical|center_horizontal"
                android:background="@color/holo_green_light"
                android:gravity="center_vertical|center_horizontal"
                android:text="+"
                android:textColor="@color/white"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>

        <TextView
            android:id="@+id/hot_offers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="15dp"
            android:ellipsize="marquee"
            android:gravity="center_horizontal"
            android:text="@string/similar_product"
            android:textColor="#000"
            android:textSize="20sp" />

        <com.hitesh_sahu.retailapp.view.customview.ClickableViewPager
            android:id="@+id/similar_products_pager"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:layout_below="@+id/hot_offers"
            android:layout_marginTop="2dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/best_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="2dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="center_horizontal"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="Top Selling"
            android:textColor="#000"
            android:textSize="20sp" />

        <com.hitesh_sahu.retailapp.view.customview.ClickableViewPager
            android:id="@+id/top_selleing_pager"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:layout_below="@+id/best_value"
            android:layout_marginTop="2dp"
            android:visibility="visible" />
    </LinearLayout>

</ScrollView>

This is my layout,这是我的布局,

and here is my onclick listener in my class.这是我的 class 中的 onclick 监听器。 please help with the same.请帮忙。 i am finding the image size is varying when clicked but the image view dimensions are not changing, how to achieve this/ how to make the image fullscreen我发现单击时图像大小会发生变化,但图像视图尺寸没有改变,如何实现这一点/如何使图像全屏显示

itemImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(isImageFitToScreen) {
        isImageFitToScreen=false;
        ScrollView.LayoutParams layoutParams = new 
        ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 192);
                    itemImage.setLayoutParams(layoutParams);
                    itemImage.setAdjustViewBounds(true);
        }else{
            isImageFitToScreen=true;
            ScrollView.LayoutParams layoutParams = new 
            ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
            itemImage.setLayoutParams(layoutParams);
            }
    }
 });

Take a look at android:adjustViewBounds .看看android:adjustViewBounds Even though you are changing the size of the ImageView , this attribute is changing it back.即使您正在更改ImageView的大小,此属性也会将其改回。 I think that you can just delete it.我认为你可以删除它。

Also, the size dimension in layoutparams is pixels and not dp .此外, layoutparams 中的尺寸维度是像素而不是dp You are treating it like is is dp .您将其视为 dp

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

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