欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义ViewPager实例

程序员文章站 2024-02-23 23:13:08
本文实例讲述了android自定义viewpager的方法。分享给大家供大家参考,具体如下: package com.rong.activity; import...

本文实例讲述了android自定义viewpager的方法。分享给大家供大家参考,具体如下:

package com.rong.activity;
import android.content.context;
import android.graphics.color;
import android.util.attributeset;
import android.view.motionevent;
import android.view.velocitytracker;
import android.view.view;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.scroller;
public class customviewpager extends viewgroup {
  private context mcontext;
  private int screenwidth;
  private int screenhight;
  private int lastmovex = 0;
  private velocitytracker velocitytracker;
  private int max_velocity=600;
  private int curscreen=0;
  private scroller scroller;
  public customviewpager(context context) {
    super(context);
    mcontext = context;
    initview();
  }
  public customviewpager(context context, attributeset attrs) {
    super(context, attrs);
    mcontext = context;
    initview();
  }
  public customviewpager(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
    mcontext = context;
    initview();
  }
  @override
  public boolean ontouchevent(motionevent event) {
    if (velocitytracker == null) {
      velocitytracker = velocitytracker.obtain();
    }
    velocitytracker.addmovement(event);
    int x = (int) event.getx();
    switch (event.getaction()) {
    case motionevent.action_down:
      lastmovex = x;
      break;
    case motionevent.action_move:
      int dis = lastmovex - x;
      scrollby(dis, 0);
      lastmovex = x;
      break;
    case motionevent.action_up:
      velocitytracker.computecurrentvelocity(1000);
      int velocityx=(int)velocitytracker.getxvelocity();
      if(velocityx>max_velocity&&curscreen>0){
        jump2screen(curscreen-1);
      }else if(velocityx<-max_velocity&&curscreen<getchildcount()-1){
        jump2screen(curscreen+1);
      }else{
        int screen=(getscrollx()+screenwidth/2)/screenwidth;
        jump2screen(screen);
      }
      if (velocitytracker != null) {
        velocitytracker.recycle();
        velocitytracker = null;
      }
      break;
    }
    return true;
  }
  /**
   * 跳转到指定screen
   * @param screen
   */
  public void jump2screen(int screen){
    curscreen=screen;
    if(curscreen>getchildcount()-1){
      curscreen=getchildcount()-1;
    }
    int dis=curscreen*screenwidth-getscrollx();
    scroller.startscroll(getscrollx(), 0, dis, 0);
    invalidate();
  }
  @override
  public void computescroll() {
    if(scroller.computescrolloffset()){
      scrollto(scroller.getcurrx(), 0);
      postinvalidate();
    }
  }
  public void initview() {
    scroller=new scroller(mcontext);
    linearlayout layout1 = new linearlayout(getcontext());
    layout1.setbackgroundcolor(color.red);
    addview(layout1);
    linearlayout layout2 = new linearlayout(getcontext());
    layout2.setbackgroundcolor(color.green);
    addview(layout2);
    linearlayout layout3 = new linearlayout(getcontext());
    layout3.setbackgroundcolor(color.blue);
    addview(layout3);
  }
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure(widthmeasurespec, heightmeasurespec);
    screenwidth = measurespec.getsize(widthmeasurespec);
    screenhight = measurespec.getsize(heightmeasurespec);
    setmeasureddimension(screenwidth, screenhight);
    for (int i = 0; i < getchildcount(); i++) {
      view view = getchildat(i);
      view.measure(screenwidth, screenhight);
    }
  }
  @override
  protected void onlayout(boolean changed, int l, int t, int r, int b) {
    int leftwidth = 0;
    for (int i = 0; i < getchildcount(); i++) {
      view view = getchildat(i);
      view.layout(leftwidth, 0, leftwidth + screenwidth, screenhight);
      leftwidth = leftwidth + screenwidth;
    }
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。