activity_main.xml

控件的线性布局

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:id="@+id/imageSwitcher"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:id="@+id/butPrevious"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:enabled="false"

android:text="上一张图片"/>

android:id="@+id/butNext"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="下一张图片"/>

MainActivity.java

package com.example.imageswitcherproject;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

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 butPrevious=null;//上一张图片按钮

private Button butNext=null;//下一张图片按钮

private ImageSwitcher imageSwitcher=null;

private int[] imgRes={R.drawable.ispic_a,R.drawable.ispic_b,

R.drawable.ispic_c,R.drawable.ispic_d,

R.drawable.ispic_e};//图片控件的ID

private int foot=0;//记录图片的下标

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

this.butPrevious=(Button)super.findViewById(R.id.butPrevious);

this.butNext=(Button)super.findViewById(R.id.butNext);

this.imageSwitcher=(ImageSwitcher)super.findViewById(R.id.imageSwitcher);

this.imageSwitcher.setFactory(new OnFactorylmpl());//设置转换工厂

this.imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置动画

this.imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置动画

this.imageSwitcher.setImageResource(this.imgRes[foot++]);//设置初始图片

this.butPrevious.setOnClickListener(new OnClickListenerPrevious());

this.butNext.setOnClickListener(new OnClickListenerNext());

}

private void checkButEnable(){//设置按钮状态

if(this.foot

this.butNext.setEnabled(true);

}else{

this.butNext.setEnabled(false);

}

if(this.foot==0){

this.butPrevious.setEnabled(false);

}else{

this.butPrevious.setEnabled(true);

}

}

private class OnClickListenerNext implements OnClickListener{//监听按钮

@Override

public void onClick(View v) {

MainActivity.this.imageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot++]);

MainActivity.this.checkButEnable();

}

}

private class OnClickListenerPrevious implements OnClickListener{//监听按钮

@Override

public void onClick(View v) {

MainActivity.this.imageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot--]);

MainActivity.this.checkButEnable();

}

}

private class OnFactorylmpl implements ViewFactory{

@Override

public View makeView() {

ImageView image=new ImageView(MainActivity.this);//创建图片控件

image.setBackgroundColor(0xFFFFFFFF);//设置背景颜色

image.setScaleType(ImageView.ScaleType.CENTER); //设置显示方式

image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.FILL_PARENT));//定义组建

return image;

}

}

@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;

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