你的位置:首页 > 操作系统

[操作系统]安卓图片滑动,实现带小点的导航页面效果


今天给大家说说安卓中类似这样的引导页面怎么实现,我自己简单的添加了个跳过按钮方便跳转到主界面,图片跟小点图片资源大家自己去网上找

下面这个是我定义的一个activity_guide.

<?<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v4.view.ViewPager>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
></LinearLayout>
<TextView
android:id="@+id/guideBotton"
android:text="跳过>>>"
android:textColor="#D9D919"
android:textSize="17dp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text
android:layout_marginBottom="96dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

</FrameLayout>




下面呢,是我的java代码Guide.java



package net.oschina.app;
//底部小点变化引导页面
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.ui.MainActivity;

public class Guide extends Activity implements OnPageChangeListener,View.OnTouchListener
{
private ViewPager mViewPager = null;
private LinearLayout mViewGroup = null;
private TextView textView;
private int[] mImageIds = {R.drawable.guide1, R.drawable.guide2,
R.drawable.guide3,R.drawable.guide4,
};

private ImageView[] mImageViews = null;

private ImageView[] mTips = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
textView=(TextView)findViewById(R.id.guideBotton);
Buttonlistenner buttonlistenner=new Buttonlistenner();
textView.setOnClickListener(buttonlistenner);
textView.setClickable(true);
mViewGroup = (LinearLayout) findViewById(R.id.viewGroup);
mViewPager = (ViewPager) findViewById(R.id.viewPager);

mTips = new ImageView[mImageIds.length];
for(int i = 0; i < mTips.length; i++)
{
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LayoutParams(40,40));
mTips[i] = iv;

if(i == 0)
{
iv.setBackgroundResource(R.drawable.white_dot);
}else
{
iv.setBackgroundResource(R.drawable.dark_dot);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
lp.leftMargin = 40;
lp.rightMargin = 40;
mViewGroup.addView(iv,lp);
}
mImageViews = new ImageView[mImageIds.length];
for(int i = 0; i < mImageViews.length; i++)
{
ImageView iv = new ImageView(this);
mImageViews[i] = iv;
int reqWidth = getWindowManager().getDefaultDisplay().getWidth();
int reqHeight = getWindowManager().getDefaultDisplay().getHeight();
iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mImageIds[i], reqWidth, reqHeight));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}

mViewPager.setAdapter(new MyPagerAdapter());
mViewPager.setOnPageChangeListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}

class MyPagerAdapter extends PagerAdapter
{
@Override
public int getCount()
{
return mImageIds.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
try
{
container.addView(mImageViews[position]);
} catch (Exception e)
{
}
return mImageViews[position];
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
}
}
@Override
public void onPageSelected(int arg0)
{
for(int i = 0; i < mTips.length; i++)
{
if(arg0 == i)
{
mTips[i].setBackgroundResource(R.drawable.white_dot);
}else
{
mTips[i].setBackgroundResource(R.drawable.dark_dot);
}
}
}
@Override
public void onPageScrollStateChanged(int arg0)
{

}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
mViewPager.getParent().requestDisallowInterceptTouchEvent(true);
}

private static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight)
{
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId);
int inSampleSize = cacluateInSampledSize(opts, reqWidth, reqHeight);
opts.inSampleSize = inSampleSize;
opts.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res,resId,opts);

}

private static int cacluateInSampledSize(BitmapFactory.Options opts,int width,int height)
{
if(opts == null)
{
return 1;
}
int inSampleSize = 1;
int realWidth = opts.outWidth;
int realHeight = opts.outHeight;

if(realWidth > width || realHeight > height)
{
int heightRatio = realHeight/height;
int widthRatio = realWidth/width;

inSampleSize = (widthRatio > heightRatio) ? heightRatio : widthRatio;
}
return inSampleSize;
}
class Buttonlistenner implements View.OnClickListener {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(Guide.this, MainActivity.class);
Guide.this.startActivity(intent);
Guide.this.finish();
;
}
}}

这里要提醒下,在intent跳转到下一个页面的时候,就是上方的
Guide.this.startActivity(intent);
Guide.this.finish();
一定要记得要finish,否则再加入多个页面跳转后,会导致内存溢出,这点很关键