本文为大家分享了Android实现拖动选择按钮的具体代码,供大家参考,具体内容如下
效果图
View代码
第一步:自定义属性
第二步:自定义圆形
public class CircleView extends View { /** * 默认颜色 */ private final int DEFAULT_COLOR = Color.LTGRAY; /** * 默认半径dp */ private final float DEFAULT_RADIUS = 32; private int mColor; private Paint mCirclePaint; private float mRadius; private float mCenterX; private float mCenterY; public CircleView(Context context) { this(context, null); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DragView); mColor = array.getColor(R.styleable.DragView_color_circle,DEFAULT_COLOR); mCirclePaint = new Paint(); mCirclePaint.setColor(mColor); mCenterY = mCenterX = mRadius = getMeasuredWidth() == 0?DEFAULT_RADIUS:getMeasuredWidth()/2; array.recycle(); } public float getRadius(){ return mRadius; } public int getColor() { return mColor; } @Override protected void onDraw(Canvas canvas) { canvas.drawCircle(mCenterX,mCenterY,mRadius,mCirclePaint); } }
第三步:自定义拖动按钮
public class DragView extends RelativeLayout implements View.OnTouchListener { private Drawable mDragIcon;//拖动图标 private ImageView mDragView;//拖动图标ImageView private int mCircleColor;//圆的颜色 private Context mContext;//上下文 private int mDotNum;//节点数量 private int mWidth;//组件的宽度 private int mHight;//组件的高度 private int mCircleRadius;//园的半径 private int mDragWidth;//拖动图标的宽度 private int mLineWidth,mLineHeight;//中间线的长宽 private float mStartX;//开始拖动的屏幕坐标 private float mCurX;//开始拖动的触点坐标 private float mMinX;//拖动范围的最小值 private float mMaxX;//拖动范围的最大值 private OnNodeSelect mOnNodeSelect;//回调接口 public DragView(Context context) { this(context,null); } public DragView(Context context, AttributeSet attrs) { this(context, attrs,0); } public DragView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.DragView); mDragIcon=array.getDrawable(R.styleable.DragView_icon_drag); mCircleColor=array.getColor(R.styleable.DragView_color_circle,Color.WHITE); mDotNum=array.getInt(R.styleable.DragView_dot_num,2); array.recycle(); mCOntext=context; initView(); } /** *视图初始化,这个方法的作用是把所有的视图添加进来,并做一些初始化的配置 * 其实可以用LayoutInflater把xml的布局文件加载进来,这样比较简便 */ public void initView(){ for (int i=0;i= 0 && x <= mMaxX - mMinX) { v.setTranslationX(mCurX + event.getRawX() - mStartX); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: int distance=(int) (event.getRawX()-mStartX); if (distance>0){ if (Math.abs(distance)>mLineWidth/2){ v.setTranslationX(mCurX + event.getRawX() - mStartX); setAnim(mLineWidth,1); }else { v.setTranslationX(mCurX + event.getRawX() - mStartX); setAnim(0,0); } }else if (distance<0){ if (Math.abs(distance)>mLineWidth/2){ v.setTranslationX(mCurX + event.getRawX() - mStartX); setAnim(0,0); }else { v.setTranslationX(mCurX + event.getRawX() - mStartX); setAnim(mLineWidth,1); } } break; } }else if (v instanceof CircleView){ switch (event.getAction()) { case MotionEvent.ACTION_UP: if (event.getRawX()>getDisplayWidth()/2){ setAnim(mLineWidth,1); }else { setAnim(0,0); } break; } } return true; } //设置平移动画 private void setAnim(float moveX, final int scrollPosition) { ObjectAnimator animator = ObjectAnimator.ofFloat(mDragView, "translationX", mDragView.getTranslationX(), moveX); animator.setDuration(300); animator.start(); if (mOnNodeSelect != null) { mOnNodeSelect.onNodeSelect(scrollPosition); } } public void setNodeSelectListener(OnNodeSelect onNodeSelect) { mOnNodeSelect= onNodeSelect; } //事件回调接口 public interface OnNodeSelect { void onNodeSelect(int position); } //dp转px public int dp2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } //获取屏幕宽度 public int getDisplayWidth(){ DisplayMetrics metrics=new DisplayMetrics(); ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics); return metrics.widthPixels; } }
使用方法
布局文件:
页面代码:
dragView=(DragView)view.findViewById(R.id.view_drag); dragView.setNodeSelectListener(new DragView.OnNodeSelect() { @Override public void onNodeSelect(int position) { Toast.makeText(getActivity(),"我选择了"+position,Toast.LENGTH_SHORT).show(); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。