愚妄i
2018-07-10 21:19
就是圆那里下拉没反应
讲的无法运行
//下拉View public class PullDownView extends View { private float mProgress; Paint mCirclePaint; private int mCircleRadius = 150; private int mCircleRadiuX, mCircleRadiuY; //可拖动的高度 private int mDragHeight = 800; public PullDownView(Context context) { super(context); init(); } public PullDownView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public PullDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public PullDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } private void init() { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //设置抗锯齿 paint.setAntiAlias(true); //设置防抖 paint.setDither(true); //设置填充方式 paint.setStyle(Paint.Style.FILL); paint.setColor(0xff000000); mCirclePaint = paint; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(mCircleRadiuX, mCircleRadiuY, mCircleRadius, mCirclePaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int minWidth = 2*mCircleRadius+getPaddingLeft()+getPaddingRight(); int minHeight = (int) ((mDragHeight*mProgress +0.5f)+getPaddingTop()+getPaddingBottom()); int measureWidth, measureHeight; if (widthMode == MeasureSpec.EXACTLY){ //确定的值 measureWidth = width; } else if (widthMode == MeasureSpec.AT_MOST){ //最多的值 measureWidth = Math.min(minWidth, width); } else { measureWidth = minWidth; } if (heightMode == MeasureSpec.EXACTLY){ //确定的值 measureHeight = height; } else if (heightMode == MeasureSpec.AT_MOST){ //最多的值 measureHeight = Math.min(minHeight, height); } else { measureHeight = minHeight; } //设置宽高 setMeasuredDimension(measureWidth, measureHeight); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mCircleRadiuX = getWidth()>>1; mCircleRadiuY = getHeight()>>1; } /** * 设置拖动进度 * @param progress */ public void setProgress(float progress){ Log.e("TAG", "p"+progress); mProgress = progress; requestLayout(); } } //RelativeLayout下拉监听 final PullDownView pullDownView = findViewById(R.id.pulldown); findViewById(R.id.activity_main).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { int action = motionEvent.getAction(); switch (action){ case MotionEvent.ACTION_DOWN: mTouchMoveStartY = motionEvent.getY(); return true; case MotionEvent.ACTION_MOVE: float y = motionEvent.getY(); if (y>=mTouchMoveStartY){ float moveSize = y - mTouchMoveStartY; float progress = moveSize>= TOUCH_MOVE_MAX_Y? 1: moveSize/TOUCH_MOVE_MAX_Y; pullDownView.setProgress(progress); } return true; case MotionEvent.ACTION_UP: break; } return false; } }); //Xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:top="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff"> <com.summer.h5.View.PullDownView android:id="@+id/pulldown" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>
自定义实现顶部粘性下拉刷新效果
10723 学习 · 29 问题
相似问题