滚轮样式的性别选择器
来源:互联网 发布:redis 主从数据同步 编辑:程序博客网 时间:2024/05/19 15:39
最近项目需要,做日历轮子‘性别轮子
日历轮子用的wheelview,现在来保存一下性别轮子用法
首先,自定义PickView
public class PickView extends View{ public static final String TAG = "PickerView"; /** * text之间间距和minTextSize之比 */ public static final float MARGIN_ALPHA = 2.8f; /** * 自动回滚到中间的速度 */ public static final float SPEED = 2; private List<String> mDataList; /** * 选中的位置,这个位置是mDataList的中心位置,一直不变 */ private int mCurrentSelected; private Paint mPaint; private float mMaxTextSize = 80; private float mMinTextSize = 40; private float mMaxTextAlpha = 255; private float mMinTextAlpha = 120; private int mColorText = 0x333333; private int mViewHeight; private int mViewWidth; private float mLastDownY; /** * 滑动的距离 */ private float mMoveLen = 0; private boolean isInit = false; private onSelectListener mSelectListener; private Timer timer; private MyTimerTask mTask; Handler updateHandler = new Handler() { @Override public void handleMessage(Message msg) { if (Math.abs(mMoveLen) < SPEED) { mMoveLen = 0; if (mTask != null) { mTask.cancel(); mTask = null; performSelect(); } } else // 这里mMoveLen / Math.abs(mMoveLen)是为了保有mMoveLen的正负号,以实现上滚或下滚 mMoveLen = mMoveLen - mMoveLen / Math.abs(mMoveLen) * SPEED; invalidate(); } }; public PickView(Context context) { super(context); init(); } public PickView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public void setOnSelectListener(onSelectListener listener) { mSelectListener = listener; } private void performSelect() { if (mSelectListener != null) mSelectListener.onSelect(mDataList.get(mCurrentSelected)); } public void setData(List<String> datas) { mDataList = datas; mCurrentSelected = datas.size() / 2; invalidate(); } public void setSelected(int selected) { mCurrentSelected = selected; } private void moveHeadToTail() { String head = mDataList.get(0); mDataList.remove(0); mDataList.add(head); } private void moveTailToHead() { String tail = mDataList.get(mDataList.size() - 1); mDataList.remove(mDataList.size() - 1); mDataList.add(0, tail); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mViewHeight = getMeasuredHeight(); mViewWidth = getMeasuredWidth(); // 按照View的高度计算字体大小 mMaxTextSize = mViewHeight / 4.0f; mMinTextSize = mMaxTextSize / 2f; isInit = true; invalidate(); } private void init() { timer = new Timer(); mDataList = new ArrayList<String>(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Paint.Style.FILL); mPaint.setTextAlign(Paint.Align.CENTER); mPaint.setColor(mColorText); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 根据index绘制view if (isInit) drawData(canvas); } private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f, mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标 float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); Paint.FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected), x, baseline, mPaint); // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 1); } } /** * @param canvas * @param position * 距离mCurrentSelected的差值 * @param type * 1表示向下绘制,-1表示向上绘制 */ private void drawOtherText(Canvas canvas, int position, int type) { float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type * mMoveLen); float scale = parabola(mViewHeight / 4.0f, d); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); Paint.FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected + type * position), (float) (mViewWidth / 2.0), baseline, mPaint); } /** * 抛物线 * * @param zero * 零点坐标 * @param x * 偏移量 * @return scale */ private float parabola(float zero, float x) { float f = (float) (1 - Math.pow(x / zero, 2)); return f < 0 ? 0 : f; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: doDown(event); break; case MotionEvent.ACTION_MOVE: doMove(event); break; case MotionEvent.ACTION_UP: doUp(event); break; } return true; } private void doDown(MotionEvent event) { if (mTask != null) { mTask.cancel(); mTask = null; } mLastDownY = event.getY(); } private void doMove(MotionEvent event) { mMoveLen += (event.getY() - mLastDownY); if (mMoveLen > MARGIN_ALPHA * mMinTextSize / 2) { // 往下滑超过离开距离 moveTailToHead(); mMoveLen = mMoveLen - MARGIN_ALPHA * mMinTextSize; } else if (mMoveLen < -MARGIN_ALPHA * mMinTextSize / 2) { // 往上滑超过离开距离 moveHeadToTail(); mMoveLen = mMoveLen + MARGIN_ALPHA * mMinTextSize; } mLastDownY = event.getY(); invalidate(); } private void doUp(MotionEvent event) { // 抬起手后mCurrentSelected的位置由当前位置move到中间选中位置 if (Math.abs(mMoveLen) < 0.0001) { mMoveLen = 0; return; } if (mTask != null) { mTask.cancel(); mTask = null; } mTask = new MyTimerTask(updateHandler); timer.schedule(mTask, 0, 10); } class MyTimerTask extends TimerTask { Handler handler; public MyTimerTask(Handler handler) { this.handler = handler; } @Override public void run() { handler.sendMessage(handler.obtainMessage()); } } public interface onSelectListener { void onSelect(String text); }}
然后,dialog对话框的布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/dialog_white_drawable" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp"> <TextView android:id="@+id/tv_sexdialog_sure" android:layout_width="77dip" android:layout_height="33dip" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center" android:text="确认" android:textSize="16sp" /> <TextView android:id="@+id/tv_dialog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center" android:text="选择性别" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_sexdialog_cancel" android:layout_width="77dip" android:layout_height="33dip" android:layout_alignParentLeft="true" android:gravity="center" android:text="取消" android:textSize="16sp" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginBottom="10dp" android:background="@color/high_gray_color" /> <com.jixiong.teen.util.datepickdialogutil.PickView android:id="@+id/pvPickView" android:layout_width="match_parent" android:layout_height="@dimen/margin_onehundred_sixty" android:layout_gravity="center" /> </LinearLayout>
最后就是在activity中需要的地方,使用对话框弹出选择:
首先,声明当前性别
public class PickViewActivity extends Activity { Button btnShow; PickView pickView; //声明当前性别 String currentSex;然后在相应的点击事件里面写,并用当前性别currentSex去赋值
View view = View.inflate(this,R.layout.sexpick_layout,null);mDialog = new MyDialog(this,view);pickView = (PickView)view.findViewById(R.id.pvPickView);//定义滚动选择器的数据项ArrayList<String> grade = new ArrayList<>();grade.add("女");grade.add("男");//为滚动选择器设置数据pickView.setData(grade);pickView.setOnSelectListener(new PickView.onSelectListener() { @Override public void onSelect(String text) { Log.i("tag","选择了"+text); currentSex= text; }});mDialog.show();
public class MyDialog extends Dialog { Activity context; View view; public MyDialog(Activity context,View view) { super(context); this.context = context; this.view = view; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setBackgroundDrawableResource(R.color.bantouming_color); /*view = View.inflate(this.context, R.layout.dialog_user_headimg, null);*/ setContentView(view); WindowManager m = context.getWindowManager(); Display d = m.getDefaultDisplay(); WindowManager.LayoutParams p = getWindow().getAttributes(); //获取对话框当前的参数值 p.height = (int) (d.getHeight() * 0.35); //高度设置为屏幕的0.6 p.width = (int) (d.getWidth() * 0.7); //宽度设置为屏幕的0.95 getWindow().setAttributes(p); //设置生效 }}
现在将此添加到点击事件里面,就会弹出滑动选择性别了
具体是参考博客http://blog.csdn.net/flt1130045604/article/details/51132902
0 0
- 滚轮样式的性别选择器
- 使Android原生时间选择器样式为滚轮
- 性别选择器
- 自定义的iOS的滚轮选择器,附带农历选择器实现
- 自定义的iOS的滚轮选择器,附带农历选择器实现
- 自定义的iOS的滚轮选择器,附带农历选择器实现
- 样式的部分选择器
- Android滚轮选择器实现
- 滚轮地区选择器
- android滚轮选择器
- RecyclerView 实现滚轮选择器
- 自定义滚轮选择器Wheelview
- 双滚轮条件选择器
- 自定义滚轮效果选择器spinnerwheel的使用总结
- CSS样式选择器的总结
- 带阴影的样式选择器
- 时间,性别,选择器
- 自定义的iOS的滚轮选择器,附带农历选择器实现代码
- Android\Java回调 , 解决了我大难题 , 本文转载
- 1107. Social Clusters (30)
- 递归处理无限级分类为树形数组
- 什么是虚拟现实,什么是增强现实?
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
- 滚轮样式的性别选择器
- 关于maven依赖范围的个人理解
- 习题笔记---蓝桥--170107
- LeetCode: Remove K Digits
- jmeter接口自动化测试之二:jenkins+maven+jmeter
- GLSL 内建函数
- spring 3.2 自定义参数绑定--日期格式转换器
- 关于ml.exe编译时的错误提示:error A2138 和 error A2036
- Html 5 app + Mui 运用时 header 真机测试(坑)