自定义 view 圆形并添加拉动响应

前言:

各位同学大家好,有段时间没有给大家更新文章了,因为前段时间要去考驾照一直都在学车所以 都没时间更新文章了,最近又开始学习安卓的一些技术 所以就开始一些安卓部分的文章,今天给大家讲的自定义 view 圆形并添加拉动响应 。那么废话不多说我们正式开始。

效果图

1CA2F4AD33FCDE9CF8C37D71A96E5C22[00_00_07--00_00_27].gif

具体实现:

自定义TouchPullView 继承view

package com.example.myapplication.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
/**
*
* 创建人:xuqing
* 创建时间:2022年8月8日10:08:35
* 类说明:自定义绘制view
*
*
*/
public class TouchPullView extends View {
   private static final String TAG = "TouchPullView";
   //圆的画笔
   private Paint mCirclePanit;
   //圆的半径
   private int mCircleRadius=100;
   private int mCirclePointX ,mCirclePointY;
   private float mProgress;
   private int mDrawHeight=800;
   public TouchPullView(Context context) {
       super(context);
       init();
   }
   public TouchPullView(Context context, @Nullable AttributeSet attrs) {
       super(context, attrs);
       init();
   }
   public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       init();
   }
   public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
       super(context, attrs, defStyleAttr, defStyleRes);
       init();
   }
   private void  init(){
       Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
       //设置抗锯齿
       paint.setAntiAlias(true);
       //设置防抖动
       paint.setDither(true);
       //设置填充方式
       paint.setStyle(Paint.Style.FILL);
       paint.setColor(Color.RED);
       mCirclePanit=paint;
   }
   @Override
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       canvas.drawCircle(mCirclePointX,mCirclePointY,mCircleRadius,mCirclePanit);
   }
   /***
    *
    * @param widthMeasureSpec
    * @param heightMeasureSpec
    * 当测量时触发
    *
    *
    */
   @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 iWidth=2*mCircleRadius+getPaddingLeft()+getPaddingRight();
       int iHeight=(int)((mDrawHeight*mProgress +0.5f)+getPaddingTop()+getPaddingBottom());
       int measureWidth, measureHeight;
       if(widthMode==MeasureSpec.EXACTLY){
           //确切的
           measureWidth=width;

       }else if(widthMode==MeasureSpec.AT_MOST){
           //最多的
           measureWidth=Math.min(iWidth,width);
       }else {
           measureWidth=iWidth;
       }
       if(heightMode==MeasureSpec.EXACTLY){
           //确切的
           measureHeight=height;

       }else if(heightMode==MeasureSpec.AT_MOST){
           //最多的
           measureHeight=Math.min(iHeight,height);
       }else {
           measureHeight=iHeight;
       }
       //设置测量的高度和宽度
       setMeasuredDimension(measureWidth,measureHeight);
   }
   /**
    *
    *
    * @param w
    * @param h
    * @param oldw
    * @param oldh
    * 当大小改变时候触发
    */
   @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
       super.onSizeChanged(w, h, oldw, oldh);
         mCirclePointX=getWidth()>>1;
         mCirclePointY=getHeight()>>1;
   }
   /***
    *
    * @param progress
    * 设置进度
    *
    */
   public  void  setProgress(float  progress){
       mProgress=progress;
       //请求重新进行测量
       Log.e(TAG, "setProgress: progress  --- >  "+progress );
       requestLayout();
   }
}

我们在 onSizeChanged 里面·设置大小 我们再onDraw 方法里面 进行绘制 然后我们 onMeasure 重新绘制 当手指拖动屏幕的时候 意图发生改变的时候 重新绘制

外部调用

package com.example.myapplication; 
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import com.example.myapplication.widget.TouchPullView;

public class MainActivity extends AppCompatActivity {
    private  static final float TOUCH_MOVE_MAX_Y=600;
    private  float mTouchMoveStartY;
    private TouchPullView  pullView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pullView=findViewById(R.id.touchPull);
        findViewById(R.id.activity_main).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //得到意图
                int action=event.getActionMasked();
                switch (action){
                    case MotionEvent.ACTION_DOWN:
                        mTouchMoveStartY=event.getY();
                        return  true;
                    case MotionEvent.ACTION_MOVE:
                        float y=event.getY();
                        if(y>=mTouchMoveStartY){
                            float movesize=y - mTouchMoveStartY;
                            float progress=movesize>= TOUCH_MOVE_MAX_Y?1:movesize/TOUCH_MOVE_MAX_Y;
                            pullView.setProgress(progress);}
                        break;
                        default:
                            break;
                     }
                return false;
            }
        });
    }
}

你可能感兴趣的:(自定义 view 圆形并添加拉动响应)