简体   繁体   中英

Why does ImageButton quality blurs or reduces to worst?

My app has a ImageButton whose quality is too bad. The actual image dimensions are 4176*4176 (4176pixels) which looks completely fine when opened separately. But when I import this to android studio and assign it to a ImageButton, the quality of the image downgrades. I have been trying a number of ways to fix this but haven't succeeded yet. I'm completely new to Android Programming.I go to res/drawable then right click to create a new Image Asset and then specify the location which imports the png file.

Actual image: 在此处输入图片说明 Image inside studio: 在此处输入图片说明

Here is the code in activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">

<ImageButton
    android:id="@+id/police"
    android:layout_width="317dp"
    android:layout_height="295dp"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"

    android:background="@drawable/custom_button"
    />
</LinearLayout>

Custom button code which calls the image:

<item
    android:state_pressed="true"
    android:drawable="@mipmap/pressed" />

<item
    android:drawable="@mipmap/defaultt" />
</selector>

I'd start by reviewing density independent pixels on Android. This is a good blog that covers the concept: https://www.captechconsulting.com/blogs/understanding-density-independence-in-android

The main issue is that your image (which is very high res) is getting compressed to fit inside of the image button. Typically, you would want to include image assets for different densities via different drawable resource directories (eg, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi).

However, these days, you can use vector drawables, which will reduce the need for having multiple assets for each density bucket, assuming you have the ability to generate SVG asset files: https://developer.android.com/guide/topics/graphics/vector-drawable-resources

You are actually referencing the image like Launcher icon in /mipmap directory.

I suggest reducing the image size which you are only using it for ImageButton and placing it inside /drawable with the quality you want or maybe smaller than the current one instead of placing it inside /mipmap directory.

Also, I don't actually see the reason of how scaleType helps in ImageButton . You may wanna remove that line too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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