android - 当它有白色的android时,为什么图像显示为浅灰色背景?

标签 android image background imageview android-glide

enter image description here

当我使用GlideImageView中显示图像时,背景中添加了灰色。我尝试在我的xml中的ImageView中将背景颜色设置为透明甚至白色文件.. 但它仍然不起作用......我在各种 Activity 中显示的每张图像都会发生这种情况。 下面是在 iPhone 和 Android Oreo 中显示的同一图像的屏幕截图。 下面是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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">

    <RelativeLayout
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:background="#fff">

        <LinearLayout
            android:id="@+id/bklayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="28dp"
            android:paddingTop="8dp">

            <ImageView
             android:id="@+id/bkbutton"
             android:layout_width="35dp"
             android:layout_height="50dp"
             android:layout_marginLeft="8dp"
             android:layout_marginTop="8dp"
             app:srcCompat="@drawable/ic_keyboard_arrow_left_black_24dp"/>
         </LinearLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="35dp" />

        <LinearLayout
            android:id="@+id/indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/viewPager"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal">
         </LinearLayout>
    </RelativeLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/second"
        android:id="@+id/third"
        android:orientation="vertical">


        <View
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:layout_marginTop="2dp"
            android:background="@color/new_grey">
         </View>


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

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="0.9"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:orientation="vertical">

            <TextView
                android:id="@+id/product_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Product Name: "
                android:textColor="@android:color/black"
                android:textSize="18sp" />
                <com.example.unsan.grouponebuy.helpers.ExpandableTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="3"
                    android:id="@+id/desc"/>
             </LinearLayout>



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

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/fav"
                    android:padding="5dp"
                    app:srcCompat="@drawable/ic_favorite_grey_24dp" />

                <TextView
                    android:id="@+id/favtext"
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="@string/favorite"
                    android:textColor="@color/grey_dark" />

          </LinearLayout>

        </LinearLayout>

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

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:textColor="@color/orange" />

        <TextView
            android:id="@+id/origprice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/specification" />

            <TextView
                android:id="@+id/specification"
                android:layout_marginLeft="4dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="8dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/origin" />

            <TextView
                android:id="@+id/origin"
                android:layout_marginLeft="4dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/replacement_layout"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check_box_green_24dp"/>
            <TextView
                android:layout_marginLeft="5dp"
                android:text="@string/replacement_policy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:orientation="vertical"
        android:id="@+id/prodCartLayout">
    <View

        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="@color/grey"></View>




    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:orientation="horizontal">
        <ImageView

            android:layout_width="45dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:id="@+id/carticonbutton"
            android:src="@drawable/cart_bl" />


        <View
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"></View>

        <Button
            android:id="@+id/carttext"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingRight="5dp"
            android:paddingLeft="3dp"
            android:gravity="center_vertical"
            android:background="@color/red"
            android:textSize="16sp"
            android:text="@string/add_to_cart"
            android:textColor="@android:color/white" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:text="@string/outOfStock"
            android:layout_marginRight="8dp"
            android:layout_marginTop="12dp"
            android:textColor="@color/grey"
            android:id="@+id/soldout_text"
            android:visibility="gone"
            />
    </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/prodSelectedLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:visibility="gone">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="2dp"
            android:background="@color/new_grey"></View>

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

            <ImageView
                android:layout_width="45dp"
                android:layout_height="50dp"
                android:layout_marginLeft="5dp"
                android:id="@+id/carticonbutton2"
                android:src="@drawable/cart_bl" />

            <View
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"></View>

            <ImageView
                android:id="@+id/sub_quantity"
                android:layout_width="30dp"
                android:layout_height="50dp"
                android:layout_marginTop="4dp"
                android:src="@drawable/minus" />

            <TextView
                android:id="@+id/quantity"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="12dp"
                android:text="1"
                android:textSize="18sp" />

            <ImageView
                android:id="@+id/add_quantity"
                android:layout_width="30dp"
                android:layout_height="50dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="4dp"
                android:src="@drawable/add_on" />

        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

