提交 5d52cd66 编写于 作者: 门心叼龙's avatar 门心叼龙

code perfect

上级 4e5ecbf7
...@@ -3,15 +3,22 @@ package com.mxdl.customview.test; ...@@ -3,15 +3,22 @@ package com.mxdl.customview.test;
import android.graphics.Color; import android.graphics.Color;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.mxdl.customview.R; import com.mxdl.customview.R;
import com.mxdl.customview.test.view.MyTextView;
import com.mxdl.customview.test.view.MyViewPager; import com.mxdl.customview.test.view.MyViewPager;
import com.mxdl.customview.test.view.MyViewPager1;
import com.mxdl.customview.util.MotionEventUtil;
public class ViewPagerTestActivity extends AppCompatActivity { public class ViewPagerTestActivity extends AppCompatActivity {
private MyViewPager mViewPager; private MyViewPager1 mViewPager;
int[] colors = {Color.GREEN,Color.RED,Color.BLUE}; int[] colors = {Color.GREEN,Color.RED,Color.BLUE};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -19,10 +26,27 @@ public class ViewPagerTestActivity extends AppCompatActivity { ...@@ -19,10 +26,27 @@ public class ViewPagerTestActivity extends AppCompatActivity {
setContentView(R.layout.activity_view_pager_test); setContentView(R.layout.activity_view_pager_test);
mViewPager = findViewById(R.id.view_my_pager); mViewPager = findViewById(R.id.view_my_pager);
for(int i =0; i < 3; i++){ for(int i =0; i < 3; i++){
TextView txtContent = (TextView) LayoutInflater.from(this).inflate(R.layout.item_test_view_pager, mViewPager,false); LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_test_view_pager, null);
int widthPixels = getResources().getDisplayMetrics().widthPixels;
int heightPixels = getResources().getDisplayMetrics().heightPixels;
Log.v("MYTAG1","widthPixels:"+widthPixels+";height:"+heightPixels);
layout.setLayoutParams(new LinearLayout.LayoutParams(widthPixels, heightPixels));
TextView txtContent = layout.findViewById(R.id.txt_content);
txtContent.setText(String.valueOf(i)); txtContent.setText(String.valueOf(i));
txtContent.setBackgroundColor(colors[i]); txtContent.setBackgroundColor(colors[i]);
mViewPager.addView(txtContent); mViewPager.addView(layout);
} }
} }
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.v("MYTAG","Activity dispatchTouchEvent start..."+ MotionEventUtil.getEventType(ev));
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v("MYTAG","Activity onTouchEvent start..."+ MotionEventUtil.getEventType(event));
return super.onTouchEvent(event);
}
} }
package com.mxdl.customview.test.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
import com.mxdl.customview.util.MotionEventUtil;
/**
* Description: <MyButton><br>
* Author: mxdl<br>
* Date: 2019/10/25<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class MyButton extends android.support.v7.widget.AppCompatButton {
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.v("MYTAG","MyButton dispatchTouchEvent start..."+ MotionEventUtil.getEventType(event));
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v("MYTAG","MyButton onTouchEvent start..."+ MotionEventUtil.getEventType(event));
return super.onTouchEvent(event);
}
}
package com.mxdl.customview.test.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
import com.mxdl.customview.util.MotionEventUtil;
/**
* Description: <MyTextView><br>
* Author: mxdl<br>
* Date: 2019/10/25<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class MyTextView extends android.support.v7.widget.AppCompatTextView {
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.v("MYTAG","TextView dispatchTouchEvent start..."+ MotionEventUtil.getEventType(event));
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v("MYTAG","TextView onTouchEvent start..."+ MotionEventUtil.getEventType(event));
return super.onTouchEvent(event);
}
}
...@@ -11,6 +11,8 @@ import android.view.View; ...@@ -11,6 +11,8 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Scroller; import android.widget.Scroller;
import com.mxdl.customview.util.MotionEventUtil;
/** /**
* Description: <MyViewPager><br> * Description: <MyViewPager><br>
* Author: mxdl<br> * Author: mxdl<br>
...@@ -25,6 +27,8 @@ public class MyViewPager extends ViewGroup { ...@@ -25,6 +27,8 @@ public class MyViewPager extends ViewGroup {
private int mChildIndex; private int mChildIndex;
private Scroller mScroller; private Scroller mScroller;
private VelocityTracker mVelocityTracker; private VelocityTracker mVelocityTracker;
private int mLastXIntercept;
private int mLastYIntercept;
public MyViewPager(Context context) { public MyViewPager(Context context) {
super(context); super(context);
...@@ -54,11 +58,41 @@ public class MyViewPager extends ViewGroup { ...@@ -54,11 +58,41 @@ public class MyViewPager extends ViewGroup {
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.v("MYTAG", "ViewPager onMeasure start...");
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Log.v("MYTAG","onMeasure width:"+widthSpecSize+";height:"+heightSpecSize);
measureChildren(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec, heightMeasureSpec);
int measureWidth = 0;
int measureHeight = 0;
int childCount = getChildCount();
if(childCount == 0){
setMeasuredDimension(0,0);
}else if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
View child = getChildAt(0);
measureWidth = child.getMeasuredWidth() * childCount;
measureHeight = child.getMeasuredHeight();
//setMeasuredDimension(widthSpecSize,heightSpecSize);
setMeasuredDimension(measureWidth,measureHeight);
}else if(widthSpecMode == MeasureSpec.AT_MOST){
View child = getChildAt(0);
measureWidth = child.getMeasuredWidth() * childCount;
measureHeight = child.getMeasuredHeight();
setMeasuredDimension(measureWidth,heightSpecSize);
}else if(heightMeasureSpec == MeasureSpec.AT_MOST){
View child = getChildAt(0);
measureHeight = child.getMeasuredHeight();
setMeasuredDimension(widthSpecSize,measureHeight);
}
} }
@Override @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) { protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.v("MYTAG", "ViewPager onLayout start...");
int childCount = getChildCount(); int childCount = getChildCount();
int childLeft = 0; int childLeft = 0;
for (int i = 0; i < childCount; i++) { for (int i = 0; i < childCount; i++) {
...@@ -67,30 +101,72 @@ public class MyViewPager extends ViewGroup { ...@@ -67,30 +101,72 @@ public class MyViewPager extends ViewGroup {
int measuredHeight = child.getMeasuredHeight(); int measuredHeight = child.getMeasuredHeight();
child.layout(childLeft, 0, childLeft + measuredWidth, measuredHeight); child.layout(childLeft, 0, childLeft + measuredWidth, measuredHeight);
Log.v(TAG, "view count:" + childCount + "|childLeft:" + childLeft + ";view width:" + measuredWidth + ";height:" + measuredHeight + "|scrollX:" + child.getScrollX() + ";scrollY:" + child.getScrollY() + "|x:" + child.getX() + ";y:" + child.getY() + "|" + ";left:" + child.getLeft() + ";top" + child.getTop() + ";right:" + child.getRight() + ";bottom:" + child.getBottom() + "|transtionX:" + child.getTranslationX() + ";transtionY:" + child.getTranslationY()); //Log.v(TAG, "view count:" + childCount + "|childLeft:" + childLeft + ";view width:" + measuredWidth + ";height:" + measuredHeight + "|scrollX:" + child.getScrollX() + ";scrollY:" + child.getScrollY() + "|x:" + child.getX() + ";y:" + child.getY() + "|" + ";left:" + child.getLeft() + ";top" + child.getTop() + ";right:" + child.getRight() + ";bottom:" + child.getBottom() + "|transtionX:" + child.getTranslationX() + ";transtionY:" + child.getTranslationY());
childLeft += measuredWidth; childLeft += measuredWidth;
} }
Log.v(TAG, "view pager width:" + getMeasuredWidth() + ";height:" + getMeasuredHeight()); //Log.v(TAG, "view pager width:" + getMeasuredWidth() + ";height:" + getMeasuredHeight());
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.v("MYTAG", "ViewPager dispatchTouchEvent start..." + MotionEventUtil.getEventType(ev));
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("MYTAG", "ViewPager onInterceptTouchEvent start..." + MotionEventUtil.getEventType(ev));
boolean intercept = false;
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
intercept = false;
if(!mScroller.isFinished()){
mScroller.abortAnimation();
intercept = true;
}
break;
case MotionEvent.ACTION_MOVE:
int dx = x - mLastXIntercept;
int dy = y - mLastYIntercept;
if(Math.abs(dx) > Math.abs(dy)){
intercept = true;
}else{
intercept = false;
}
break;
case MotionEvent.ACTION_UP:
intercept = false;
break;
}
mLastX = x;
mLastY = y;
mLastXIntercept = x;
mLastYIntercept = y;
return intercept;
} }
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
Log.v("MYTAG", "ViewPager onTouchEvent start..." + MotionEventUtil.getEventType(event));
Log.v("MYTAG","onTouchEvent width:"+getWidth()+";height:"+getHeight());
//Log.v("MYTAG", "ViewPager width:" +getWidth()+";height:"+getHeight()+";measureWidth:"+getMeasuredWidth()+";measureHeigh:"+getMeasuredHeight());
mVelocityTracker.addMovement(event); mVelocityTracker.addMovement(event);
boolean consume = false;
int x = (int) event.getX(); int x = (int) event.getX();
int y = (int) event.getY(); int y = (int) event.getY();
Log.v(TAG, "onTouchEvent x:" + x + ";y:" + y); //Log.v(TAG, "onTouchEvent x:" + x + ";y:" + y);
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
consume = true; if(!mScroller.isFinished()){
mScroller.abortAnimation();
}
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
int dx = x - mLastX; int dx = x - mLastX;
int dy = y - mLastY; int dy = y - mLastY;
if (Math.abs(dx) > Math.abs(dy)) { if(Math.abs(dx) > Math.abs(dy)){
scrollBy(-dx, 0); scrollBy(-dx, 0);
} else {
consume = false;
} }
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
...@@ -105,24 +181,24 @@ public class MyViewPager extends ViewGroup { ...@@ -105,24 +181,24 @@ public class MyViewPager extends ViewGroup {
View child = getChildAt(mChildIndex); View child = getChildAt(mChildIndex);
int childWidth = child.getMeasuredWidth(); int childWidth = child.getMeasuredWidth();
if(Math.abs(xVelocity) > 50){ if (Math.abs(xVelocity) > 50) {
mChildIndex = xVelocity > 0 ? mChildIndex - 1:mChildIndex + 1; mChildIndex = xVelocity > 0 ? mChildIndex - 1 : mChildIndex + 1;
}else{ } else {
mChildIndex = (scrollX + childWidth / 2) / childWidth; mChildIndex = (scrollX + childWidth / 2) / childWidth;
} }
mChildIndex = Math.max(0, Math.min(mChildIndex, getChildCount() - 1)); mChildIndex = Math.max(0, Math.min(mChildIndex, getChildCount() - 1));
int delx = mChildIndex * childWidth - scrollX; int delx = mChildIndex * childWidth - scrollX;
smoothScrollTo(delx,0); smoothScrollTo(delx, 0);
mVelocityTracker.clear(); mVelocityTracker.clear();
break; break;
} }
mLastX = x; mLastX = x;
mLastY = y; mLastY = y;
return consume; return true;
} }
private void smoothScrollTo(int x,int y) { private void smoothScrollTo(int x, int y) {
mScroller.startScroll(getScrollX(), getScrollY(), x, y, 500); mScroller.startScroll(getScrollX(), getScrollY(), x, y, 500);
invalidate(); invalidate();
} }
......
package com.mxdl.customview.test.view;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
/**
* Description: <MyViewPager1><br>
* Author: mxdl<br>
* Date: 2019/10/25<br>
* Version: V1.0.0<br>
* Update: <br>
* 1.计算控件的大小
* 2.确定子控件的位置
* 3.对左滑右滑的事件进行拦截
* 4.实现拖动效果
* 5.实现翻页效果
*/
public class MyViewPager1 extends ViewGroup {
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mLastX;
private int mLastY;
private int mLastXIntercept;
private int mLastYIntercept;
private int mChildIndex;
public MyViewPager1(Context context) {
super(context);
init();
}
public MyViewPager1(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyViewPager1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyViewPager1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public void init() {
mScroller = new Scroller(getContext());
mVelocityTracker = VelocityTracker.obtain();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measureWidth = 0;
int measureHeight = 0;
int childCount = getChildCount();
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
if (childCount == 0) {
setMeasuredDimension(0, 0);
} else if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
View child = getChildAt(0);
measureWidth = child.getMeasuredWidth() * childCount;
measureHeight = child.getMeasuredHeight();
setMeasuredDimension(measureWidth, measureHeight);
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
View child = getChildAt(0);
measureWidth = child.getMeasuredWidth() * childCount;
setMeasuredDimension(measureWidth, heightSpecSize);
} else if (heightSpecMode == MeasureSpec.AT_MOST) {
View child = getChildAt(0);
measureHeight = child.getMeasuredHeight();
setMeasuredDimension(widthSpecSize, measureHeight);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int childLeft = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int measuredWidth = child.getMeasuredWidth();
int measuredHeight = child.getMeasuredHeight();
child.layout(childLeft, 0, childLeft + measuredWidth, measuredHeight);
childLeft += measuredWidth;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercept = false;
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
intercept = false;
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
intercept = true;
}
break;
case MotionEvent.ACTION_MOVE:
int dx = x - mLastXIntercept;
int dy = y - mLastYIntercept;
intercept = false;
if (Math.abs(dx) > Math.abs(dy)) {
intercept = true;
}
break;
case MotionEvent.ACTION_UP:
intercept = false;
break;
}
mLastX = x;
mLastY = y;
mLastXIntercept = x;
mLastYIntercept = y;
return intercept;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mVelocityTracker.addMovement(event);
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
break;
case MotionEvent.ACTION_MOVE:
int dx = x - mLastX;
scrollBy(-dx, 0);
break;
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = mVelocityTracker.getXVelocity();
View child = getChildAt(0);
int measuredWidth = child.getMeasuredWidth();
if (Math.abs(xVelocity) > 50) {
mChildIndex = xVelocity > 0 ? mChildIndex - 1 : mChildIndex + 1;
} else {
mChildIndex = (scrollX + measuredWidth/2)/measuredWidth;
}
mChildIndex = Math.max(0,Math.min(mChildIndex,getChildCount() -1));
int deltaX = mChildIndex * measuredWidth - scrollX;
smoothScrollTo(deltaX);
mVelocityTracker.clear();
break;
}
mLastX = x;
mLastY = y;
return true;
}
private void smoothScrollTo(int deltaX) {
mScroller.startScroll(getScrollX(),getScrollY(),deltaX,0,500);
invalidate();
}
@Override
public void computeScroll() {
if(mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
}
}
}
package com.mxdl.customview.util;
import android.view.MotionEvent;
/**
* Description: <MotionEventUtil><br>
* Author: mxdl<br>
* Date: 2019/10/25<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class MotionEventUtil {
public static String getEventType(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
return "action_down";
case MotionEvent.ACTION_MOVE:
return "action_move";
case MotionEvent.ACTION_UP:
return "action_down";
}
return null;
}
}
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<com.mxdl.customview.test.view.MyViewPager <com.mxdl.customview.test.view.MyViewPager1
android:id="@+id/view_my_pager" android:id="@+id/view_my_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView <LinearLayout android:layout_width="match_parent"
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" /> android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.mxdl.customview.test.view.MyTextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="300dp"
xmlns:android="http://schemas.android.com/apk/res/android" />
<com.mxdl.customview.test.view.MyButton
android:layout_width="match_parent"
android:layout_height="300dp"/>
</LinearLayout>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册