标签:selector obs rem ble tle start tpi ant bottom
布局代码
<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.Button;
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);
}
}
标签:selector obs rem ble tle start tpi ant bottom
原文地址:https://www.cnblogs.com/lzsin/p/10363548.html