Fustigador
Fustigador

Reputation: 6459

PNG not showing in ImageView

I have a large .png and i want to show it in a Splash screen, but the image is not showing. The xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.infaplic.lpi.activities.SplashActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView2"/>

And the code of the Activity:

public class SplashActivity extends Activity {

ImageView imagen;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    imagen=(ImageView)findViewById(R.id.imageView2);
    imagen.setImageDrawable(getResources().getDrawable(R.drawable.pantalla_arranque));
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent mainIntent = new Intent(SplashActivity.this, NextActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, 1000);
}
}

This way, the image is not showing. I have tried with android:src: in the xml, but it doesn't work.

The image is very large. Do I need to resize it before putting it in the ImageView? If not, why is the image not showing?

Thank you.

EDIT: The PNG is 1080x1920

Upvotes: 4

Views: 41850

Answers (5)

azamatikus
azamatikus

Reputation: 73

In your .xml file add
xmlns:app="http://schemas.android.com/apk/res-auto"

then in the ImageView add this line of code:

app:srcCompat="@drawable/books"

NOT

tools:srcCompat="@drawable/books"

Upvotes: 0

Ajay Naredla
Ajay Naredla

Reputation: 408

In my case I was using a image with file name like "go-back-filename.png". And xml file is like

 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/go-back-icon"/>

After i changed the file name to "go_back_icon" it worked fine. Hope this helps someone.

Upvotes: 18

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

Change your ImageView to this :

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView2"
android:scaleType="fitXY"
android:adjustViewBounds="false"/>

If it's not working try to add this on your manifest (inside your Activity) android:hardwareAccelerated="false"

EDIT

I was looking for the answer, but I thought that better try it by my self... and I've created a sample project where I use an ImageView 1080x1920 and I was testing since I've found that I've I put 3000 miliseconds it shows up... wiht 1000 miliseconds won't show up if you don't rescale it.

By the way getDrawable() is deprecated I recomend to you use :

imagen.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.imagenprueva));

XML

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@drawable/imagen"
        />
    </RelativeLayout>

Here's the full source code hope it helps Source code

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Add Below this. Please read Options for scaling the bounds of an image to the bounds of this view.ImageView.ScaleType in your ImageView

Use This

imagen.setImageResource(R.drawable.pantalla_arranque);

Then

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"/>  

android:adjustViewBounds : Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Must be a boolean value, either "true" or "false".
Please Check demo ImageView scaleType Samples . I hope it helps you

Upvotes: 4

rekire
rekire

Reputation: 47945

Since you write that your image is very large I would guess it is too large. I have a bug in one of my apps there I generate an image which is bigger then 2048 pixels. Some devices does not support such big images I would guess that you run into a similar problem.

If that is some background image reduce the image size, a background image is not so important that each pixel on the screen has to own a image pixel.

Upvotes: 15

Related Questions