前言
在Android开发中,系统控件已经远远不能满足我们日常需求,可以使用以下方式实现控件的自定义:
1.系统控件的重新组合
2.自定义类继承View
3.自定义类继承ViewGroup
效果演示:
源码:
MyViewPager:
public class MyViewPagerextends ViewGroup {
private MyScrollerscroller;
/**
* 手势识别器
* 1.定义出来
* 2.实例化-把想要的方法给重写
* 3.在onTouchEvent()把事件传递给手势识别器
*/
private GestureDetectordetector;
/**
* 当前页面的下标位置
*/
private int currentIndex;
private float startX;
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
//实例化
initView(context);
}
private void initView(final Context context) {
scroller=new MyScroller();
//2.实例化手势识别器
detector =new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
Toast.makeText(context, "长按", Toast.LENGTH_SHORT).show();
}
/**
* @param e1
* @param e2
* @param distanceX 在X轴滑动了的距离
* @param distanceY 在Y轴滑动了的距离
* @return
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
/**
*x:要在X轴移动的距离
*y:要在Y轴移动的距离
*/
scrollBy((int) distanceX, 0);
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(context, "双击", Toast.LENGTH_SHORT).show();
return super.onDoubleTap(e);
}
});
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i =0; i < getChildCount(); i++) {
View childView = getChildAt(i);
childView.layout(i * getWidth(), 0, (i +1) * getWidth(), getHeight());
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
//3.把事件传递给手势识别器
detector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//1.记录坐标
startX = event.getX();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
//2.来到新的坐标
float endX = event.getX();
//下标位置
int tempIndex =currentIndex;
if ((startX - endX) > getWidth() /2) {
//显示下一个页面
tempIndex++;
}else if ((endX -startX) > getWidth() /2) {
//显示上一个页面
tempIndex--;
}
//根据下标位置移动到指定的页面
scrollToPager(tempIndex);
break;
}
return true;
}
/**
* 屏蔽非法值,根据位置移动到指定页面
*
* @param tempIndex
*/
private void scrollToPager(int tempIndex) {
if (tempIndex <0) {
tempIndex =0;
}
if (tempIndex > getChildCount() -1) {
tempIndex = getChildCount() -1;
}
//当前页面的下标位置
currentIndex=tempIndex;
int distanceX=currentIndex*getWidth()-getScrollX();
//scrollTo(currentIndex*getWidth(),0);
scroller.startScroll(getScrollX(),getScrollY(), distanceX,0);
invalidate(); //onDraw() 和 computeScroll()都会执行
}
@Override
public void computeScroll() {
if(scroller.cuputeScrollOffset()){
float currX =scroller.getCurrX();
scrollTo((int) currX,0);
invalidate();
}
}
}
MyScroller:
public class MyScroller {
private float startX;
private float startY;
private int distanceX;
private int distanceY;
private long startTime;
private long totalTime =500;
private boolean isFinish;
private float currX;
public float getCurrX() {
return currX;
}
public void startScroll(float startX, float startY, int distanceX, int distanceY) {
this.startX=startX;
this.startY=startY;
this.distanceX=distanceX;
this.distanceY=distanceY;
this.startTime= SystemClock.uptimeMillis(); //系统开机时间
this.isFinish=false;
}
public boolean cuputeScrollOffset(){
if(isFinish){
return false;
}
long endTime = SystemClock.uptimeMillis();
//这一小段所花的时间
long passTime = endTime -startTime;
if(passTime
//还没有移动结束
//计算平均速度
// float voleCity = distanceX/totalTime;
//移动这个一小段对应的距离
float distanceSamllX = passTime*distanceX/totalTime;
currX =startX + distanceSamllX;
}else{
//移动结束
isFinish =true;
currX =startX +distanceX;
}
return true;
}
}
MainActivity:
public class MainActivityextends Activity {
private MyViewPagermyViewPager;
private int[]ids = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewPager = findViewById(R.id.my_viewpager);
//添加页面
for (int i =0; i
ImageView imageView =new ImageView(this);
imageView.setBackgroundResource(ids[i]);
// imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//添加到MyViewPager这个View中
myViewPager.addView(imageView);
}
}
}
XML文件:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/my_viewpager" />
初次写文章
有什么不足望指点,谢谢