您的位置:首页 > 移动开发 > Android开发

Android图片轮播效果的几种实现方法

2015-02-10 10:21 609 查看
大家在使用APP的过程中,经常会看到上部banner图片轮播的效果,那么今天我们就一起来学习一下,android中图片轮询的几种实现方法:

第一种:使用动画的方法实现:(代码繁琐)

这种发放需要:两个动画效果,一个布局,一个主类来实现,不多说了,来看代码吧:

public class IamgeTrActivity extends Activity {

/** Called when the activity is first created. */

public ImageView imageView;

public ImageView imageView2;

public Animation animation1;

public Animation animation2;

public TextView text;

public boolean juage = true;

public int images[] = new int[] { R.drawable.icon, R.drawable.expriment,

R.drawable.changer, R.drawable.dataline, R.drawable.preffitication };

public int count = 0;

public Handler handler = new Handler();

public Runnable runnable = new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

AnimationSet animationSet1 = new AnimationSet(true);

AnimationSet animationSet2 = new AnimationSet(true);

imageView2.setVisibility(0);

TranslateAnimation ta = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,

-1f, Animation.RELATIVE_TO_SELF, 0f,

Animation.RELATIVE_TO_SELF, 0f);

ta.setDuration(2000);

animationSet1.addAnimation(ta);

animationSet1.setFillAfter(true);

ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,

Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,

0f, Animation.RELATIVE_TO_SELF, 0f);

ta.setDuration(2000);

animationSet2.addAnimation(ta);

animationSet2.setFillAfter(true);

//iamgeView 出去 imageView2 进来

imageView.startAnimation(animationSet1);

imageView2.startAnimation(animationSet2);

imageView.setBackgroundResource(images[count % 5]);

count++;

imageView2.setBackgroundResource(images[count % 5]);

text.setText(String.valueOf(count));

if (juage)

handler.postDelayed(runnable, 6000);

Log.i("handler", "handler");

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

imageView = (ImageView) findViewById(R.id.imageView);

imageView2 = (ImageView) findViewById(R.id.imageView2);

text=(TextView)findViewById(R.id.text);

text.setText(String.valueOf(count));

//将iamgeView先隐藏,然后显示

imageView2.setVisibility(4);

handler.postDelayed(runnable, 2000);

}

public void onPause() {

juage = false;

super.onPause();

}

}

布局代码:

<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/rl">

<ImageView

android:id="@+id/imageView"

android:layout_width="fill_parent"

android:background="@drawable/icon"

android:layout_below="@+id/rl"

android:layout_height="120dp" />

<ImageView

android:id="@+id/imageView2"

android:layout_width="fill_parent"

android:background="@drawable/expriment"

android:layout_below="@+id/rl"

android:layout_height="120dp" />

<TextView

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/imageView"/>

</RelativeLayout>

代码下载地址:http://download.csdn.net/detail/weimo1234/8438135

第二种:使用ViewFlipper实现图片的轮播

Android系统自带的一个多页面管理控件,它可以实现子界面的自动切换:

首先 需要为ViewFlipper加入View

(1) 静态导入:在layout布局文件中直接导入

(2) 动态导入:addView()方法

ViewPlipper常用方法:

setInAnimation:设置View进入屏幕时候使用的动画

setOutAnimation:设置View退出屏幕时候使用的动画

showNext:调用该函数来显示ViewFlipper里面的下一个View

showPrevious:调用该函数来显示ViewFlipper里面的上一个View

setFlipInterval:设置View之间切换的时间间隔

startFlipping使用上面设置的时间间隔来开始切换所有的View,切换会循环进行

stopFlipping:停止View切换

讲了这么多,那么我们今天要实现的是什么呢?

(1) 利用ViewFlipper实现图片的轮播

(2) 支持手势滑动的ViewFlipper

我们需要先准备几张图片:把图片放进drawable中

创建两个动画:在res下面新建一个folder里面新建两个xml:

left_in:

<?xml version="1.0" encoding="utf-8"?>

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

<translate

android:duration="5000"

android:fromXDelta="100%p"

android:toXDelta="0"/>

</set>

left_out:

<?xml version="1.0" encoding="utf-8"?>

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

<translate

android:fromXDelta="0"

android:toXDelta="-100%p"

android:duration="5000"/>

</set>

一个布局文件:

<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"

tools:context=".MainActivity" >

<ViewFlipper

android:id="@+id/flipper"

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

</RelativeLayout>

一个主类:

public class MainActivity extends Activity {

private ViewFlipper flipper;

private int[] resId = {R.drawable.pc1,R.drawable.pc2,R.drawable.pc3,R.drawable.pc4};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

flipper = (ViewFlipper) findViewById(R.id.flipper);

/*

* 动态导入的方式为ViewFlipper加入子View

* */

for (int i = 0; i < resId.length; i++) {

flipper.addView(getImageView(resId[i]));

}

/*

* 为ViewFlipper去添加动画效果

* */

flipper.setInAnimation(this, R.anim.left_in);

flipper.setOutAnimation(this, R.anim.left_out);

flipper.setFlipInterval(5000);

flipper.startFlipping();

}

private ImageView getImageView(int resId){

ImageView image = new ImageView(this);

image.setBackgroundResource(resId);

return image;

}

}

那么这样就实现了一个图片轮询的功能效果了

我们还可以添加点击,滑动效果:

我们还需要添加两个向右的滑动效果:

right_in:

<?xml version="1.0" encoding="utf-8"?>

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

<translate

android:fromXDelta="0"

android:toXDelta="-100%p"

android:duration="2000"/>

</set>

right_out:

<?xml version="1.0" encoding="utf-8"?>

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

<translate

android:fromXDelta="100%p"

android:toXDelta="0"

android:duration="2000"/>

</set>

然后我们还需要在主类里面添加(如果你不想让图片自动播放,只想通过手势来实现图片播放那么你需要把“为ViewFlipper添加动画效果的代码”删掉):

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

startX = event.getX();

break;

case MotionEvent.ACTION_MOVE://判断向左滑动还是向右滑动

if (event.getX() - startX > 100) {

flipper.setInAnimation(this, R.anim.left_in);

flipper.setOutAnimation(this, R.anim.left_out);

flipper.showPrevious();

}else if (startX - event.getX() > 100) {

flipper.setInAnimation(this, R.anim.right_in);

flipper.setOutAnimation(this, R.anim.right_out);

flipper.showNext();

}

case MotionEvent.ACTION_UP:

break;

}

return super.onTouchEvent(event);

}

这样我们利用我们的ViewFlipper完成的图片轮询的功能就做完了。

代码下载地址:http://download.csdn.net/detail/weimo1234/8438129

注:以后发现更多的方法还将续写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: