Android 引導頁的代碼
布局代碼
<android.support.v4.view.ViewPager android:id="@+id/viewpage" android:layout_width="match_parent" android:layout_height="match_parent" /> <!--android:background="@drawable/btnselector"--> <Button android:id="@+id/btnty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="100dp" android:background="@drawable/btnselector" android:paddingLeft="30dp" android:paddingRight="30dp" android:visibility="gone" android:text="立即體驗" android:textColor="@drawable/textselector" android:textSize="20sp" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="80dp"> <LinearLayout android:id="@+id/linearpointgroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> <ImageView android:id="@+id/ivredpoint" android:background="@drawable/pointred" android:layout_width="10dp" android:layout_height="10dp" /> </RelativeLayout>
後臺代碼
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class GuideActivity extends Activity {
List<ImageView> imageViews; ViewPager vpage; LinearLayout linearLayout; ImageView imagepoint; int leftmax = 0; Button btnty;
int pointwidth=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
vpage = (ViewPager) findViewById(R.id.viewpage);
linearLayout = (LinearLayout) findViewById(R.id.linearpointgroup);
imagepoint = (ImageView) findViewById(R.id.ivredpoint);
//10為 默認點的 布局寬度
pointwidth=DensityUtil.diptopx(GuideActivity.this,10);
btnty = (Button) findViewById(R.id.btnty);
int[] intimage = new int[]{R.drawable.guide1, R.drawable.guide2, R.drawable.guide3};
imageViews = new ArrayList<>();
for (int ii = 0; ii < intimage.length; ii++) {
ImageView imageView = new ImageView(GuideActivity.this);
imageView.setBackgroundResource(intimage[ii]);
imageViews.add(imageView);
//創建點
ImageView point = new ImageView(GuideActivity.this);
point.setBackgroundResource(R.drawable.point);
//此處單位是像素 需要做大小手機的適配 調用轉換成像素的方法
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pointwidth, pointwidth);
if (ii != 0) {
params.leftMargin = pointwidth;
}
point.setLayoutParams(params);
linearLayout.addView(point);
}
vpage.setAdapter(new pagetadapter());
Button btn = (Button) findViewById(R.id.btnty);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//設置緩存 進入過主界面
CacheParam.setkey(GuideActivity.this, "EnterMain", true);
Intent intent = new Intent(GuideActivity.this, LogInActivity.class);
startActivity(intent);
finish();
}
});
imagepoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//執行一次即可 所以 先移除
imagepoint.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//間距 = 第一個點距離左邊的距離-第0個點距離左邊的距離
leftmax = linearLayout.getChildAt(1).getLeft() - linearLayout.getChildAt(0).getLeft();
}
});
vpage.setOnPageChangeListener(new MyPageChange());
}
class MyPageChange implements ViewPager.OnPageChangeListener {
//當也沒滑動的時候調用
/*
* 當前滑動頁面的位置
* 頁面滑動的百分比
* 滑動的像素*/
@Override
public void onPageScrolled(int position, float positionoffset, int positionoffsetpixels) {
//兩點間滑動距離對應的坐標=原來的起始位置+兩點間移動的距離
int leftmargin = (int) (position * leftmax + (positionoffset * leftmax));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imagepoint.getLayoutParams();
params.leftMargin = leftmargin;
imagepoint.setLayoutParams(params);
}
/**
* 當頁面選中的時候調用這個方法
* 被選中頁面的位置
*/
@Override
public void onPageSelected(int position) {
//最後一個頁面
if (position == imageViews.size() - 1) {
btnty.setVisibility(View.VISIBLE);
} else//其他頁面
{
btnty.setVisibility(View.GONE);
}
}
/**
* 當頁面滑動狀態變化的時候
*/
@Override
public void onPageScrollStateChanged(int i) {
}
}
class pagetadapter extends PagerAdapter {
@Override
public int getCount() {
return imageViews.size();
}
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = imageViews.get(position);
container.addView(imageView);
return imageView;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
}
}
import android.content.Context;
public class DensityUtil {
/
根據手機的分辨率 從dip的單位 轉為px 像素/
public static int diptopx(Context context,float dpvalue)
{
final float scale=context.getResources().getDisplayMetrics().density;
return (int)(dpvaluescale+0.5f);
}
/
根據手機的分辨率 從px的單位 轉為dip
* */
public static int pxtodip(Context context,float pxvalue)
{
final float scale=context.getResources().getDisplayMetrics().density;
return (int)(pxvalue/scale+0.5f);
}
}
Android 引導頁的代碼