1

我正在尝试包括放大水平滚动视图。这个想法是用户在他想要突出显示的图像中触摸屏幕,并且该图像会比其他图像大一点。我已经制作了 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>
4

1 回答 1

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;
        }
    }
}
于 2013-07-14T15:11:25.727 回答