android自定义滚轴选择器

本文实例为大家分享了android自定义滚轴选择器的具体代码,供大家参考,具体内容如下

效果图:

点击选择餐具弹出底部弹窗

 private Dialog dialog;
 private View inflate;
 PickValueView pickString;

case R.id.rl_cj_num://餐具数量选择
    //选择对话框
    dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
    View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_cj_num, null);
    //获取组件
    tv_cancel = contentView.findViewById(R.id.tv_cancel);
    pickString = contentView.findViewById(R.id.pickString);
    //获取Dialog的监听
    tv_cancel.setOnClickListener(this);
    pickString.setOnSelectedChangeListener(this);
    String[] valueStr = new String[]{"无需餐具", "1人", "2人", "3人", "4人", "5人",
      "6人", "7人", "8人"};
    pickString.setValueData(valueStr, valueStr[1]);
    dialog.setContentView(contentView);
    ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
    layoutParams.width = getResources().getDisplayMetrics().widthPixels;
    contentView.setLayoutParams(layoutParams);
    dialog.getWindow().setGravity(Gravity.BOTTOM);//弹窗位置
    dialog.getWindow().setWindowAnimations(R.style.ActionSheetDialogStyle);//弹窗样式
    dialog.show();//显示弹窗
    break;

styles.xml中的样式:

 <!-- Dialog弹窗效果-->
 <style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">
  <!-- 背景透明 -->
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:windowContentOverlay">@null</item>
  <!-- 浮于Activity之上 -->
  <item name="android:windowIsFloating">true</item>
  <!-- 边框 -->
  <item name="android:windowFrame">@null</item>
  <!-- Dialog以外的区域模糊效果 -->
  <item name="android:backgroundDimEnabled">true</item>
  <!-- 无标题 -->
  <item name="android:windowNoTitle">true</item>
  <!-- 半透明 -->
  <item name="android:windowIsTranslucent">true</item>
  <!-- Dialog进入及退出动画 -->
  <item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item>
 </style>
 <!-- ActionSheet进出动画 -->
 <style name="ActionSheetDialogAnimation" parent="@android:style/Animation.Dialog">
  <item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>
  <item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>
 </style>

 <style name="SplashTheme" parent="AppTheme">
  <!-- 这里的trans自己写一个#00000000即可-->
  <item name="android:windowBackground">@drawable/layer_splash</item>
  <item name="windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item>
 </style>

弹入弹出动画效果:

anim下新建:弹入资源文件actionsheet_dialog_in和弹出资源文件actionsheet_dialog_out

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="200"
 android:fromYDelta="100%"
 android:toYDelta="0" />
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="200"
 android:fromYDelta="0"
 android:toYDelta="100%" />

自定义弹窗布局dialog_cj_num:

<?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:background="#f9f9f9"
 android:orientation="vertical">

 <TextView
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="餐具选择"
  android:textColor="#333333"
  android:textSize="16sp" />

 <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#E8E8E8" />

 <com.shanjing.mymeishi.View.PickValueView
  android:id="@+id/pickString"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

 <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#E8E8E8" />

 <TextView
  android:id="@+id/tv_cancel"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="取消"
  android:textColor="#333333"
  android:textSize="16sp" />
</LinearLayout>

自定义视图PickValueView和引用类MyNumberPicker:

package com.shanjing.mymeishi.View;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

/**
 * 选择日期视图
 */

public class PickValueView extends LinearLayout implements NumberPicker.OnValueChangeListener {
 private Context mContext;
 /**
  * 组件 标题、单位、滚轮
  */
 private TextView mTitleLeft, mTitleMiddle, mTitleRight;
 private TextView mUnitLeft, mUnitMiddle, mUnitRight;
 private MyNumberPicker mNpLeft, mNpMiddle, mNpRight;
 /**
  * 数据个数 1列 or 2列 or 3列
  */
 private int mViewCount = 1;
 /**
  * 一组数据长度
  */
 private final int DATA_SIZE = 3;

