您的位置:首页 > 其它

GridView + ImageSwitch 图片点击切换

2012-11-16 12:36 218 查看
效果:



 

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

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

java代码:

public class MainActivity extends Activity {


[code] 


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;


}


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


[code] 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>

[/code]

 


 


 


 


 


另一个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>


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