android图片自动定时切换

android图片自动定时切换

布局文件: 

        <RelativeLayout 

             android:id="@+id/rv_img_nupt"
             android:layout_width="match_parent"
             android:layout_height="100dp"
             android:gravity="center">
           <!-- ViewFlipper是可以使用动画的控件,可以分页,这里因个人项目需要,把像素给定了 -->
             <ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="853px"
android:layout_height="510px">
                 <!-- 第一页 -->
                 <LinearLayout 
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent">
                     <ImageView
          android:id="@+id/eduinfo_img_nupt1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/app_name"
            android:background="@drawable/ying"/>
                 </LinearLayout>
                 <!-- 第二页 -->
                 <LinearLayout 
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent">
                     <ImageView
          android:id="@+id/eduinfo_img_nupt2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/app_name"
            android:background="@drawable/biye"/>
                 </LinearLayout>
                 <!-- 第三页 -->
                 <LinearLayout 
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent">
                     <ImageView
          android:id="@+id/eduinfo_img_nupt3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/app_name"
            android:background="@drawable/zhaopian"/>
                 </LinearLayout>
             </ViewFlipper>

         </RelativeLayout>


Activity:

public class EduinfoLayoutActivity extends Activity implements OnClickListener{
//返回主界面的ImageView
private ImageView imgSlide;
private ViewFlipper viewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eduinfo_layout);
//返回主界面的ImageView
imgSlide=(ImageView) findViewById(R.id.img_slide);
imgSlide.setOnClickListener(this);

//初始化参数
final Animation in = AnimationUtils.loadAnimation(this, R.anim.in_alpha);
final Animation out = AnimationUtils.loadAnimation(this, R.anim.out_alpha);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
//开始图片自动播放,第一个时间为间隔时间,第二个时间未知
new CountDownTimer(3500,100000) {

@Override
public void onTick(long millisUntilFinished) {
}

@Override
public void onFinish() {
viewFlipper.setInAnimation(in);
viewFlipper.setOutAnimation(out);
viewFlipper.showNext();
start();
}
}.start();

}

}



anim文件夹下建一个in_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set android:oneshot="false"  xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0"
        android:toAlpha="1.0"
        android:duration="2000"/>
</set>


anim文件夹下建一个out_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set android:oneshot="false"  xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0"
        android:toAlpha="1.0"
        android:duration="2000"/>
</set>


猜你喜欢

转载自blog.csdn.net/qq_33897656/article/details/52551305