繁体   English   中英

如何居中裁剪线性布局的背景图像?

[英]how to center crop a background image of linear layout?

嗨,我不得不使用带有 recyclerview 的卡片进行布局,因此我将图像设置为线性布局的背景,但现在我无法将该图像居中裁剪我的问题是我无法使用 imageview,因为使用卡片的高度也增加了,我不想要,所以请如果有人可以帮助我..我的 xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/primary_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="19dp"
    android:layout_marginRight="19dp"
    android:layout_marginTop="15dp"
    app:cardCornerRadius="115dp">

    <ImageView
        android:id="@+id/background_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@drawable/club1"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/ellipse" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/first_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="John Doe"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/second_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="checked in to"
                        android:textColor="@color/White"
                        android:textSize="10sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/third_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="W south"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:orientation="horizontal">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fourth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="9dp"
                        android:text="beach mumbai"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fifth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/second_text"
                        android:layout_toRightOf="@+id/fourth_text"
                        android:text="30 mins ago."
                        android:textColor="@color/White"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="85dp">

            <com.ct.listrtrial.Custom.CustomTextViewMedium
                android:id="@+id/sixth_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:text="reply to abc............"
                android:textColor="@color/White" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/favourite_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_favorite_border_black_24dp" />

                <com.ct.listrtrial.Custom.CustomTextViewMedium
                    android:id="@+id/seventh_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:text="40 likes"
                    android:textColor="@color/White" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

您不能将线性布局的背景图像居中裁剪,但您可以使用以下更改实现相同的效果:

  1. 用 RelativeLayout 替换你的 LinearLayout
  2. 将 ImageView 作为第一个子元素放置在相对布局中,并使用属性进行中心裁剪。

     <RelativeLayout android:id="@+id/background_image" android:layout_width="match_parent" android:layout_height="210dp" android:orientation="vertical"> <ImageView android:scaleType="centerCrop" android:src="@drawable/club1" android:layout_width="match_parent" android:layout_height="match_parent" /> ..... ...... </RelativeLayout>

如果你想控制图像视图比例

你需要把它放在 ImageView 作为 Src Not Background

您可以使用框架布局来实现它

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/primary_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="19dp"
    android:layout_marginRight="19dp"
    android:layout_marginTop="15dp"
    app:cardCornerRadius="115dp">

    <ImageView
        android:id="@+id/background_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:visibility="gone" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="210dp">
        <ImageView
            android:layout_width="match_parent"
            android:src="@mipmap/ic_launcher"
            android:layout_height="match_parent" />
        <LinearLayout
            android:id="@+id/background_image"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/profile_image"
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:src="@drawable/playstore_icon" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="5dp">

                        <TextView
                            android:id="@+id/first_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="18dp"
                            android:padding="5dp"
                            android:text="John Doe"
                            android:textSize="15sp" />

                        <TextView
                            android:id="@+id/second_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="18dp"
                            android:padding="5dp"
                            android:text="checked in to"
                            android:textSize="10sp" />

                        <TextView
                            android:id="@+id/third_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="18dp"
                            android:padding="5dp"
                            android:text="W south"
                            android:textSize="15sp" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/fourth_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="9dp"
                            android:text="beach mumbai"
                            android:textSize="15sp" />

                        <TextView
                            android:id="@+id/fifth_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/second_text"
                            android:layout_toRightOf="@+id/fourth_text"
                            android:text="30 mins ago."
                            android:textSize="10sp" />
                    </LinearLayout>
                </LinearLayout>


            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="85dp">

                <TextView
                    android:id="@+id/sixth_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:padding="10dp"
                    android:text="reply to abc............"
                    android:textColor="@color/white" />


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true">

                    <ImageView
                        android:id="@+id/favourite_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/ic_close" />

                    <TextView
                        android:id="@+id/seventh_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="15dp"
                        android:text="40 likes"
                        android:textColor="@color/white" />
                </LinearLayout>
            </RelativeLayout>

        </LinearLayout>

    </FrameLayout>
</android.support.v7.widget.CardView>

CenterCrop 逻辑在ImageView内部烘焙。 正确的解决方案是将其提取到一个 Drawable 中该 Drawable包装另一个 Drawable 并居中裁剪它。

如果您使用 CoordinatorLayout,那么只需将带有 centerCrop 参数的 ImageView 放在顶部。

<CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:scaleType="centerCrop"
    android:background="@drawable/background_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:elevation="0dp">

    <include layout="@layout/header_main" />

    <TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@android:color/white"
        android:background="@drawable/white_border_background"
        app:tabIndicatorHeight="3dp"/>

</AppBarLayout>

<ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</CoordinatorLayout>

嗨,我不得不使用recyclerview制作卡片的布局,因此我将图像设置为线性布局的背景,但是现在我无法对图像进行居中裁剪。我的问题是我无法使用imageview,因为使用了卡片的高度以及增加,我不希望这样,如果有人可以帮我..我的XML

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/primary_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="19dp"
    android:layout_marginRight="19dp"
    android:layout_marginTop="15dp"
    app:cardCornerRadius="115dp">

    <ImageView
        android:id="@+id/background_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@drawable/club1"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/ellipse" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/first_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="John Doe"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/second_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="checked in to"
                        android:textColor="@color/White"
                        android:textSize="10sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/third_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="W south"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:orientation="horizontal">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fourth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="9dp"
                        android:text="beach mumbai"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fifth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/second_text"
                        android:layout_toRightOf="@+id/fourth_text"
                        android:text="30 mins ago."
                        android:textColor="@color/White"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="85dp">

            <com.ct.listrtrial.Custom.CustomTextViewMedium
                android:id="@+id/sixth_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:text="reply to abc............"
                android:textColor="@color/White" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/favourite_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_favorite_border_black_24dp" />

                <com.ct.listrtrial.Custom.CustomTextViewMedium
                    android:id="@+id/seventh_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:text="40 likes"
                    android:textColor="@color/White" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

暂无
暂无

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

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