我的Java代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_detail);
        backButton = (ImageView) findViewById(R.id.bkbutton);
        cartImageButton = (ImageView) findViewById(R.id.carticonbutton);
        cartButton2 = (ImageView) findViewById(R.id.carticonbutton2);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        productNameText = (TextView) findViewById(R.id.product_name);
        originText = (TextView) findViewById(R.id.origin);
        specText = (TextView) findViewById(R.id.specification);
        favText = (TextView) findViewById(R.id.favtext);
        favImgView = (ImageView) findViewById(R.id.fav);
        priceText = (TextView) findViewById(R.id.price);
        soldOutText = (TextView) findViewById(R.id.soldout_text);
        quantityText = (TextView) findViewById(R.id.quantity);
        addButton = (ImageView) findViewById(R.id.add_quantity);
        subButton = (ImageView) findViewById(R.id.sub_quantity);
        originalPriceText = (TextView) findViewById(R.id.origprice);
        descriptionText = (ExpandableTextView) findViewById(R.id.desc);
        cartLinearLayout = (LinearLayout) findViewById(R.id.prodCartLayout);
        productSelectedLayout = (LinearLayout) findViewById(R.id.prodSelectedLayout);
        cartAddButton = (Button) findViewById(R.id.carttext);
        replacementLayout = (LinearLayout) findViewById(R.id.replacement_layout);
        listViews = new ArrayList<>();
        indicator = (LinearLayout) findViewById(R.id.indicator);
        globalProvider = GlobalProvider.getGlobalProviderInstance(getApplicationContext());
        buyNumView = new CircleBadgeView(this, cartButton2);
        buyNumView.setTextColor(Color.WHITE);
        buyNumView.setBackgroundColor(Color.RED);
        numBadge = new CircleBadgeView(this, cartImageButton);
        numBadge.setBackgroundColor(Color.RED);

        //cycleTextViewExpansion(descriptionText);
        // expandCollapsedByMaxLines(descriptionText);

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent returnIntent = new Intent();
                returnIntent.putExtra("productupdated", product);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();

            }
        });
        Intent intent = getIntent();
        product = (Product) intent.getSerializableExtra("product");


        List<String> imgList = product.getImageDisplay();
        if (Constants.getLanguage(this).equals("english")) {

            String origin = product.getOriginEn();


            productNameText.setText(product.getNameEn());
            originText.setText(origin);
            ;
            specText.setText(product.getSpecificationEn());
            if (product.getDescriptionEn() != null) {
                descriptionText.setText(product.getDescriptionEn());
            }
        } else {
            productNameText.setText(product.getNameCh());
            originText.setText(product.getOriginCh());
            specText.setText(product.getSpecificationCh());
            if (product.getDescriptionCh() != null) {
                descriptionText.setText(product.getDescriptionCh());
            }
        }

        boolean prodSelected = false;


        priceText.setText("$ " + product.getPrice());
        listViews.clear();


        indicator.removeAllViews();

        if (imgList.size() > 0)

        {
            for (int a = 0; a < imgList.size(); a++) {
                ViewGroup pager = (ViewGroup) getLayoutInflater().inflate(R.layout.viewpager_image, null);

                ImageView imageView = (ImageView) pager.findViewById(R.id.viewpagerimg);
               /* if (a == 0) {

                        imageView.setTransitionName(imgTransitionName);

                }
                */
                Glide.with(ProductDetailActivity.this).load(Constants.baseUrlStr + imgList.get(a)).override(500, 500).centerCrop().into(imageView);
                listViews.add(pager);


            }
            productDetailViewPagerAdapter = new ProductDetailViewPagerAdapter(listViews, this);
            viewPager.setAdapter(productDetailViewPagerAdapter);


            if (imgList.size() > 1) {
                for (int a = 0; a < imgList.size(); a++) {
                    if (a == 0) {
                        ImageView imgView = new ImageView(this);
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(15, 15);
                        imgView.setLayoutParams(lp);
                        imgView.setImageResource(R.drawable.selected_dot);
                        indicator.addView(imgView);


                    } else {
                        ImageView imgView = new ImageView(this);
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(15, 15);
                        lp.setMargins(20, 0, 0, 0);
                        imgView.setLayoutParams(lp);
                        imgView.setImageResource(R.drawable.default_dot);
                        // imgs.add(img);
                        indicator.addView(imgView);


                    }
                }
            } else {

                ImageView img = new ImageView(this);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
                        (15, 15);
                //lp.setMargins(5,0,0,0);
                img.setLayoutParams(lp);
                img.setImageResource(R.drawable.selected_dot);
                //imgs.add(img);
                indicator.addView(img);

            }
        }


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()

        {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < indicator.getChildCount(); i++) {
                    if (i == position) {
                        ((ImageView) indicator.getChildAt(i)).setImageResource(R.drawable.selected_dot);

                    } else {
                        ((ImageView) indicator.getChildAt(i)).setImageResource(R.drawable.default_dot);
                    }
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

以下是我的应用程序的主题:

    <resources>

        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:buttonStyle">@style/Button</item>


        </style>
        <style name="Button" parent="Widget.AppCompat.Button">
            <item name="android:textAllCaps">false</item>
        </style>

</resources>

enter image description here

最佳答案

解决方案:

无论您将 ImageView 放置在哪个父布局中,如<LinearLayout><RelativeLayout><ConstraintLayout> ,它们都有一个默认颜色代码 #FAFAFA这不完全是白色,这是可能的原因之一

您可能已经知道白色的颜色代码是 #FFFFFF .

所以你的答案在于你的布局,设置你的父布局的背景颜色,其中你的ImageView居住为白色 #FFFFFF如下图:

    <LinearLayout
        android:id="@+id/bklayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="28dp"
        android:paddingTop="8dp"
        android:background="#FFFFFF">

        <ImageView
            android:id="@+id/bkbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"> 
   </LinearLayout>

更新:Glide 中添加以下行如上所述Here

.dontTransform()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)

尝试一下,我希望这就是问题所在。

关于android - 当它有白色的android时,为什么图像显示为浅灰色背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53001993/

相关文章:

java - 导出带有图像的 Jar

css - 一直在 CSS 背景图片上失败

java - com.android.dex.DexIndexOverflowException : Error:Execution failed for task "...\Java\jdk1.7.0_17\bin\java.exe" finished with non-zero exit value 2

android - 在Android中使用GoogleHeaders时要包括哪些内容?

html - 使一个图像落后于另一个

python - 从服务器流式传输大量屏幕截图

html - 使 div 视频背景淡出 - 这可能吗?

html - div 不显示背景

java - Android WebView 将它拉到多个 html 页面,这些页面根据用户滑动而改变

android - 使用 Glide 刷新 ImageView