在win7中查看图片会有图片预览窗格效果,下面通过一个实例来说。
1.界面布局
在xml布局文件中,我们使用LinearLayout对整个界面进行水平布局。在界面的左端设置了一个GirdView控件,用来显示列表。在GridView控件的右面设置一个ImageSwitcher,a当点击GridView中的图片时,会在ImageView上显示出来。代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.eg_win7viewphoto.MainActivity" > <GridView android:id="@+id/gridview" android:layout_width="418dp" android:layout_height="432dp" android:layout_marginTop="10px" android:layout_weight="0.65" android:horizontalSpacing="3px" android:numColumns="4" android:verticalSpacing="3px" > </GridView> <ImageSwitcher android:id="@+id/imageswitcher" android:layout_width="499dp" android:layout_height="match_parent" android:padding="20px" /> </LinearLayout>
2.程序运行主要界面如下:
3.ViewFactory接口
要将图片显示在ImageSwitcher控件中,必须为ImageSwitcher类设置一个ViewFactory,用来将显示的图片和父窗口区分开来。这可以通过如下方法来实现:
ImageSwitcher.setFactory();
此外,我们还需要实现ViewSwitcher.ViewFactory接口中的makeView()抽象方法,通过该方法可以返回一个ImageView对象,所有图片都将通过该ImageView对象来进行显示。具体实现方法如下:
imageswitcher.setFactory(new ViewFactory() { ImageView imageview1; @Override public View makeView() { // TODO Auto-generated method stub imageview1=new ImageView(MainActivity.this); imageview1.setScaleType(ImageView.ScaleType.FIT_CENTER); imageview1.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); return imageview1; } });
4.获取图片资源
通过查看ImageSwitcher的API帮助文档
(http://developer.android.com/reference/android/widget/ImageSwitcher.html),我们可以看出向ImageSwitcher加载图片有以下三种方式:
(1)public void setImageDrawable(Drawable drawable);
(2)public void setImageResource(int resid);
(3)public void setImageURI(Uri uri);
其中,setImageDrawable()方法通过Drawable对象来获取图片资源;setImageResource()方法通过图片资源Id来获取图片资源;setImageURI()方法通过图片的Uri路径来获取图片资源。
在该实例中,我们选择使用setImageResource()方法来获取图片资源.
imageswitcher.setImageResource(imageId[((imageId.length)/2)]);
5.创建BaseAdapter对象,重写该对象的getView()方法设置显示图片的格式
BaseAdapter adapter=new BaseAdapter() { ImageView imageview2; @Override public View getView(int position, View contentView, ViewGroup parent) { // TODO Auto-generated method stub if(contentView==null){ imageview2=new ImageView(MainActivity.this); /*****设置图像高度与宽度******/ imageview2.setAdjustViewBounds(true); imageview2.setMaxHeight(113); imageview2.setMaxWidth(150); /**********************/ imageview2.setPadding(5, 5, 5, 5); }else{ imageview2=(ImageView)contentView; } imageview2.setImageResource(imageId[position]); return imageview2; } /* * 获得当前选项的ID(non-Javadoc) * @see android.widget.Adapter#getItemId(int) */ @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } /** * 获得当前选项 */ @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } /** * 获得数量 */ @Override public int getCount() { // TODO Auto-generated method stub return imageId.length; } };
6.将GridVIew与适配器BaseAdapter对象关联,设置单击GridView对象监听器
gridView.setAdapter(adapter);//将适配器与GridView gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parant, View view, int position , long id) { // TODO Auto-generated method stub imageswitcher.setImageResource(imageId[position]);//显示选中的图片 } });
附:Java全部代码
package com.example.eg_win7viewphoto; import android.support.v7.app.ActionBarActivity; import android.app.ActionBar.LayoutParams; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends ActionBarActivity { private int[] imageId=new int[]{ R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04, R.drawable.img04,R.drawable.img05,R.drawable.img07,R.drawable.img08, R.drawable.img09,R.drawable.img10,R.drawable.img11,R.drawable.img12 }; private ImageSwitcher imageswitcher; private GridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageswitcher=(ImageSwitcher)findViewById(R.id.imageswitcher); gridView=(GridView)findViewById(R.id.gridview); imageswitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageswitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); imageswitcher.setFactory(new ViewFactory() { ImageView imageview1; @Override public View makeView() { // TODO Auto-generated method stub imageview1=new ImageView(MainActivity.this); imageview1.setScaleType(ImageView.ScaleType.FIT_CENTER); imageview1.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); return imageview1; } }); imageswitcher.setImageResource(imageId[((imageId.length)/2)]); BaseAdapter adapter=new BaseAdapter() { ImageView imageview2; @Override public View getView(int position, View contentView, ViewGroup parent) { // TODO Auto-generated method stub if(contentView==null){ imageview2=new ImageView(MainActivity.this); /*****设置图像高度与宽度******/ imageview2.setAdjustViewBounds(true); imageview2.setMaxHeight(113); imageview2.setMaxWidth(150); /**********************/ imageview2.setPadding(5, 5, 5, 5); }else{ imageview2=(ImageView)contentView; } imageview2.setImageResource(imageId[position]); return imageview2; } /* * 获得当前选项的ID(non-Javadoc) * @see android.widget.Adapter#getItemId(int) */ @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } /** * 获得当前选项 */ @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } /** * 获得数量 */ @Override public int getCount() { // TODO Auto-generated method stub return imageId.length; } }; gridView.setAdapter(adapter);//将适配器与GridView gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parant, View view, int position , long id) { // TODO Auto-generated method stub imageswitcher.setImageResource(imageId[position]);//显示选中的图片 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }