提交 48643d2f 编写于 作者: 门心叼龙's avatar 门心叼龙

add commit

上级 fb712271
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".test.ScrollTestActivity"></activity> <activity android:name=".test.ViewPagerTestActivity"></activity>
<activity android:name=".test.MainTestActivity" />
<activity android:name=".test.ScrollTestActivity" />
<activity android:name=".test.SmoothScrollTestActivity" />
<activity android:name=".CaptureRectViewActivity" /> <activity android:name=".CaptureRectViewActivity" />
<activity android:name=".CaptureCricleViewActivity" /> <activity android:name=".CaptureCricleViewActivity" />
<activity android:name=".CaptureSquareViewActivity" /> <activity android:name=".CaptureSquareViewActivity" />
......
...@@ -3,10 +3,14 @@ package com.mxdl.customview; ...@@ -3,10 +3,14 @@ package com.mxdl.customview;
import android.content.Intent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import com.mxdl.customview.test.MainTestActivity;
import com.mxdl.customview.test.ScrollTestActivity; import com.mxdl.customview.test.ScrollTestActivity;
import com.mxdl.customview.test.SmoothScrollTestActivity;
/** /**
* Description: <MainActivity><br> * Description: <MainActivity><br>
...@@ -23,7 +27,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -23,7 +27,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private Button mBtnCaptrueView; private Button mBtnCaptrueView;
private Button mBtnCricleCapture; private Button mBtnCricleCapture;
private Button mBtnRectCapture; private Button mBtnRectCapture;
private Button mBtnScrollTo;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -35,7 +38,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -35,7 +38,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
mBtnCaptrueView = findViewById(R.id.btn_capture_square); mBtnCaptrueView = findViewById(R.id.btn_capture_square);
mBtnCricleCapture = findViewById(R.id.btn_capture_cricle); mBtnCricleCapture = findViewById(R.id.btn_capture_cricle);
mBtnRectCapture = findViewById(R.id.btn_capture_rect); mBtnRectCapture = findViewById(R.id.btn_capture_rect);
mBtnScrollTo = findViewById(R.id.btn_scroll_to);
mBtnStickyLayout.setOnClickListener(this); mBtnStickyLayout.setOnClickListener(this);
mBtnHorizontalScrollViewEx.setOnClickListener(this); mBtnHorizontalScrollViewEx.setOnClickListener(this);
...@@ -43,7 +45,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -43,7 +45,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
mBtnCaptrueView.setOnClickListener(this); mBtnCaptrueView.setOnClickListener(this);
mBtnCricleCapture.setOnClickListener(this); mBtnCricleCapture.setOnClickListener(this);
mBtnRectCapture.setOnClickListener(this); mBtnRectCapture.setOnClickListener(this);
mBtnScrollTo.setOnClickListener(this);
} }
@Override @Override
...@@ -67,10 +69,20 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -67,10 +69,20 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
case R.id.btn_capture_cricle: case R.id.btn_capture_cricle:
startActivity(new Intent(this, CaptureCricleViewActivity.class)); startActivity(new Intent(this, CaptureCricleViewActivity.class));
break; break;
case R.id.btn_scroll_to: }
startActivity(new Intent(this, ScrollTestActivity.class)); }
break;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_test,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.test){
startActivity(new Intent(this, MainTestActivity.class));
} }
return super.onOptionsItemSelected(item);
} }
} }
package com.mxdl.customview.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.mxdl.customview.R;
public class MainTestActivity extends AppCompatActivity implements View.OnClickListener{
private Button mBtnScrollSmooth;
private Button mBtnScrollTo;
private Button mBtnViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
mBtnScrollSmooth = findViewById(R.id.btn_scroll_smooth);
mBtnScrollTo = findViewById(R.id.btn_scroll_to);
mBtnViewPager = findViewById(R.id.btn_view_pager);
mBtnScrollSmooth.setOnClickListener(this);
mBtnScrollTo.setOnClickListener(this);
mBtnViewPager.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_scroll_to:
startActivity(new Intent(this, ScrollTestActivity.class));
break;
case R.id.btn_scroll_smooth:
startActivity(new Intent(this, SmoothScrollTestActivity.class));
break;
case R.id.btn_view_pager:
startActivity(new Intent(this, ViewPagerTestActivity.class));
break;
}
}
}
...@@ -2,31 +2,38 @@ package com.mxdl.customview.test; ...@@ -2,31 +2,38 @@ package com.mxdl.customview.test;
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.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import com.mxdl.customview.R; import com.mxdl.customview.R;
import com.mxdl.customview.test.view.TestTextView;
public class ScrollTestActivity extends AppCompatActivity { public class ScrollTestActivity extends AppCompatActivity {
public static final String TAG = ScrollTestActivity.class.getSimpleName();
private TextView mTxtScroll;
private Button mBtnScroll; private Button mBtnScroll;
private TestTextView mTxtContent; private Button mBtnShowPositon;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_test); setContentView(R.layout.activity_scroll_test);
mBtnScroll = findViewById(R.id.btn_scroll); mTxtScroll = findViewById(R.id.txt_scroll_to);
mTxtContent = findViewById(R.id.txt_scroll); mBtnScroll = findViewById(R.id.btn_scroll_smooth);
mBtnShowPositon = findViewById(R.id.btn_show_position);
mBtnScroll.setOnClickListener(new View.OnClickListener(){ mBtnScroll.setOnClickListener(new View.OnClickListener(){
@Override @Override
public void onClick(View v) { public void onClick(View v) {
float density = getResources().getDisplayMetrics().density; mTxtScroll.scrollBy(-200,-200);
//mTxtContent.scrollTo(-(int)(density * 150+0.5f),-(int)(density * 150+0.5f)); }
mTxtContent.scrollBy(-(int)(density * 150+0.5f),-(int)(density * 150+0.5f)); });
//mTxtContent.smoothScrollTo(-(int)(density * 150+0.5f),-(int)(density * 150+0.5f));
mBtnShowPositon.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.v(TAG,"scrollX:"+mTxtScroll.getScrollX()+";scrollY:"+mTxtScroll.getScrollY()+"|x:"+mTxtScroll.getX()+";y:"+mTxtScroll.getY()+"|"+";left:"+mTxtScroll.getLeft()+";top:"+mTxtScroll.getTop()+";right:"+mTxtScroll.getRight()+";bottom:"+mTxtScroll.getBottom()+"|transtionX:"+mTxtScroll.getTranslationX()+";transtionY:"+mTxtScroll.getTranslationY());
} }
}); });
} }
......
package com.mxdl.customview.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.mxdl.customview.R;
import com.mxdl.customview.test.view.TestTextView;
public class SmoothScrollTestActivity extends AppCompatActivity {
private Button mBtnScroll;
private TestTextView mTxtContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smooth_scroll);
mBtnScroll = findViewById(R.id.btn_scroll);
mTxtContent = findViewById(R.id.txt_scroll);
mBtnScroll.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mTxtContent.smoothScrollTo(-300,-300);
}
});
}
}
package com.mxdl.customview.test;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TextView;
import com.mxdl.customview.R;
import com.mxdl.customview.test.view.MyViewPager;
public class ViewPagerTestActivity extends AppCompatActivity {
private MyViewPager mViewPager;
int[] colors = {Color.BLACK,Color.GREEN,Color.BLUE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_test);
mViewPager = findViewById(R.id.view_my_pager);
for(int i =0; i < 3; i++){
TextView txtContent = (TextView) LayoutInflater.from(this).inflate(R.layout.item_test_view_pager, mViewPager,false);
txtContent.setText(String.valueOf(i));
txtContent.setBackgroundColor(colors[i]);
mViewPager.addView(txtContent);
}
}
}
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.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
/**
* Description: <MyViewPager><br>
* Author: mxdl<br>
* Date: 2019/10/23<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class MyViewPager extends ViewGroup {
public static final String TAG = MyViewPager.class.getSimpleName();
private int mLastX;
private int mLastY;
private int mChildIndex;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyViewPager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView();
}
private void initView() {
mScroller = new Scroller(getContext());
mVelocityTracker = VelocityTracker.obtain();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
@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);
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;
}
Log.v(TAG, "view pager width:" + getMeasuredWidth() + ";height:" + getMeasuredHeight());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mVelocityTracker.addMovement(event);
boolean consume = false;
int x = (int) event.getX();
int y = (int) event.getY();
Log.v(TAG, "onTouchEvent x:" + x + ";y:" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
consume = true;
break;
case MotionEvent.ACTION_MOVE:
int dx = x - mLastX;
int dy = y - mLastY;
if (Math.abs(dx) > Math.abs(dy)) {
scrollBy(-dx, 0);
} else {
consume = false;
}
break;
case MotionEvent.ACTION_UP:
//手指抬起的时候,首先要计算的是要滚动到哪个位置上,然后在计算滚动的距离是多少
//1.如果滑动没有过半儿,应该还在当前位置
//2.如果已经过半则滑动到下一个位置
//3.如果滑动的速度快也跳到下一个位置
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = mVelocityTracker.getXVelocity();
int scrollX = getScrollX();
View child = getChildAt(mChildIndex);
int childWidth = child.getMeasuredWidth();
if(Math.abs(xVelocity) > 50){
mChildIndex = xVelocity > 0 ? mChildIndex - 1:mChildIndex + 1;
}else{
mChildIndex = (scrollX + childWidth / 2) / childWidth;
}
mChildIndex = Math.max(0, Math.min(mChildIndex, getChildCount() - 1));
int delx = mChildIndex * childWidth - scrollX;
smoothScrollTo(delx,0);
mVelocityTracker.clear();
break;
}
mLastX = x;
mLastY = y;
return consume;
}
private void smoothScrollTo(int x,int y) {
mScroller.startScroll(getScrollX(), getScrollY(), x, y, 500);
invalidate();
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
}
package com.mxdl.customview.util;
import android.content.Context;
/**
* Description: <DisplayUtil><br>
* Author: mxdl<br>
* Date: 2019/10/23<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class DisplayUtil {
public static int dp2px(Context context,int dp){
float density = context.getResources().getDisplayMetrics().density;
return (int)(dp * density+0.5f);
}
}
...@@ -132,6 +132,7 @@ public class HorizontalScrollViewEx extends ViewGroup { ...@@ -132,6 +132,7 @@ public class HorizontalScrollViewEx extends ViewGroup {
intercept = true; intercept = true;
} else { } else {
intercept = false; intercept = false;
} }
Log.v(TAG,"dx:"+dx+";dy:"+dy+"intercept:"+intercept); Log.v(TAG,"dx:"+dx+";dy:"+dy+"intercept:"+intercept);
break; break;
......
...@@ -49,12 +49,5 @@ ...@@ -49,12 +49,5 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="CaptureCricleView" android:text="CaptureCricleView"
android:textAllCaps="false" /> android:textAllCaps="false" />
<Button
android:id="@+id/btn_scroll_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ScrollTo"
android:textAllCaps="false"
/>
</LinearLayout> </LinearLayout>
</android.support.v4.widget.NestedScrollView> </android.support.v4.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn_scroll_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SmoothTo"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_scroll_smooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SmoothScroll"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPagerTest"
android:textAllCaps="false"
/>
</LinearLayout>
\ No newline at end of file
...@@ -2,20 +2,28 @@ ...@@ -2,20 +2,28 @@
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android" >
<com.mxdl.customview.test.view.TestTextView <TextView
android:id="@+id/txt_scroll" android:id="@+id/txt_scroll_to"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="500dp" android:layout_height="match_parent"
android:text="hello world" android:text="Hello world"
android:layout_marginTop="100dp" android:gravity="center"
/> />
<Button <Button
android:id="@+id/btn_scroll" android:id="@+id/btn_scroll_smooth"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="ScrollTo"
android:textAllCaps="false"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:text="开始滚动"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_show_position"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看位置"
android:textAllCaps="false"
android:layout_above="@+id/btn_scroll_smooth"
/> />
</RelativeLayout> </RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.mxdl.customview.test.view.TestTextView
android:id="@+id/txt_scroll"
android:layout_width="match_parent"
android:layout_height="500dp"
android:text="hello world"
android:layout_marginTop="100dp"
/>
<Button
android:id="@+id/btn_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ScrollTo"
android:textAllCaps="false"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mxdl.customview.test.view.MyViewPager
android:id="@+id/view_my_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/test" android:title="测试" app:showAsAction="always"/>
</menu>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册