android为白色时,为什么图像显示为浅灰色背景?

时间:2018-10-26 05:25:55

标签: android image background imageview android-glide

enter image description here

当我使用ImageViewGlide中显示图像时,在背景中添加了灰色。我尝试在xml文件的ImageView中将背景颜色设置为透明甚至白色。 但是它仍然无法正常工作。我在各种活动中显示的每幅图像都在发生这种情况。 以下是在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

2 个答案:

答案 0 :(得分:3)

解决方案:

无论您放置了ImageView之类的父布局,例如<LinearLayout>还是<RelativeLayout><ConstraintLayout>,它们的默认颜色代码均为#FAFAFA,即并非完全是 White ,这可能是原因之一

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

所以答案就在您的布局中,将ImageView所在的父布局的背景色设置为 White #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>

更新:按照Here

在您的Glide中添加以下行:
.dontTransform()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)

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

答案 1 :(得分:0)

最终找到了解决方案。这是Glide的问题 https://github.com/bumptech/glide/issues/853 使用DiskCacheStrategy.SOURCE和DecodeFormat.PREFER_ARGB_8888已解决了此问题。