也算不上轮播,只是有两个按钮用来刷出上一张和下一张的图片。
先拖元件,新建string
<AbsoluteLayout 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="com.narumi.android_9_1.MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="105dp" android:layout_height="105dp" android:layout_x="100dp" android:layout_y="36dp" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="41dp" android:layout_y="160dp" android:text="@string/prev_btn" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="184dp" android:layout_y="160dp" android:="" android:text="@string/next_btn" /> </AbsoluteLayout>
写一点Java,新的知识点是针对v对象的判断语句以及求模运算的作用
package com.narumi.android_9_1; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { ImageView iv; Button btnPrev; Button btnNext; int[] imgId = { R.drawable.p01, R.drawable.p02, R.drawable.p03, R.drawable.p04, R.drawable.p05, R.drawable.p06, }; int currImgId = 0; private View.OnClickListener mylistener = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == btnPrev){ currImgId = (currImgId - 1 + imgId.length)%imgId.length; iv.setImageResource(imgId[currImgId]); }else if(v == btnNext){ currImgId = (currImgId+1)%imgId.length; iv.setImageResource(imgId[currImgId]); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageView)findViewById(R.id.imageView1); btnPrev = (Button)findViewById(R.id.button1); btnNext = (Button)findViewById(R.id.button2); btnPrev.setOnClickListener(mylistener); btnNext.setOnClickListener(mylistener); } }
最原始的无任何动画效果的图片切换就出来了,此例结束。