Search code examples
androidimagezoominghorizontalscrollview

Zooming an image of a ScrollView


I´m trying to include zooming in a Horizontal ScrollView. The idea is that the user touches the screen in the image he wants to highlight and that image get a little bit bigger than the others. I have make the Horizontall ScrollView but I´m having problems with the OnClick event. What is the better way to do it?

This is what I have in the MainActivity.java

 public class Carrusel extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.iniciar);


    {int[] images = new int[] { R.drawable.image1, R.drawable.image2, 
                                R.drawable.image3, R.drawable.image4,
                                R.drawable.image5 };
    LinearLayout sv = (LinearLayout) findViewById (R.id.carrusel);
    for (int i=0 ; i<5; i++){
       ImageView iv = new ImageView (this);
       iv.setBackgroundResource (images[i]);
       sv.addView(iv);

    }
    }
 }

    @Override   
    public void onClick(View v) {

        switch (v.getId()) {
        case R.drawable.image1:
            //I don´t know what to put here
        break;
        }


    }
    }

And here is what I have in te main_activity.xml

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/black" >

 <HorizontalScrollView 
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="10dp"
  >

 <LinearLayout
   android:id="@+id/carrusel"         
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" >
  </LinearLayout>

 </HorizontalScrollView>

   </RelativeLayout>

Solution

  • Here's how I'd do it:

    MainActivity.java :

    public class MainActivity extends Activity implements OnClickListener {
    
        ImageView lastClicked = null;
        int width = 400;
        int height = 320;
        int padding = 30;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LinearLayout l;
            l = (LinearLayout) findViewById(R.id.linear);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                width, height);
            int[] images = new int[] { R.drawable.image1, R.drawable.image2,
                        R.drawable.image3, R.drawable.image4, R.drawable.image5 };
            for (int i = 0; i < 5; i++) {
                ImageView iv = new ImageView(this);
                iv.setLayoutParams(layoutParams);
                iv.setImageResource(images[i]);
                iv.setPadding(padding, padding, padding, padding);
                iv.setOnClickListener(this);
                l.addView(iv);
            }
        }
    
        @Override
        public void onClick(View v) {
            if (v instanceof ImageView) {
                if (lastClicked != null) {
                    lastClicked.setPadding(padding, padding, padding, padding);
                    lastClicked.invalidate();
                }
                v.setPadding(0, 0, 0, 0);
                v.invalidate();
                lastClicked = (ImageView) v;
            }
        }
    }