 /**
  * 需要设置的值与默认值
  */
 private Object[] mLeftValues;
 private Object[] mMiddleValues;
 private Object[] mRightValues;
 private Object mDefaultLeftValue;
 private Object mDefaultMiddleValue;
 private Object mDefaultRightValue;
 /**
  * 当前正在显示的值
  */
 private Object[] mShowingLeft = new Object[DATA_SIZE];
 private Object[] mShowingMiddle = new Object[DATA_SIZE];
 private Object[] mShowingRight = new Object[DATA_SIZE];

 /**
  * 步长
  */
 private int mLeftStep = 5;
 private int mMiddleStep = 1;
 private int mRightStep = 1;
 /**
  * 回调接口对象
  */
 private onSelectedChangeListener mSelectedChangeListener;

 public PickValueView(Context context) {
  super(context);
  this.mContext = context;
  generateView();
 }

 public PickValueView(Context context, AttributeSet attrs) {
  super(context, attrs);
  this.mContext = context;
  generateView();
 }

 public PickValueView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  this.mContext = context;
  generateView();
 }

 /**
  * 生成视图
  */
 private void generateView() {
  //标题
  LinearLayout titleLayout = new LinearLayout(mContext);
  LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  titleParams.setMargins(0, 0, 0, dip2px(12));
  titleLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  titleLayout.setOrientation(HORIZONTAL);
  mTitleLeft = new TextView(mContext);
  mTitleMiddle = new TextView(mContext);
  mTitleRight = new TextView(mContext);

  LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
  TextView[] titles = new TextView[]{mTitleLeft, mTitleMiddle, mTitleRight};
  for (int i = 0; i < titles.length; i++) {
   titles[i].setLayoutParams(params);
   titles[i].setGravity(Gravity.CENTER);
   titles[i].setTextColor(Color.parseColor("#000000"));
   titles[i].setTextSize(12);
  }
  titleLayout.addView(mTitleLeft);
  titleLayout.addView(mTitleMiddle);
  titleLayout.addView(mTitleRight);
  //内容
  LinearLayout contentLayout = new LinearLayout(mContext);
  contentLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  contentLayout.setOrientation(HORIZONTAL);
  contentLayout.setGravity(Gravity.CENTER);
  mNpLeft = new MyNumberPicker(mContext);
  mNpMiddle = new MyNumberPicker(mContext);
  mNpRight = new MyNumberPicker(mContext);
  mUnitLeft = new TextView(mContext);
  mUnitMiddle = new TextView(mContext);
  mUnitRight = new TextView(mContext);

  MyNumberPicker[] nps = new MyNumberPicker[]{mNpLeft, mNpMiddle, mNpRight};
  for (int i = 0; i < nps.length; i++) {
   nps[i].setLayoutParams(params);
   nps[i].setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
   nps[i].setOnValueChangedListener(this);
  }

  contentLayout.addView(mNpLeft);
  contentLayout.addView(mUnitLeft);
  contentLayout.addView(mNpMiddle);
  contentLayout.addView(mUnitMiddle);
  contentLayout.addView(mNpRight);
  contentLayout.addView(mUnitRight);

  this.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  this.setOrientation(VERTICAL);
  this.addView(titleLayout);
  this.addView(contentLayout);
 }

 /**
  * 初始化数据和值
  */
 private void initViewAndPicker() {
  if (mViewCount == 1) {
   this.mNpMiddle.setVisibility(GONE);
   this.mNpRight.setVisibility(GONE);
   this.mUnitMiddle.setVisibility(GONE);
   this.mUnitRight.setVisibility(GONE);
  } else if (mViewCount == 2) {
   this.mNpRight.setVisibility(GONE);
   this.mUnitRight.setVisibility(GONE);
  }

  //初始化数组值
  if (mLeftValues != null && mLeftValues.length != 0) {
   if (mLeftValues.length < DATA_SIZE) {
    for (int i = 0; i < mLeftValues.length; i++) {
     mShowingLeft[i] = mLeftValues[i];
    }
    for (int i = mLeftValues.length; i < DATA_SIZE; i++) {
     mShowingLeft[i] = -9999;
    }
   } else {
    for (int i = 0; i < DATA_SIZE; i++) {
     mShowingLeft[i] = mLeftValues[i];
    }
   }
   mNpLeft.setMinValue(0);
   mNpLeft.setMaxValue(DATA_SIZE - 1);
   if (mDefaultLeftValue != null)
    updateLeftView(mDefaultLeftValue);
   else
    updateLeftView(mShowingLeft[0]);
  }
  /**
   * 中间控件
   */
  if (mViewCount == 2 || mViewCount == 3) {
   if (mMiddleValues != null && mMiddleValues.length != 0) {
    if (mMiddleValues.length < DATA_SIZE) {
     for (int i = 0; i < mMiddleValues.length; i++) {
      mShowingMiddle[i] = mMiddleValues[i];
     }
     for (int i = mMiddleValues.length; i < DATA_SIZE; i++) {
      mShowingMiddle[i] = -9999;
     }
    } else {
     for (int i = 0; i < DATA_SIZE; i++) {
      mShowingMiddle[i] = mMiddleValues[i];
     }
    }
    mNpMiddle.setMinValue(0);
    mNpMiddle.setMaxValue(DATA_SIZE - 1);
    if (mDefaultMiddleValue != null)
     updateMiddleView(mDefaultMiddleValue);
    else
     updateMiddleView(mShowingMiddle[0]);
   }
  }

  /**
   * 右侧控件
   */
  if (mViewCount == 3) {
   if (mRightValues != null && mRightValues.length != 0) {
    if (mRightValues.length < DATA_SIZE) {
     for (int i = 0; i < mRightValues.length; i++) {
      mShowingRight[i] = mRightValues[i];
     }
     for (int i = mRightValues.length; i < DATA_SIZE; i++) {
      mShowingRight[i] = -9999;
     }
    } else {
     for (int i = 0; i < DATA_SIZE; i++) {
      mShowingRight[i] = mRightValues[i];
     }
    }
    mNpRight.setMinValue(0);
    mNpRight.setMaxValue(DATA_SIZE - 1);
    if (mDefaultRightValue != null)
     updateRightView(mDefaultRightValue);
    else
     updateRightView(mShowingRight[0]);
   }
  }

 }

 private void updateLeftView(Object value) {
  updateValue(value, 0);
 }

 private void updateMiddleView(Object value) {
  updateValue(value, 1);
 }

 private void updateRightView(Object value) {
  updateValue(value, 2);
 }

 /**
  * 更新滚轮视图
  *
  * @param value
  * @param index
  */
 private void updateValue(Object value, int index) {
  String showStr[] = new String[DATA_SIZE];
  MyNumberPicker picker;
  Object[] showingValue;
  Object[] values;
  int step;
  if (index == 0) {
   picker = mNpLeft;
   showingValue = mShowingLeft;
   values = mLeftValues;
   step = mLeftStep;
  } else if (index == 1) {
   picker = mNpMiddle;
   showingValue = mShowingMiddle;
   values = mMiddleValues;
   step = mMiddleStep;
  } else {
   picker = mNpRight;
   showingValue = mShowingRight;
   values = mRightValues;
   step = mRightStep;
  }

  if (values instanceof Integer[]) {
   for (int i = 0; i < DATA_SIZE; i++) {
    showingValue[i] = (int) value - step * (DATA_SIZE / 2 - i);
    int offset = (int) values[values.length - 1] - (int) values[0] + step;
    if ((int) showingValue[i] < (int) values[0]) {
     showingValue[i] = (int) showingValue[i] + offset;
    }
    if ((int) showingValue[i] > (int) values[values.length - 1]) {
     showingValue[i] = (int) showingValue[i] - offset;
    }
    showStr[i] = "" + showingValue[i];
   }
  } else {
   int strIndex = 0;
   for (int i = 0; i < values.length; i++) {
    if (values[i].equals(value)) {
     strIndex = i;
     break;
    }
   }
   for (int i = 0; i < DATA_SIZE; i++) {
    int temp = strIndex - (DATA_SIZE / 2 - i);
    if (temp < 0) {
     temp += values.length;
    }
    if (temp >= values.length) {
     temp -= values.length;
    }
    showingValue[i] = values[temp];
    showStr[i] = (String) values[temp];
   }
  }
  picker.setDisplayedValues(showStr);
  picker.setValue(DATA_SIZE / 2);
  picker.postInvalidate();
 }

 @Override
 public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  if (picker == mNpLeft) {
   updateLeftView(mShowingLeft[newVal]);
  } else if (picker == mNpMiddle) {
   updateMiddleView(mShowingMiddle[newVal]);
  } else if (picker == mNpRight) {
   updateRightView(mShowingRight[newVal]);
  }
  if (mSelectedChangeListener != null) {
   mSelectedChangeListener.onSelected(this, mShowingLeft[DATA_SIZE / 2], mShowingMiddle[DATA_SIZE / 2], mShowingRight[DATA_SIZE / 2]);
  }
 }

 /**
  * 设置数据--单列数据
  *
  * @param leftValues
  * @param mDefaultLeftValue
  */
 public void setValueData(Object[] leftValues, Object mDefaultLeftValue) {
  this.mViewCount = 1;
  this.mLeftValues = leftValues;
  this.mDefaultLeftValue = mDefaultLeftValue;

  initViewAndPicker();
 }

 /**
  * 设置数据--两列数据
  *
  * @param leftValues
  * @param mDefaultLeftValue
  * @param middleValues
  * @param defaultMiddleValue
  */
 public void setValueData(Object[] leftValues, Object mDefaultLeftValue, Object[] middleValues, Object defaultMiddleValue) {
  this.mViewCount = 2;
  this.mLeftValues = leftValues;
  this.mDefaultLeftValue = mDefaultLeftValue;

  this.mMiddleValues = middleValues;
  this.mDefaultMiddleValue = defaultMiddleValue;

  initViewAndPicker();
 }

 /**
  * 设置数据--三列数据
  *
  * @param leftValues
  * @param mDefaultLeftValue
  * @param middleValues
  * @param defaultMiddleValue
  * @param rightValues
  * @param defaultRightValue
  */
 public void setValueData(Object[] leftValues, Object mDefaultLeftValue, Object[] middleValues, Object defaultMiddleValue, Object[] rightValues, Object defaultRightValue) {
  this.mViewCount = 3;
  this.mLeftValues = leftValues;
  this.mDefaultLeftValue = mDefaultLeftValue;

  this.mMiddleValues = middleValues;
  this.mDefaultMiddleValue = defaultMiddleValue;

  this.mRightValues = rightValues;
  this.mDefaultRightValue = defaultRightValue;

  initViewAndPicker();
 }

 /**
  * 设置左边数据步长
  *
  * @param step
  */
 public void setLeftStep(int step) {
  this.mLeftStep = step;
  initViewAndPicker();
 }

 /**
  * 设置中间数据步长
  *
  * @param step
  */
 public void setMiddleStep(int step) {
  this.mMiddleStep = step;
  initViewAndPicker();
 }

 /**
  * 设置右边数据步长
  *
  * @param step
  */
 public void setRightStep(int step) {
  this.mRightStep = step;
  initViewAndPicker();
 }

 /**
  * 设置标题
  *
  * @param left
  * @param middle
  * @param right
  */
 public void setTitle(String left, String middle, String right) {
  if (left != null) {
   mTitleLeft.setVisibility(VISIBLE);
   mTitleLeft.setText(left);
  } else {
   mTitleLeft.setVisibility(GONE);
  }
  if (middle != null) {
   mTitleMiddle.setVisibility(VISIBLE);
   mTitleMiddle.setText(middle);
  } else {
   mTitleMiddle.setVisibility(GONE);
  }
  if (right != null) {
   mTitleRight.setVisibility(VISIBLE);
   mTitleRight.setText(right);
  } else {
   mTitleRight.setVisibility(GONE);
  }
  this.postInvalidate();
 }

 public void setUnitLeft(String unitLeft) {
  setUnit(unitLeft, 0);
 }

 public void setmUnitMiddle(String unitMiddle) {
  setUnit(unitMiddle, 1);
 }

 public void setUnitRight(String unitRight) {
  setUnit(unitRight, 2);
 }

 private void setUnit(String unit, int index) {
  TextView tvUnit;
  if (index == 0) {
   tvUnit = mUnitLeft;
  } else if (index == 1) {
   tvUnit = mUnitMiddle;
  } else {
   tvUnit = mUnitRight;
  }
  if (unit != null) {
   tvUnit.setText(unit);
  } else {
   tvUnit.setText(" ");
  }
  initViewAndPicker();
 }

 /**
  * 设置回调
  *
  * @param listener
  */
 public void setOnSelectedChangeListener(onSelectedChangeListener listener) {
  this.mSelectedChangeListener = listener;
 }

 /**
  * dp转px
  *
  * @param dp
  * @return
  */
 private int dip2px(int dp) {
  float scale = mContext.getResources().getDisplayMetrics().density;
  return (int) (scale * dp + 0.5f);
 }

 /**
  * 回调接口
  */
 public interface onSelectedChangeListener {
  void onSelected(PickValueView view, Object leftValue, Object middleValue, Object rightValue);
 }
}
package com.shanjing.mymeishi.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;

