我们在Windows 平台上要查看很多张图片,最简单的方法就是通过Windows图片和传真查看器,在上一张和下一张之间切换,Android平台上可以通过ImageSwicher类来实现这一效果(事实上通过其他方法也是可以实现的Bitmap)ImageSwitcher类必须设置一个ViewFactory,主要用来将显示的图片和父窗口区分开,通过makeView()方法来显示图片,这里会返回一个ImageView对象,而方法setImageResource用来显示指定的图片资源。我们现在看一个实例,当点击上一张或者下一张图片的时候切换浏览另一张图片。这里首先通过自己在界面中实现LinearLayout的方式来实现:

void android.widget.ImageSwitcher.setImageResource(int resid)

public void setImageResource(int resid)Added inAPI level 1

android.widget.LinearLayout.LayoutParams.LayoutParams(int width, int height)

public LinearLayout.LayoutParams(int width, int height) Added in API level 1


android.widget.Button.Button(Context context)


public Button (Context context)Added in API level 1


void android.widget.ViewSwitcher.setFactory(ViewFactory factory)


public void setFactory(ViewSwitcher.ViewFactory factory)

Added in API level 1

Sets the factory used to create the two views between which the ViewSwitcher will flip. Instead of using a factory, you can calladdView(android.view.View, int, android.view.ViewGroup.LayoutParams) twice.

Parameters
factory the view factory used to generate the switcher's content

void android.view. ViewGroup.addView( View child, LayoutParams params)

public void addView(View child,ViewGroup.LayoutParams params)

Added in API level 1

Adds a child view with the specified layout parameters.

Note: do not invoke this method from draw(android.graphics.Canvas),onDraw(android.graphics.Canvas),dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child the child view to add
params the layout parameters to set on the child


package com.example.activity01;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ViewSwitcher.ViewFactory;public class Activity01 extends Activity implements OnClickListener,ViewFactory{/* 所有要显示的图片资源索引 */private static final Integer[] imagelist = { R.drawable.img1, R.drawable.img2, R.drawable.img3,R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8,     }; //创建ImageSwitcher对象private ImageSwitcherm_Switcher;//索引private  intindex= 0;//“下一页”按钮IDprivate static final intBUTTON_DWON_ID= 0x123456;//“上一页”按钮IDprivate static final intBUTTON_UP_ID= 0x123457;//ImageSwitcher对象的IDprivate static final intSWITCHER_ID= 0x123458;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//创建一个线性布局LinearLayoutLinearLayout main_view = new LinearLayout(this);//创建ImageSwitcher对象m_Switcher = new ImageSwitcher(this);//在线性布局中添加ImageSwitcher视图main_view.addView(m_Switcher);//设置ImageSwitcher对象的IDm_Switcher.setId(SWITCHER_ID);//设置ImageSwitcher对象的数据源  首先放置图片  然后放置按钮m_Switcher.setFactory(this);m_Switcher.setImageResource(imagelist[index]);//设置显示上面创建的线性布局setContentView(main_view);//创建“下一张”按钮Button next = new Button(this);next.setId(BUTTON_DWON_ID);next.setText("下一张");next.setOnClickListener(this);//就是一个构造函数  用于生成LinearLayout.LayoutParams对象LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 100);main_view.addView(next, param);//创建“上一张”按钮Button pre = new Button(this);pre.setId(BUTTON_UP_ID);pre.setText("上一张");pre.setOnClickListener(this);main_view.addView(pre, param);}//事件监听、处理public void onClick(View v){switch (v.getId()){//下一页case BUTTON_DWON_ID:index++;if (index >= imagelist.length){index = 0;}//ImageSwitcher对象资源索引m_Switcher.setImageResource(imagelist[index]);break;//上一页case BUTTON_UP_ID:index--;if (index < 0){index = imagelist.length - 1;}//ImageSwitcher对象资源索引m_Switcher.setImageResource(imagelist[index]);break;default:break;}}public View makeView(){//将所有图片通过ImageView来显示return new ImageView(this);}}


下面是通过布局文件来设置界面组件的方式来实现的相关操作


<?xml version="1.0" encoding="utf-8"?>   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"               android:layout_width="fill_parent"               android:layout_height="fill_parent">       <LinearLayout                    android:orientation="horizontal"                     android:layout_width="fill_parent"                     android:layout_height="fill_parent"                     android:gravity="center"                    android:background="#f0f0f0"              >           <Button                   android:id="@+id/pre"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                  android:layout_gravity="center"                android:text="@+string/pre"                  />           <ImageSwitcher                   android:id="@+id/switcher"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                   android:layout_marginLeft="20dp"                  android:layout_marginRight="20dp"                  />           <Button                   android:id="@+id/next"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                   android:layout_gravity="center"                  android:text="@+string/next"                />          </LinearLayout>   </ScrollView>   

package com.example.activity01;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class Activity01 extends Activity  implements ViewFactory{/* 所有要显示的图片资源索引 */private static final Integer[] imagelist = { R.drawable.img1, R.drawable.img2, R.drawable.img3,R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8,     }; //创建ImageSwitcher对象private ImageSwitcher   m_Switcher = null;private Button   buttonNext = null;private Button   buttonPre = null;//索引private  int   index  = 0;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);buttonNext = (Button)findViewById(R.id.next);buttonPre  =(Button)findViewById(R.id.pre);m_Switcher = (ImageSwitcher)findViewById(R.id.switcher);m_Switcher.setFactory(this);m_Switcher.setImageResource(imagelist[index]);buttonNext.setOnClickListener(listener);buttonPre.setOnClickListener(listener);}//事件监听、处理OnClickListener listener = new OnClickListener(){public void onClick(View v){switch (v.getId()){//下一页case R.id.next:index++;if (index >= imagelist.length){index = 0;}//ImageSwitcher对象资源索引m_Switcher.setImageResource(imagelist[index]);break;//上一页case R.id.pre:index--;if (index < 0){index = imagelist.length - 1;}//ImageSwitcher对象资源索引m_Switcher.setImageResource(imagelist[index]);break;default:break;}}};@Overridepublic View makeView() {//将所有图片通过ImageView来显示return new ImageView(this);}}

很奇怪的一开始的界面布局xml文件并没有起作用


更多相关文章

  1. android直接在桌面生成快捷方式
  2. [android] HttpURLConnection的初步学习
  3. Android原生Json解析
  4. Android(安卓)中Message,MessageQueue,Looper,Handler详解+实例
  5. android TextView显示文字和图片
  6. Android(安卓)TextView中显示图片的4种方式
  7. Handler、HandlerThread理解
  8. 详解ImageView的CENTER_CROP,CENTER_INSIDE,FIT_CENTER等属性
  9. [置顶] Android(安卓)Gallery用法(自定义边框+底部小圆点)

随机推荐

  1. android EditText多行文本输入的若干问题
  2. 如何在Android的模拟器中的SD卡中添加文
  3. 基于WebRTC的Android数字楼宇对讲系统回
  4. Android事件管理源码剖析
  5. Android(安卓)TextView自动换行文字排版
  6. 关于应用Volley框架 + Android(安卓)网络
  7. Android(安卓)酷炫来袭:制作属于你自己的
  8. android 帧动画的替代方案
  9. 关于Android(安卓)Studio使用开发者允许
  10. 2018 Android面试心得,已拿到offer