标签:
1
|
public ImageSwitcher(Context context)
|
构造
|
创建ImageSwitcher对象
|
2
|
public void setFactory(ViewSwitcher.ViewFactory factory)
|
普通
|
设置ViewFactory对象。用于完毕两个图片切换时ViewSwitcher的转换操作
|
3
|
public void setImageResource(int resid)
|
普通
|
设置显示的图片资源ID
|
4
|
public void setInAnimation(Animation inAnimation)
|
普通
|
图片读取进ImageSwitcher时的动画效果
|
5
|
public void setOutAnimation(Animation outAnimation)
|
普通
|
图片从ImageSwitcher要消失时的动画效果
|
<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: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=".MainActivity" > <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="114dp" > </ImageSwitcher> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_marginRight="20dp" android:layout_toLeftOf="@+id/imageSwitcher1" android:text="上一张" /> <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/imageSwitcher1" android:text="下一张" /> </RelativeLayout>
package com.example.imageswitcher; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { private Button ButNext, ButPrevious;//初始化button private ImageSwitcher imageSwitcher;//初始化组件 private int Images[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6 };//设置图片数据 private int foot = 0;//设置角标 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);//获得组件 ButNext = (Button) this.findViewById(R.id.button1); ButPrevious = (Button) this.findViewById(R.id.button2); imageSwitcher.setFactory(new Myfactory());//为组件设置组件工厂 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation( MainActivity.this, android.R.anim.fade_in));//设置图片进入动画 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation( MainActivity.this, android.R.anim.fade_out));//设置图片离开动画 imageSwitcher.setImageResource(Images[foot++]);//设置图片 // button事件监听 ButNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub imageSwitcher.setImageResource(Images[foot++]); MainActivity.this.CheckEnable();//设置button是否可用防止数组越界 } }); // button事件监听 ButPrevious.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub imageSwitcher.setImageResource(Images[foot--]); MainActivity.this.CheckEnable(); } }); } class Myfactory implements ViewFactory { @Override public View makeView() { // TODO Auto-generated method stub ImageView image = new ImageView(MainActivity.this);//设置图片组件 image.setBackgroundColor(Color.GRAY);//设置对齐效果 image.setScaleType(ImageView.ScaleType.CENTER);//设置剧中 image.setLayoutParams(new ImageSwitcher.LayoutParams( // 自适应图片大小 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // 定义组件 return image;//返回图片 } } public void CheckEnable() { if (this.foot < this.Images.length - 1) { this.ButNext.setEnabled(true); // button可用 } else { this.ButNext.setEnabled(false); // button不可用 } if (this.foot == 0) { this.ButPrevious.setEnabled(false); // button不可用 } else { this.ButPrevious.setEnabled(true); // button可用 } } }
从零開始学android<ImageSwitcher图片切换组件.二十六.>
标签:
原文地址:http://www.cnblogs.com/yxwkf/p/5060775.html