引导页viewpager中的自绘小圆点

今天分享下如何用android中的画笔自绘小园点,采用自绘的小圆点,可以节约内存,还移植方便,效率高。
首先我们先派生一个自定义的view,这个派生的view继承viewpager。
1.布局文件如下
相对布局

<com.example.administrator.myapplication00.PointView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/vp_splash">
</com.example.administrator.myapplication00.PointView>

2.派生view的java代码,在派生的view中用画笔画小圆点,代码如下:

public class PointView extends ViewPager {
Context context;
Paint paint;

public PointView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    paint = new Paint();

}

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    drawCycle(canvas);
}

//画笔画小圆点
private void drawCycle(Canvas canvas) {
canvas.save();
canvas.translate(getScrollX(), getScrollY());
int count = 0;
if (this.getAdapter() != null) {
count = this.getAdapter().getCount();
}
int select = getCurrentItem();
float density = getContext().getResources().getDisplayMetrics().density;
int itemWidth = (int) (11 * density);
int itemHeight = itemWidth / 2;
int x = (getWidth() - count * itemWidth)/2;
int y = getHeight() - itemWidth;
int minItemHeight = (int) ((float) itemHeight * 0.8F);
paint.setAntiAlias(true);

    paint.setStyle(Paint.Style.FILL);
    for (int i = 0; i < count; i++) {
        if (select == i) {
            paint.setColor(0xFFbdbdbd);
            canvas.drawCircle(x + itemWidth * i + itemWidth / 2, y, minItemHeight, paint);
        } else {
            paint.setColor(0xFFe6e6e6);
            canvas.drawCircle(x + itemWidth * i + itemWidth / 2, y, minItemHeight, paint);
        }
    }
    canvas.restore();
}

}

3.引导页的java代码

package com.example.administrator.myapplication00;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

public class SplashActivity extends Activity {
private ViewPager mPager;
private ArrayList mImageViewList;
private ViewPagerAdapter mAdapter;
private GestureDetector mGestureDetector; // 用户滑动
private int mPreviousPosition = 0;//图片的索引
private int mFlaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    mGestureDetector = new GestureDetector(new GuideViewTouch());
    // 获取分辨率根据滑动屏幕的距离来来判断
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    mFlaggingWidth = dm.widthPixels / 3;

    init();

}

private void init() {
    mPager = (ViewPager) findViewById(R.id.vp_splash);


    //图片资源
    int[] imageResourseIDs = new int[]{R.drawable.splash1, R.drawable.splash2, R.drawable.splash3};
    mImageViewList = new ArrayList<ImageView>();
    ImageView iv;

    for (int i = 0; i < imageResourseIDs.length; i++) {
        iv = new ImageView(this);
        iv.setBackgroundResource(imageResourseIDs[i]);
        mImageViewList.add(iv);

    }

    mAdapter = new ViewPagerAdapter();
    mPager.setAdapter(mAdapter);
    mPager.setCurrentItem(mPreviousPosition);



    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        public void onPageSelected(int position) {

            mPreviousPosition = position % mImageViewList.size();

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        public void onPageScrollStateChanged(int arg0) {

        }
    });
}





/**
 * 滑动的监听
 * @param event
 * @return
 */
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (mGestureDetector.onTouchEvent(event)) {
        event.setAction(MotionEvent.ACTION_CANCEL);
    }
    return super.dispatchTouchEvent(event);
}

private class GuideViewTouch extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        if (mPreviousPosition == mImageViewList.size() - 1) {
            if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()
                    - e2.getY())
                    && (e1.getX() - e2.getX() <= (-mFlaggingWidth) || e1
                    .getX() - e2.getX() >= mFlaggingWidth)) {
                if (e1.getX() - e2.getX() >= mFlaggingWidth) {
                    GoToMainActivity();
                    return true;
                }
            }
        }
        return false;
    }
}

/**
 * 跳转到主页
 */
private void GoToMainActivity() {
    Intent i = new Intent(SplashActivity.this, MainActivity.class);
    startActivity(i);
    finish();
}

//适配器
protected class ViewPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return mImageViewList.size();
    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mImageViewList.get(position));
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mImageViewList.get(position));
        return mImageViewList.get(position);
    }

}
}

你可能感兴趣的:(android,viewpager,自绘小圆点,省内存)