Steve P.
Steve P.

Reputation: 14699

Android: Attempting to switch between two Images and display them as the same size

So, basically, I just want to display an image, and when that image is clicked, I want to replace it with another image (and go back and forth between the two images onClick). This works fine, but I want to display them as the exact same size, but no matter what I try, they're not appearing as the same size on screen.

Here's my java file:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ImageView baneling = (ImageView) findViewById(R.id.bane);
    baneling.setTag("alive");
    baneling.setClickable(true);
    baneling.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(baneling.getTag().equals("alive"))
            {
                baneling.setImageResource(R.drawable.banelingexplosion);
                baneling.setTag("dead");
                resizeImage(baneling);
            }
            else
            {
                baneling.setImageResource(R.drawable.baneling);
                baneling.setTag("alive");
            }
        }
    });
}

public void resizeImage(ImageView v)
{
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
    v.setLayoutParams(layoutParams);
}

Here's my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dip" >
    <ImageView
            android:id="@+id/bane"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/baneling" />
</LinearLayout>

I've tried a ton of things and have successfully resized the images, but can never get them to be the same size, even why I explicitly assign them numerical pixel values in the xml and in the .java.

Upvotes: 3

Views: 1455

Answers (1)

Tyler MacDonell
Tyler MacDonell

Reputation: 2629

Setting the android:scaleType="fitXY" attribute in your ImageView will make sure your image fills the entire bounds of the ImageView. That should help with explicitly setting the width and height of your image.

Nice Starcraft theme, by the way.

Upvotes: 2

Related Questions