import java.lang.reflect.Field;

public class MyNumberPicker extends NumberPicker {
 private Context mContext;
 NumberPicker picker;

 public MyNumberPicker(Context context) {
  super(context);
  picker = this;
  mContext = context;
 }

 public MyNumberPicker(Context context, AttributeSet attrs) {
  super(context, attrs);
  picker = this;
  mContext = context;
 }

 public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  picker = this;
  mContext = context;
 }

 @Override
 public void addView(View child) {
  super.addView(child);
  updateView(child);
 }

 @Override
 public void addView(View child, int index, ViewGroup.LayoutParams params) {
  super.addView(child, index, params);
  updateView(child);
 }

 @Override
 public void addView(View child, ViewGroup.LayoutParams params) {
  super.addView(child, params);
  updateView(child);
 }

 private void updateView(View view) {
  if (view instanceof EditText) {
   ((EditText) view).setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
   ((EditText) view).setTextColor(Color.parseColor("#000000"));//进入时滚轴显示颜色
  }
 }

 private int mRight, mLeft, mBottom;
 private int mTopSelectionDividerTop;
 private int mBottomSelectionDividerBottom;
 private int[] mSelectorIndices;
 private int mScrollState;
 private SparseArray<String> mSelectorIndexToStringCache;
 private EditText mInputText;
 private Paint mSelectorWheelPaint;
 private int mSelectorElementHeight;
 private int mCurrentScrollOffset;
 private boolean mHasSelectorWheel;
 private boolean mHideWheelUntilFocused;
 private Drawable mSelectionDivider;

 /**
  * 通过反射获取值
  */
 private void getMyValue() {
  mLeft = super.getLeft();
  mRight = super.getRight();
  mBottom = super.getBottom();
  Field[] pickerFields = NumberPicker.class.getDeclaredFields();
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mSelectorWheelPaint")) {
    try {
     mSelectorWheelPaint = (Paint) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mSelectorElementHeight")) {
    try {
     mSelectorElementHeight = (int) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mCurrentScrollOffset")) {
    try {
     mCurrentScrollOffset = (int) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mInputText")) {
    try {
     mInputText = (EditText) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mSelectorIndexToStringCache")) {
    try {
     mSelectorIndexToStringCache = (SparseArray<String>) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mSelectorIndices")) {
    try {
     mSelectorIndices = (int[]) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mHasSelectorWheel")) {
    try {
     mHasSelectorWheel = (boolean) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mHideWheelUntilFocused")) {
    try {
     mHideWheelUntilFocused = (boolean) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
//  for (Field field : pickerFields) {
//   field.setAccessible(true);
//   if (field.getName().equals("mDecrementVirtualButtonPressed")) {
//    try {
//     mDecrementVirtualButtonPressed = (boolean) field.get(picker);
//    } catch (IllegalAccessException e) {
//     e.printStackTrace();
//    }
//    break;
//   }
//  }
//  for (Field field : pickerFields) {
//   field.setAccessible(true);
//   if (field.getName().equals("mIncrementVirtualButtonPressed")) {
//    try {
//     mIncrementVirtualButtonPressed = (boolean) field.get(picker);
//    } catch (IllegalAccessException e) {
//     e.printStackTrace();
//    }
//    break;
//   }
//  }
//  for (Field field : pickerFields) {
//   field.setAccessible(true);
//   if (field.getName().equals("mVirtualButtonPressedDrawable")) {
//    try {
//     mVirtualButtonPressedDrawable = (Drawable) field.get(picker);
//    } catch (IllegalAccessException e) {
//     e.printStackTrace();
//    }
//    break;
//   }
//  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mScrollState")) {
    try {
     mScrollState = (int) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mTopSelectionDividerTop")) {
    try {
     mTopSelectionDividerTop = (int) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mBottomSelectionDividerBottom")) {
    try {
     mBottomSelectionDividerBottom = (int) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }
  for (Field field : pickerFields) {
   field.setAccessible(true);
   if (field.getName().equals("mSelectionDivider")) {
    try {
     mSelectionDivider = (Drawable) field.get(picker);
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
    break;
   }
  }

 }

 @Override
 protected void onDraw(Canvas canvas) {
//  super.onDraw(canvas);
  getMyValue();
  mSelectorWheelPaint.setColor(Color.BLUE);

  if (!mHasSelectorWheel) {
   super.onDraw(canvas);
   return;
  }
  final boolean showSelectorWheel = mHideWheelUntilFocused ? hasFocus() : true;
  float x = (mRight - mLeft) / 2;
  float y = mCurrentScrollOffset;

//  if (showSelectorWheel && mVirtualButtonPressedDrawable != null
//    && mScrollState == OnScrollListener.SCROLL_STATE_IDLE) {
//   if (mDecrementVirtualButtonPressed) {
//    mVirtualButtonPressedDrawable.setState(View.PRESSED_STATE_SET);
//    mVirtualButtonPressedDrawable.setBounds(0, 0, mRight, mTopSelectionDividerTop);
//    mVirtualButtonPressedDrawable.draw(canvas);
//   }
//   if (mIncrementVirtualButtonPressed) {
//    mVirtualButtonPressedDrawable.setState(PRESSED_STATE_SET);
//    mVirtualButtonPressedDrawable.setBounds(0, mBottomSelectionDividerBottom, mRight,
//      mBottom);
//    mVirtualButtonPressedDrawable.draw(canvas);
//   }
//  }

  int[] selectorIndices = mSelectorIndices;
  for (int i = 0; i < selectorIndices.length; i++) {
   int selectorIndex = selectorIndices[i];
   String scrollSelectorValue = mSelectorIndexToStringCache.get(selectorIndex);
   if (i != 1) {
    mSelectorWheelPaint.setColor(Color.BLACK);
    mSelectorWheelPaint.setTextSize(sp2px(16));
   } else {
    mSelectorWheelPaint.setColor(Color.parseColor("#000000"));//滑动时当前滚轴上的内容颜色
    mSelectorWheelPaint.setTextSize(sp2px(20));
   }

   if ((showSelectorWheel && i != 1) ||
     (i == 1 && mInputText.getVisibility() != VISIBLE)) {
    Rect mRect = new Rect();
    mSelectorWheelPaint.getTextBounds(scrollSelectorValue, 0, scrollSelectorValue.length(), mRect);
    canvas.drawText(scrollSelectorValue, x, y, mSelectorWheelPaint);
   }
   y += mSelectorElementHeight;
  }

  // draw the selection dividers
  if (showSelectorWheel && mSelectionDivider != null) {
   mSelectionDivider = new ColorDrawable(Color.parseColor("#a0c4c4c4"));//滚轴线的颜色
   // draw the top divider
   int topOfTopDivider = mTopSelectionDividerTop;
   int bottomOfTopDivider = topOfTopDivider + 2;
   mSelectionDivider.setBounds(0, topOfTopDivider, mRight, bottomOfTopDivider);
   mSelectionDivider.draw(canvas);

   // draw the bottom divider
   int bottomOfBottomDivider = mBottomSelectionDividerBottom;
   int topOfBottomDivider = bottomOfBottomDivider - 2;
   mSelectionDivider.setBounds(0, topOfBottomDivider, mRight, bottomOfBottomDivider);
   mSelectionDivider.draw(canvas);
  }
 }

 private int sp2px(int sp) {
  float scale = mContext.getResources().getDisplayMetrics().scaledDensity;
  return (int) (scale * sp + 0.5f);
 }
}

用法:

先实现PickValueView.onSelectedChangeListener

然后TextView显示:

@Override
public void onSelected(PickValueView view, Object leftValue, Object middleValue, Object rightValue) {
 String selectedStr = (String) leftValue;
 tv_cj_num.setText(selectedStr);
}

完成,自定义弹窗里可以修改一些属性。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android自定义可循环的滚动选择器CycleWheelView

    最近碰到个项目要使用到滚动选择器,原生的NumberPicker可定制性太差,不大符合UI要求. 网上开源的WheelView是用ScrollView写的,不能循环滚动,而且当数据量很大时要加载的Item太多,性能非常低. 然后,还是自己写一个比较靠谱,用的是ListView实现的.写完自己体验了一下,性能不错,再大的数据也不怕了. 感觉不错,重新封装了一下,提供了一些接口可以直接按照自己的需求定制,调用方法在MainActivity中. 补个图片: 不多说了,直接上代码: CycleWheel

  • android 字体颜色选择器(ColorPicker)介绍

    primary_text_yellow.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this f

  • Android时间选择器、日期选择器实现代码

    本文为大家分享了两款选择器,一款可以针对时间进行选择.一款可以针对日期进行选择,供大家参考,具体内容如下 一.时间选择器 1.1.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and

  • Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)

    本文Github代码链接 https://github.com/AndroidMsky/AndoirdIOSPicker 先上图吧: 这是笔者最近一个项目一直再用的一个选择器库,自己也在其中做了修改,并决定持续维护下去. 先看使用方法: 日期选择: private void showDateDialog(List<Integer> date) { DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this); bui

  • 基于android背景选择器selector的用法汇总

    一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 没有焦点时的背景图片 -->    <item android:dr

  • Android中颜色选择器和改变字体颜色的实例教程

    1.构建一张七彩图: 我们经常看到这种样子的颜色选择器吧.. 然后其实右边的亮度选择是: 这样我们的代码就可以进行啦... // 创建七彩图片 private void init() { int[] oc = { 0xffff0000, 0xffffff00, 0xff00ff00, 0xff00ffff, 0xff0000ff, 0xffff00ff, 0xffff0000 }; float[] op = { 0, 0.16667f, 0.33333f, 0.5f, 0.66667f, 0.8

  • Android仿微信照片选择器实现预览查看图片

    好了下面进入正题,我们先看一下实现效果吧: 下面来介绍一下代码: 本思路就是: 1.先到手机中扫描jpeg和png的图片 2.获取导图片的路径和图片的父路径名也就是文件夹名 3.将图片路径和文件夹名分别添加导数据源中 4.数据源有了就是显示了,文件夹显示是利用的popwindow,而图片显示则是GridView 看一下具体代码: 首先开启一个线程去扫描图片 /** * 利用ContentProvider扫描手机中的图片,此方法在运行在子线程中 完成图片的扫描,最终获得jpg最多的那个文件夹 */

  • Android selector背景选择器的使用详解

    在开发应用中,很多情况下要设计listview或button控件的背景,下面总结一下android的selector的用法:1.在drawable中配置Android的selector.将如下的XML文件保存成你自己命名的.xml文件(比如item_bg.xml),并将该文件放置在drawable文件中,在系统使用时根据ListView中的列表项的状态来使用相应的背景图片. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8&q

  • Android PickerView滚动选择器的使用方法

    手机里设置闹钟需要选择时间,那个选择时间的控件就是滚动选择器,前几天用手机刷了MIUI,发现自带的那个时间选择器效果挺好看的,于是就自己仿写了一个,权当练手.先来看效果: 效果还行吧?实现思路就是自定义一个PickerView,单独滚动的是一个PickerView,显然上图中有分和秒的选择所以在布局里用了两个PickerView.由于这里不涉及到text的点击事件,所以只需要继承View就行了,直接把text用canvas画上去.PickerView的实现的主要难点: 难点1: 字体随距离的渐变

  • 浅谈谈Android 图片选择器

    ImageSelector 简介 Android自定义相册,实现了拍照.图片选择(单选/多选).ImageLoader无绑定 任由开发者选择 https://github.com/YancyYe/ImageSelector Demo Download Apk 更新内容 UI重改 所有功能可配置 解决OOM情况 图片手动选择 支持汉语和英语 截图展示 使用说明 步骤一: 通过Gradle抓取 dependencies { compile 'com.yancy.imageselector:image

随机推荐