国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

很贊的引導界面效果Android控件ImageSwitcher實現

2019-12-12 06:18:51
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android控件ImageSwitcher實現引導界面的代碼,供大家參考,具體內容如下

效果圖:

布局代碼:

<?xml version="1.0" encoding="UTF-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:layout_width="fill_parent"       android:layout_height="fill_parent">  <ImageSwitcher    android:id="@+id/imageSwitcher"    android:layout_width="fill_parent"    android:layout_height="fill_parent">  </ImageSwitcher>  <RelativeLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <LinearLayout      android:id="@+id/ll_view"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_marginBottom="30dp"      android:gravity="center_horizontal"      android:orientation="horizontal">    </LinearLayout>  </RelativeLayout></FrameLayout>

頁面代碼:

public class ImageSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnTouchListener {  private int[] imgIds;//圖片id數組  private int currentPosition;//當前選中的圖片id序號  private ImageSwitcher mImageSwitcher;//ImagaSwitcher 的引用  private float downX;//按下點的X坐標  private ImageView[] tips;//點點數組  private LinearLayout linearLayout;//裝載點點的容器  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_switcher);    imgIds = new int[]{R.drawable.bg, R.drawable.c2, R.drawable.c3, R.drawable.c4, R.drawable.c5, R.drawable.c6, R.drawable.c7, R.drawable.c8, R.drawable.c9};    mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);//實例化ImageSwitcher    mImageSwitcher.setFactory(this); //設置Factory    mImageSwitcher.setOnTouchListener(this);//設置OnTouchListener,我們通過Touch事件來切換圖片    linearLayout = (LinearLayout) findViewById(R.id.ll_view);//指示器布局    tips = new ImageView[imgIds.length];    for (int i = 0; i < imgIds.length; i++) {      ImageView mImageView = new ImageView(this);      tips[i] = mImageView;      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));      layoutParams.rightMargin = 3;      layoutParams.leftMargin = 3;      mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);      linearLayout.addView(mImageView, layoutParams);    }    //上一個界面傳過來的位置    currentPosition = getIntent().getIntExtra("position", 0);    mImageSwitcher.setImageResource(imgIds[currentPosition]);    setImageBackground(currentPosition);  }  //設置選中的tip的背景  private void setImageBackground(int selectItems) {    for (int i = 0; i < tips.length; i++) {      if (i == selectItems) {        tips[i].setBackgroundResource(R.drawable.page_indicator_focused);      } else {        tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);      }    }  }  @Override  public View makeView() {    final ImageView i = new ImageView(this);    i.setBackgroundColor(0xff000000);    i.setScaleType(ImageView.ScaleType.CENTER_CROP);    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));    return i;  }  @Override  public boolean onTouch(View v, MotionEvent event) {    switch (event.getAction()) {      case MotionEvent.ACTION_DOWN: {        downX = event.getX();//手指按下的X坐標        break;      }      case MotionEvent.ACTION_UP: {        float lastX = event.getX();        //抬起的時候的X坐標大于按下的時候就顯示上一張圖片        if (lastX > downX) {          if (currentPosition > 0) {            //設置動畫            mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));            mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out));            currentPosition--;            mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);            setImageBackground(currentPosition);          } else {            Toast.makeText(getApplication(), "已經是第一張", Toast.LENGTH_SHORT).show();          }        }        if (lastX < downX) {          if (currentPosition < imgIds.length - 1) {            mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in));            mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.lift_out));            currentPosition++;            mImageSwitcher.setImageResource(imgIds[currentPosition]);            setImageBackground(currentPosition);          } else {            Toast.makeText(getApplication(), "到了最后一張", Toast.LENGTH_SHORT).show();          }        }      }      break;    }    return true;  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明水县| 体育| 兴安县| 定安县| 成都市| 台安县| 澎湖县| 南雄市| 银川市| 桐柏县| 桂阳县| 富源县| 新田县| 五大连池市| 随州市| 饶平县| 枣庄市| 三江| 丹巴县| 内乡县| 大兴区| 永修县| 隆化县| 东山县| 蓝田县| 枣强县| 平顺县| 马边| 白银市| 福安市| 兰考县| 安福县| 阿克| 雷波县| 来安县| 噶尔县| 昌吉市| 望江县| 崇明县| 崇明县| 云林县|