Horrible ImageView bitmap quality?

2019-04-08 05:30发布

问题:

I've got a splash screen with an ImageView playing the part of a background. Regardless of whether or not I allow my bitmap to be scaled (which I do), the image quality is horrible. I think it's reducing the color depth. I've looked around SO already and one suggestion was to place my image in raw instead of drawable but that didn't help either. Here is a screenshot:

What exactly is going on here and how do I fix it?

EDIT: I didn't apply this bitmap programmatically so there's no relevant Java code. Here is the XML code for this activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayoutSplash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawingCacheQuality="high"
    android:orientation="vertical"
    android:padding="@dimen/splash_padding"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    tools:context=".SplashActivity" >

    <ImageView
        android:id="@+id/ImageViewBg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/logo_description"
        android:scaleType="centerCrop"
        android:src="@drawable/splash_bg_register" />

    <ImageView
        android:id="@+id/ImageViewLogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/logo_description"
        android:maxHeight="@dimen/logo_maxheight"
        android:maxWidth="@dimen/logo_maxwidth"
        android:layout_centerVertical="true"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/TextViewCopyright"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/copyright"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/white_50"
        android:textSize="@dimen/copyright_size" />

</RelativeLayout>

EDIT: I tried getWindow().setFormat(PixelFormat.RGBA_8888); as suggested which made a visible difference but the banding is still present. This is the image I'm using as the background.

回答1:

Banding occurs mostly when aspect ratio of the image is disturbed.

You could try any one of the following:

  1. Make your image .9.png. In that case, it will automatically fix any stretches and take care of banding.

  2. Apply superficial dithering to your image which might prevent any unusual banding.

    <bitmap
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/b1"
        android:tileMode="repeat"
        android:dither="true" />
    


回答2:

What are the dimentions of your image? And what is the device? What I suspect is that image size does not match the size of the display and it is in wrong resource drawable folder. The image looks pretty scaled. These articles should help you out picking the image resolution and resource folder for different screens.

http://developer.android.com/guide/practices/screens_support.html

http://developer.android.com/training/multiscreen/index.html



回答3:

You have to set window color format. In your activity put something like this:

getWindow().setFormat(PixelFormat.RGBA_8888);


回答4:

I guess size of image is smaller than the area in which image is displayed. So, may be image is scaling up and loosing the quality.

What you can try is:

1. Either put the image of similar dimension 
2. use 9 patch image ( good idea )


回答5:

You need to resize your image into different sizes and put those into respective drawable folders under res/



回答6:

Try antialias this may solved you problem.

Create a bitmap drawable with the below xml.

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:antialias="true"
    android:dither="true"  />


回答7:

What you are doing is to set the image in xml that much more depends on what that image resolution support for .

there is a number of folders for each supported density of multiple devices , put your image on some suitable folder of dpi .

please refer this link for it http://developer.android.com/guide/practices/screens_support.html

see same app icon how behave diffidently in different folder

Example application without support for different densities, as shown on low, medium, and high density screens

Example application with good support for different densities (it's density independent), as shown on low, medium, and high density screens.



回答8:

Just tweak it by making the image a background image. Here is a sample code

   <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/your_image"
    />