缩放ScrollView的图像

时间:2013-07-14 11:47:46

标签: android image zoom horizontalscrollview

我试图在水平ScrollView中包含缩放。想法是用户触摸他想要突出显示的图像中的屏幕,并且该图像比其他图像稍微大一点。我已经制作了Horizo​​ntall ScrollView,但我遇到了OnClick事件的问题。有什么更好的方法呢?

这是我在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;
        }


    }
    }

以下是我在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>

1 个答案:

答案 0 :(得分:0)

我是这样做的:

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;
        }
    }
}