Asimina88
Asimina88

Reputation: 107

how to scale a imageview in Android with a button?

I have this app im building that i want it to scale a imageview center.

This is my image

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/lamp" />

enter image description here

and two buttons + and -

<Button
    android:id="@+id/plusBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:layout_alignTop="@+id/minusBtn"
    android:layout_alignEnd="@+id/cameraImageButton2" />

<Button
    android:id="@+id/minusBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="-"
    android:layout_above="@+id/videoOnlineImageButton"
    android:layout_alignStart="@+id/videoOnlineImageButton" />

I want when you click on the minus the image to scale down. lets say it was 100x100 it could become 90x90 enter image description here

when you click on the plus to increase the size. lets say from 100x100 to 110x110

and so on.....

I need the image to scale and remain in the center of my screen. so somehow it should scale from its default size up or down.


This is what worked for me guys thanks to Alpha mori!

plusbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    float x = imagview.getScaleX();
    float y = imagview.getScaleY();

    imagview.setScaleX((float) (x + 5));
    imagview.setScaleY((float) (y + 5));

            }
        });


  minusbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    float x = imagview.getScaleX();
    float y = imagview.getScaleY();

    imagview.setScaleX((float) (x - 5));
    imagview.setScaleY((float) (y - 5));

            }
        });

Upvotes: 2

Views: 1121

Answers (1)

Alpa Mori
Alpa Mori

Reputation: 260

plusbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    float x = imagview.getScaleX();
    float y = imagview.getScaleY();

    imagview.setScaleX((float) (x + 5));
    imagview.setScaleY((float) (y + 5));

            }
        });


  minusbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    float x = imagview.getScaleX();
    float y = imagview.getScaleY();

    imagview.setScaleX((float) (x - 5));
    imagview.setScaleY((float) (y - 5));

            }
        });

Upvotes: 1

Related Questions