Android引導頁實現(帶動點)

YouGc發表於2017-04-07

程式碼貼上來有些不見了 可以參考一下 整個專案放在百度雲上解壓就可以在ecplice匯入即可 https://pan.baidu.com/s/1miG4sh6

yingdaye.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/Viewpagerid"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

<LinearLayout
    android:id="@+id/dianjiheid"
    android:layout_width="match_parent"
    android:layout_height="20px"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="100dp"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageviewid1"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />

    <ImageView
        android:id="@+id/imageviewid2"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />

    <ImageView
        android:id="@+id/imageviewid3"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />

    <ImageView
        android:id="@+id/imageviewid4"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />
</LinearLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="20px"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="70dp">


<LinearLayout
    android:id="@+id/dianjiheid2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageviewid12"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />

    <ImageView
        android:id="@+id/imageviewid22"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />

    <ImageView
        android:id="@+id/imageviewid32"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />

    <ImageView
        android:id="@+id/imageviewid42"
        android:layout_width="20px"
        android:layout_height="match_parent"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:background="#000000" />


</LinearLayout>

<ImageView
    android:id="@+id/imageviewid6"
    android:layout_width="20px"
    android:layout_height="20px"
    android:background="#87cefa" />

yingdayetupiang.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/yingdandyetupiang" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

<Button
    android:id="@+id/tiaoguoid"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_gravity="right|top"
    android:layout_marginRight="50dp"
    android:layout_marginTop="50dp"
    android:background="#ffaaaa"
    android:text="跳過"
    android:visibility="gone" />

YingDaYeActivity.java package com.example.yingdaoye;

import java.util.ArrayList; import java.util.List;

import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout.LayoutParams;

public class YingDaYeActivity extends Activity { private List listarray; // 資料來源就要顯示的圖片 private ViewPager viewpager; private LinearLayout dianjihe; private PagerAdapers adapers; // 自定義介面卡

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yingdaye);
    indateorview(); // 資料,介面初始化
    setviewpager(); // 設定引導實現

}

private int pingkuang(){   //測量當前螢幕的寬度(畫素px)
    DisplayMetrics metric = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metric);
    int width = metric.widthPixels;  // 螢幕寬度(畫素)
    return width;  
}

private void indateorview() {

    listarray = new ArrayList<Integer>();
    listarray.add(R.drawable.tu1); // 新增圖片   tu1——4根據你自己的圖片
    listarray.add(R.drawable.tu2);
    listarray.add(R.drawable.tu3);
    listarray.add(R.drawable.tu4); // 總共四張

    viewpager = (ViewPager) findViewById(R.id.Viewpagerid); // 關聯ViewPager引導元件
    dianjihe = (LinearLayout) findViewById(R.id.dianjiheid); // 關聯LinearLayout元件,LinearLayout包含四個滑動點

    for (int i = 0; i < dianjihe.getChildCount(); i++) { // getChildCount計算LinearLayout元件裡面包含幾個元件(滑動點)
        ImageView imageviews = (ImageView) dianjihe.getChildAt(i); // 獲取指定滑動點

        if (i == 0) { // 介面開啟第一個為紅,後三個位黃
            imageviews.setBackgroundColor(Color.RED);

        } else {
            imageviews.setBackgroundColor(Color.YELLOW);
        }

    }
    //下面設定藍動點初始化左邊距
    ImageView ima = (ImageView) findViewById(R.id.imageviewid6);
    LayoutParams redparams = (LayoutParams) ima.getLayoutParams();
    int len =(int)(pingkuang()/2.0f-70);
    redparams.leftMargin = len;
    ima.setLayoutParams(redparams); //
}

private void setviewpager() {
    adapers = new PagerAdapers(this, listarray); // 初始化自定義PagerAdapter介面卡
    viewpager.setAdapter(adapers); // ViewPager引導元件設定介面卡

    viewpager.setOnPageChangeListener(new OnPageChangeListener() { // ViewPager引導元件設定滑動監聽

        // 實現滑動監聽需要實現下面三個方法,方法具體作用自己查吧!
        @Override
        public void onPageSelected(int arg0) {  //紅色滑動點會根據螢幕滑動一頁後,對應跳動滑動點
            // TODO Auto-generated method stub
            for (int i = 0; i < dianjihe.getChildCount(); i++) {
                ImageView imageview = (ImageView) dianjihe.getChildAt(i);
                if (i == arg0) {
                    imageview.setBackgroundColor(Color.RED);
                } else {
                    imageview.setBackgroundColor(Color.YELLOW);

                }

            }
        }
          //(pingkuang()/2.0f-70)等於第一個滑動點的左邊距
         //40 * positionOffset + position * 40 等於已經滑動過的頁數加當前頁滑動的百分比距離
        @Override
        public void onPageScrolled(int position, float positionOffset, int arg2) {  //藍色滑動點會根據螢幕滑動多少,對應動態滑動
            ImageView ima = (ImageView) findViewById(R.id.imageviewid6);
            LayoutParams redparams = (LayoutParams) ima.getLayoutParams();
            int len = (int) (40 * positionOffset + position * 40 + (pingkuang()/2.0f-70));
            redparams.leftMargin = len;
            ima.setLayoutParams(redparams);

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }
    });

}

}

PagerAdapers.java package com.example.yingdaoye;

import java.util.List;

import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout;

/** * 自定義介面卡 */

class PagerAdapers extends PagerAdapter { private LayoutInflater mInflater; private List mlistarray;

/**
 * 建構函式
 * 
 * @param mContext
 *            上下文
 * @param mList
 *            資料來源
 */

PagerAdapers(Context mContext, List<Integer> mList) {
    this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mlistarray = mList;
}

/**
 * 資料來源長度
 */
@Override
public int getCount() {
    return mlistarray.size();
}

/**
 * 銷燬檢視
 */
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

/**
 * 自定義檢視(這個是關鍵 介面卡根據ViewPager的操作,把instantiateItem()返回的view顯示ViewPager上)
 * View是用另一張佈局
 */
@Override
public Object instantiateItem(ViewGroup container, int position) {
    View view = (View) mInflater.inflate(R.layout.yingdayetupiang, container, false);

    LinearLayout lineas = (LinearLayout) view.findViewById(R.id.yingdandyetupiang);

    lineas.setBackgroundResource(mlistarray.get(position));

    if(position==3){    //最後一張時,設定按鈕為可見
        Button button = (Button)view.findViewById(R.id.tiaoguoid);
        button.setVisibility(View.VISIBLE);  
    }


    ((ViewPager) container).addView(view, 0);

    return view;
}

/**
 * 判斷兩個物件是否相同
 */
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0.equals((View) arg1);
}

}

相關文章