您好,登錄后才能下訂單哦!
直接代碼:后面附上壓縮文件:
package cn.kevin.test.matrixtest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnPageChangeListener {
private ViewPager viewPager;
private int[] idArray;// 圖片
private ImageView[] tips;// 點點的圖片
private ImageView[] mImages;// 放圖片的數組
private LinearLayout radio;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
radio = (LinearLayout) findViewById(R.id.radio);
idArray = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4, R.drawable.a5 };
tips = new ImageView[idArray.length];
for (int i = 0; i < tips.length; i++) {
ImageView p_w_picpath = new ImageView(MainActivity.this);
tips[i] = p_w_picpath;
if (i == 0) {
tips[i].setBackgroundResource(R.drawable.p_w_picpath_select);
} else {
tips[i].setBackgroundResource(R.drawable.p_w_picpath);
}
radio.addView(p_w_picpath);
p_w_picpath.setPadding(10, 10, 10, 10);
radio.setGravity(Gravity.CENTER);
radio.setOrientation(0);
}
mImages = new ImageView[tips.length];
for (int i = 0; i < mImages.length; i++) {
ImageView mImage = new ImageView(MainActivity.this);
mImages[i] = mImage;
mImage.setBackgroundResource(idArray[i]);
}
viewPager.setAdapter(new MyAdapter());
viewPager.setOnPageChangeListener(this);
// 設置ViewPager的默認項, 設置為長度的100倍,這樣子開始就能往左滑動
// viewPager.setCurrentItem((mImages.length) * 100);
}
public class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mImages[position % mImages.length]);
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(mImages[position % mImages.length],
0);
return mImages[position % mImages.length];
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
setImageBackGround(arg0 % mImages.length);
}
public void setImageBackGround(int selectItem) {
for (int i = 0; i < tips.length; i++) {
if (i == selectItem) {
tips[i].setBackgroundResource(R.drawable.p_w_picpath_select);
} else {
tips[i].setBackgroundResource(R.drawable.p_w_picpath);
}
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。