GridView + ImageSwitch 图片点击切换

效果:

image 

点击上面的图片,下面的大图实现切换

此包含三个文件: 两个xml一个主程序java文件

java代码:

public class MainActivity extends Activity {
 
    private static final String tag = "==CrazyIt.org==";
    int[] imgs = new int[] { R.drawable.x1, R.drawable.x2, R.drawable.x3,
            R.drawable.x5, R.drawable.x4 };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        List<Map<String, Object>> li = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imgs.length; i++) {
            Map<String, Object> l = new HashMap<String, Object>();
            l.put("image", imgs[i]);
            li.add(l);
        }
 
        final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.is1);
        is.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left));
        is.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right));
        is.setFactory(new ViewFactory() {
 
            public View makeView() {
                // TODO Auto-generated method stub
                ImageView iv = new ImageView(MainActivity.this);
                iv.setBackgroundColor(0xff0000);
                iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                iv.setLayoutParams(new ImageSwitcher.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                return iv;
 
            }
        });
 
        SimpleAdapter sa = new SimpleAdapter(this, li, R.layout.cell,
                new String[] { "image" }, new int[] { R.id.image1 });
        GridView gv = (GridView) findViewById(R.id.gv1);
        gv.setAdapter(sa);
        gv.setOnItemSelectedListener(new OnItemSelectedListener() {
 
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                is.setImageResource(imgs[position % imgs.length]);
                Toast.makeText(MainActivity.this,
                        String.valueOf(position % imgs.length),
                        Toast.LENGTH_LONG).show();
            }
 
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
 
            }
        });
 
        gv.setOnItemClickListener(new OnItemClickListener() {
 
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                is.setImageResource(imgs[position % imgs.length]);
                Toast.makeText(MainActivity.this,
                        String.valueOf(position % imgs.length),
                        Toast.LENGTH_LONG).show();
 
            }
        });
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
 
    <GridView
        android:id="@+id/gv1"
        android:layout_width="match_parent"
        android:layout_height="139dp"
        android:numColumns="8" >
 
    </GridView>
 
    <ImageSwitcher
        android:id="@+id/is1"
        android:layout_width="match_parent"
        android:layout_height="184dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="4.06"
        android:padding="40dp" >
 
    </ImageSwitcher>
 
</LinearLayout>
 
 
 
 
 

另一个xml文件 cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="true"
    android:gravity="center_horizontal"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
posted @ 2012-11-16 12:36  高捍得  阅读(416)  评论(0编辑  收藏  举报