VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/AddDialog.java
@@ -18,8 +18,8 @@ import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.view.annotation.event.OnClick; import com.wx.wheelview.adapter.BaseWheelAdapter; import com.wx.wheelview.widget.WheelView; import cn.com.basic.face.dialog.wheelview.adapter.BaseWheelAdapter; import cn.com.basic.face.dialog.wheelview.widget.WheelView; import java.util.ArrayList; import java.util.List; VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/CountryDialog.java
@@ -18,7 +18,7 @@ import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.view.annotation.event.OnClick; import com.wx.wheelview.widget.WheelView; import cn.com.basic.face.dialog.wheelview.widget.WheelView; import java.util.ArrayList; import java.util.HashMap; VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/MyWheelView.java
New file @@ -0,0 +1,24 @@ package cn.com.basic.face.dialog; import android.content.Context; import android.util.AttributeSet; import cn.com.basic.face.dialog.wheelview.widget.WheelView; public class MyWheelView extends WheelView { public MyWheelView(Context context) { super(context); } public MyWheelView(Context context, WheelViewStyle style) { super(context, style); } public MyWheelView(Context context, AttributeSet attrs) { super(context, attrs); } public MyWheelView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/SelectDialog.java
@@ -22,8 +22,8 @@ import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.view.annotation.event.OnClick; import com.wx.wheelview.adapter.BaseWheelAdapter; import com.wx.wheelview.widget.WheelView; import cn.com.basic.face.dialog.wheelview.adapter.BaseWheelAdapter; import cn.com.basic.face.dialog.wheelview.widget.WheelView; import java.util.ArrayList; import java.util.List; VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/adapter/ArrayWheelAdapter.java
New file @@ -0,0 +1,53 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import cn.com.basic.face.dialog.wheelview.adapter.*; import cn.com.basic.face.dialog.wheelview.widget.WheelItem; /** * 滚轮数组适配器 * * @author venshine */ public class ArrayWheelAdapter<T> extends cn.com.basic.face.dialog.wheelview.adapter.BaseWheelAdapter<T> { private Context mContext; public ArrayWheelAdapter(Context context) { mContext = context; } @Override public View bindView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = new WheelItem(mContext); } WheelItem wheelItem = (WheelItem) convertView; T item = getItem(position); if (wheelItem instanceof CharSequence) { wheelItem.setText((CharSequence) item); } else { wheelItem.setText(item.toString()); } return convertView; } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/adapter/BaseWheelAdapter.java
New file @@ -0,0 +1,168 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.adapter; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import cn.com.basic.face.dialog.wheelview.util.WheelUtils; import cn.com.basic.face.dialog.wheelview.widget.IWheelView; import java.util.List; /** * 滚轮抽象数据适配器 * * @author venshine */ public abstract class BaseWheelAdapter<T> extends BaseAdapter { protected List<T> mList = null; protected boolean mLoop = IWheelView.LOOP; protected int mWheelSize = IWheelView.WHEEL_SIZE; protected boolean mClickable = IWheelView.CLICKABLE; private int mCurrentPositon = -1; protected abstract View bindView(int position, View convertView, ViewGroup parent); /** * 设置当前刻度 * * @param position */ public final void setCurrentPosition(int position) { mCurrentPositon = position; } @Override public final int getCount() { if (mLoop) { return Integer.MAX_VALUE; } return !WheelUtils.isEmpty(mList) ? (mList.size() + mWheelSize - 1) : 0; } @Override public final long getItemId(int position) { return !WheelUtils.isEmpty(mList) ? position % mList.size() : position; } @Override public final T getItem(int position) { return !WheelUtils.isEmpty(mList) ? mList.get(position % mList.size()) : null; } @Override public boolean areAllItemsEnabled() { return mClickable ? false : true; } @Override public boolean isEnabled(int position) { if (mClickable) { if (mLoop) { if (position % mList.size() == mCurrentPositon) { return true; } } else { if (position == (mCurrentPositon + mWheelSize / 2)) { return true; } } } return false; } @Override public final View getView(int position, View convertView, ViewGroup parent) { if (mLoop) { position = position % mList.size(); } else { if (position < mWheelSize / 2) { position = -1; } else if (position >= mWheelSize / 2 + mList.size()) { position = -1; } else { position = position - mWheelSize / 2; } } View view; if (position == -1) { view = bindView(0, convertView, parent); } else { view = bindView(position, convertView, parent); } if (!mLoop) { if (position == -1) { view.setVisibility(View.INVISIBLE); } else { view.setVisibility(View.VISIBLE); } } return view; } public final BaseWheelAdapter setClickable(boolean clickable) { if (clickable != mClickable) { mClickable = clickable; super.notifyDataSetChanged(); } return this; } public final BaseWheelAdapter setLoop(boolean loop) { if (loop != mLoop) { mLoop = loop; super.notifyDataSetChanged(); } return this; } public final BaseWheelAdapter setWheelSize(int wheelSize) { mWheelSize = wheelSize; super.notifyDataSetChanged(); return this; } public final BaseWheelAdapter setData(List<T> list) { mList = list; super.notifyDataSetChanged(); return this; } /** * 数据已改变,重绘可见区域 */ @Override @Deprecated public final void notifyDataSetChanged() { super.notifyDataSetChanged(); } /** * 数据失效,重绘控件 */ @Override @Deprecated public final void notifyDataSetInvalidated() { super.notifyDataSetInvalidated(); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/adapter/SimpleWheelAdapter.java
New file @@ -0,0 +1,50 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import cn.com.basic.face.dialog.wheelview.adapter.*; import cn.com.basic.face.dialog.wheelview.common.WheelData; import cn.com.basic.face.dialog.wheelview.widget.WheelItem; /** * 滚轮图片和文本适配器 * * @author venshine */ public class SimpleWheelAdapter extends cn.com.basic.face.dialog.wheelview.adapter.BaseWheelAdapter<WheelData> { private Context mContext; public SimpleWheelAdapter(Context context) { mContext = context; } @Override public View bindView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = new WheelItem(mContext); } WheelItem item = (WheelItem) convertView; item.setImage(mList.get(position).getId()); item.setText(mList.get(position).getName()); return convertView; } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/common/WheelConstants.java
New file @@ -0,0 +1,127 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.common; import android.graphics.Color; /** * 滚轮常量类 * * @author venshine */ public class WheelConstants { /** * 滚轮tag */ public static final String TAG = "com.wx.wheelview"; /** * 滚轮每一项的内边距 */ public static final int WHEEL_ITEM_PADDING = 20; /** * 滚轮每一项的内部控件外边距 */ public static final int WHEEL_ITEM_MARGIN = 20; /** * 滚轮每一项的图片TAG */ public static final int WHEEL_ITEM_IMAGE_TAG = 100; /** * 滚轮每一项的文本TAG */ public static final int WHEEL_ITEM_TEXT_TAG = 101; /** * 平滑滚动持续时间 */ public static final int WHEEL_SMOOTH_SCROLL_DURATION = 50; /** * 默认背景 */ public static final int WHEEL_BG = Color.WHITE; /** * common皮肤默认背景 */ public static final int WHEEL_SKIN_COMMON_BG = Color.parseColor("#dddddd"); /** * holo皮肤默认背景 */ public static final int WHEEL_SKIN_HOLO_BG = Color.WHITE; /** * holo皮肤默认选中框颜色 */ public static final int WHEEL_SKIN_HOLO_BORDER_COLOR = Color.parseColor("#83cde6"); /** * 滚轮item高度 */ public static final int WHEEL_ITEM_HEIGHT = 45; /** * 滚轮默认文本颜色 */ public static final int WHEEL_TEXT_COLOR = Color.BLACK; /** * 滚轮默认文本大小 */ public static final int WHEEL_TEXT_SIZE = 16; /** * 滚轮默认文本透明度 */ public static final float WHEEL_TEXT_ALPHA = 0.7f; /** * common皮肤默认选中框颜色 */ public static final int WHEEL_SKIN_COMMON_COLOR = Color.parseColor("#f0cfcfcf"); /** * common皮肤选中框divider颜色 */ public static final int WHEEL_SKIN_COMMON_DIVIDER_COLOR = Color.parseColor("#b5b5b5"); /** * common皮肤左右边框颜色 */ public static final int WHEEL_SKIN_COMMON_BORDER_COLOR = Color.parseColor("#666666"); /** * 滚轮滑动时展示选中项 */ public static final int WHEEL_SCROLL_HANDLER_WHAT = 0x0100; /** * 滚轮滑动时展示选中项延迟时间 */ public static final int WHEEL_SCROLL_DELAY_DURATION = 300; /** * dialog默认颜色 */ public static final int DIALOG_WHEEL_COLOR = WHEEL_SKIN_HOLO_BORDER_COLOR; } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/common/WheelData.java
New file @@ -0,0 +1,71 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.common; import java.io.Serializable; /** * 滚轮数据 * * @author venshine */ public class WheelData implements Serializable { private static final long serialVersionUID = 1L; /** * 资源id */ private int id; /** * 数据名称 */ private String name; public WheelData() { } public WheelData(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { final StringBuilder sb = new StringBuilder("WheelData{"); sb.append("id=").append(id); sb.append(", name='").append(name).append('\''); sb.append('}'); return sb.toString(); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/common/WheelViewException.java
New file @@ -0,0 +1,40 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.common; /** * 滚轮异常类 * * @author venshine */ public class WheelViewException extends RuntimeException { public WheelViewException() { super(); } public WheelViewException(String detailMessage) { super(detailMessage); } public WheelViewException(String detailMessage, Throwable throwable) { super(detailMessage, throwable); } public WheelViewException(Throwable throwable) { super(throwable); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/graphics/CommonDrawable.java
New file @@ -0,0 +1,101 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.graphics; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.GradientDrawable; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.graphics.*; import cn.com.basic.face.dialog.wheelview.widget.WheelView; /** * common滚轮样式 * * @author venshine */ public class CommonDrawable extends cn.com.basic.face.dialog.wheelview.graphics.WheelDrawable { private static final int[] SHADOWS_COLORS = { 0xFF111111, 0x00AAAAAA, 0x00AAAAAA }; // 阴影色值 private GradientDrawable mTopShadow = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, SHADOWS_COLORS); // 顶部阴影 private GradientDrawable mBottomShadow = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, SHADOWS_COLORS); // 底部阴影 private Paint mCommonBgPaint, mCommonPaint, mCommonDividerPaint, mCommonBorderPaint; private int mWheelSize, mItemH; public CommonDrawable(int width, int height, WheelView.WheelViewStyle style, int wheelSize, int itemH) { super(width, height, style); mWheelSize = wheelSize; mItemH = itemH; init(); } private void init() { mCommonBgPaint = new Paint(); mCommonBgPaint.setColor(mStyle.backgroundColor != -1 ? mStyle.backgroundColor : WheelConstants .WHEEL_SKIN_COMMON_BG); mCommonPaint = new Paint(); mCommonPaint.setColor(WheelConstants.WHEEL_SKIN_COMMON_COLOR); mCommonDividerPaint = new Paint(); mCommonDividerPaint.setColor(WheelConstants.WHEEL_SKIN_COMMON_DIVIDER_COLOR); mCommonDividerPaint.setStrokeWidth(2); mCommonBorderPaint = new Paint(); mCommonBorderPaint.setStrokeWidth(6); mCommonBorderPaint.setColor(WheelConstants.WHEEL_SKIN_COMMON_BORDER_COLOR); } @Override public void draw(Canvas canvas) { // draw background canvas.drawRect(0, 0, mWidth, mHeight, mCommonBgPaint); // draw select border if (mItemH != 0) { canvas.drawRect(0, mItemH * (mWheelSize / 2), mWidth, mItemH * (mWheelSize / 2 + 1), mCommonPaint); canvas.drawLine(0, mItemH * (mWheelSize / 2), mWidth, mItemH * (mWheelSize / 2), mCommonDividerPaint); canvas.drawLine(0, mItemH * (mWheelSize / 2 + 1), mWidth, mItemH * (mWheelSize / 2 + 1), mCommonDividerPaint); // top, bottom mTopShadow.setBounds(0, 0, mWidth, mItemH); mTopShadow.draw(canvas); mBottomShadow.setBounds(0, mHeight - mItemH, mWidth, mHeight); mBottomShadow.draw(canvas); // left,right canvas.drawLine(0, 0, 0, mHeight, mCommonBorderPaint); canvas.drawLine(mWidth, 0, mWidth, mHeight, mCommonBorderPaint); } } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/graphics/DrawableFactory.java
New file @@ -0,0 +1,41 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.graphics; import android.graphics.drawable.Drawable; import cn.com.basic.face.dialog.wheelview.graphics.*; import cn.com.basic.face.dialog.wheelview.graphics.HoloDrawable; import cn.com.basic.face.dialog.wheelview.graphics.WheelDrawable; import cn.com.basic.face.dialog.wheelview.widget.WheelView; /** * @author venshine */ public class DrawableFactory { public static Drawable createDrawable(WheelView.Skin skin, int width, int height, WheelView.WheelViewStyle style, int wheelSize, int itemH) { if (skin.equals(WheelView.Skin.Common)) { return new cn.com.basic.face.dialog.wheelview.graphics.CommonDrawable(width, height, style, wheelSize, itemH); } else if (skin.equals(WheelView.Skin.Holo)) { return new HoloDrawable(width, height, style, wheelSize, itemH); } else { return new WheelDrawable(width, height, style); } } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/graphics/HoloDrawable.java
New file @@ -0,0 +1,68 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.graphics; import android.graphics.Canvas; import android.graphics.Paint; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.graphics.WheelDrawable; import cn.com.basic.face.dialog.wheelview.widget.WheelView; /** * holo滚轮样式 * * @author venshine */ public class HoloDrawable extends WheelDrawable { private Paint mHoloBgPaint, mHoloPaint; private int mWheelSize, mItemH; public HoloDrawable(int width, int height, WheelView.WheelViewStyle style, int wheelSize, int itemH) { super(width, height, style); mWheelSize = wheelSize; mItemH = itemH; init(); } private void init() { mHoloBgPaint = new Paint(); mHoloBgPaint.setColor(mStyle.backgroundColor != -1 ? mStyle.backgroundColor : WheelConstants .WHEEL_SKIN_HOLO_BG); mHoloPaint = new Paint(); mHoloPaint.setStrokeWidth(mStyle.holoBorderWidth != -1 ? mStyle.holoBorderWidth : 3); mHoloPaint.setColor(mStyle.holoBorderColor != -1 ? mStyle.holoBorderColor : WheelConstants .WHEEL_SKIN_HOLO_BORDER_COLOR); } @Override public void draw(Canvas canvas) { // draw background canvas.drawRect(0, 0, mWidth, mHeight, mHoloBgPaint); // draw select border if (mItemH != 0) { canvas.drawLine(0, mItemH * (mWheelSize / 2), mWidth, mItemH * (mWheelSize / 2), mHoloPaint); canvas.drawLine(0, mItemH * (mWheelSize / 2 + 1), mWidth, mItemH * (mWheelSize / 2 + 1), mHoloPaint); } } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/graphics/WheelDrawable.java
New file @@ -0,0 +1,70 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.graphics; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.drawable.Drawable; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.widget.WheelView; /** * 滚轮样式 * * @author venshine */ public class WheelDrawable extends Drawable { protected int mWidth; // 控件宽 protected int mHeight; // 控件高 protected WheelView.WheelViewStyle mStyle; private Paint mBgPaint; public WheelDrawable(int width, int height, WheelView.WheelViewStyle style) { mWidth = width; mHeight = height; mStyle = style; init(); } private void init() { mBgPaint = new Paint(); mBgPaint.setColor(mStyle.backgroundColor != -1 ? mStyle.backgroundColor : WheelConstants.WHEEL_BG); } @Override public void draw(Canvas canvas) { canvas.drawRect(0, 0, mWidth, mHeight, mBgPaint); } @Override public void setAlpha(int alpha) { } @Override public void setColorFilter(ColorFilter colorFilter) { } @Override public int getOpacity() { return 0; } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/util/WheelUtils.java
New file @@ -0,0 +1,100 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.util; import android.content.Context; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Collection; /** * 滚轮工具类 * * @author venshine */ public class WheelUtils { private static final String TAG = "WheelView"; /** * 打印日志 * * @param msg */ public static void log(String msg) { if (!TextUtils.isEmpty(msg)) { Log.d(TAG, msg); } } /** * 判断集合是否为空 * * @param c * @param <V> * @return */ public static <V> boolean isEmpty(Collection<V> c) { return (c == null || c.size() == 0); } /** * 遍历查找TextView * * @param view * @return */ public static TextView findTextView(View view) { if (view instanceof TextView) { return (TextView) view; } else { if (view instanceof ViewGroup) { return findTextView(((ViewGroup) view).getChildAt(0)); } else { return null; } } } /** * dip转换到px * * @param context * @param dp * @return */ public static int dip2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } /** * sp转换到px * * @param context * @param sp * @return */ public static int sp2px(Context context, float sp) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (sp * fontScale + 0.5f); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/widget/IWheelView.java
New file @@ -0,0 +1,96 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.widget; import cn.com.basic.face.dialog.wheelview.adapter.BaseWheelAdapter; import cn.com.basic.face.dialog.wheelview.widget.*; import java.util.HashMap; import java.util.List; /** * 滚轮抽象接口 * * @author venshine */ public interface IWheelView<T> { /** * 滚轮数据是否循环,默认不循环 */ boolean LOOP = false; /** * 滚轮默认数量 */ int WHEEL_SIZE = 3; /** * 滚轮选中刻度是否可点击 */ boolean CLICKABLE = false; /** * 设置滚轮选中刻度是否可点击 * * @param clickable */ void setWheelClickable(boolean clickable); /** * 设置滚轮是否循环滚动 * * @param loop */ void setLoop(boolean loop); /** * 设置滚轮个数 * * @param wheelSize */ void setWheelSize(int wheelSize); /** * 设置滚轮数据 * * @param list */ void setWheelData(List<T> list); /** * 设置滚轮数据源适配器 * * @param adapter */ void setWheelAdapter(BaseWheelAdapter<T> adapter); /** * 连接副WheelView * * @param wheelView */ void join(cn.com.basic.face.dialog.wheelview.widget.WheelView wheelView); /** * 副WheelView数据 * * @param map */ void joinDatas(HashMap<String, List<T>> map); } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/widget/NestedScrollView.java
New file @@ -0,0 +1,63 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.widget; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ScrollView; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.widget.*; /** * 嵌套ScrollView * * @author venshine */ public class NestedScrollView extends ScrollView { public NestedScrollView(Context context) { super(context); init(); } public NestedScrollView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { cn.com.basic.face.dialog.wheelview.widget.WheelView wv = (cn.com.basic.face.dialog.wheelview.widget.WheelView) findViewWithTag(WheelConstants.TAG); if (wv != null) { wv.getParent().requestDisallowInterceptTouchEvent(false); } return false; } }); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/widget/WheelItem.java
New file @@ -0,0 +1,111 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.widget; import android.content.Context; import android.graphics.Color; import android.text.TextUtils; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.util.WheelUtils; /** * 滚轮Item布局,包含图片和文本 * * @author venshine */ public class WheelItem extends FrameLayout { private ImageView mImage; private TextView mText; public WheelItem(Context context) { super(context); init(); } public WheelItem(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WheelItem(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化 */ private void init() { LinearLayout layout = new LinearLayout(getContext()); LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, WheelUtils.dip2px(getContext(), WheelConstants .WHEEL_ITEM_HEIGHT)); layout.setOrientation(LinearLayout.HORIZONTAL); layout.setPadding(WheelConstants.WHEEL_ITEM_PADDING, WheelConstants.WHEEL_ITEM_PADDING, WheelConstants .WHEEL_ITEM_PADDING, WheelConstants.WHEEL_ITEM_PADDING); layout.setGravity(Gravity.CENTER); addView(layout, layoutParams); // 图片 mImage = new ImageView(getContext()); mImage.setTag(WheelConstants.WHEEL_ITEM_IMAGE_TAG); mImage.setVisibility(View.GONE); LayoutParams imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); imageParams.rightMargin = WheelConstants.WHEEL_ITEM_MARGIN; layout.addView(mImage, imageParams); // 文本 mText = new TextView(getContext()); mText.setTag(WheelConstants.WHEEL_ITEM_TEXT_TAG); mText.setEllipsize(TextUtils.TruncateAt.END); mText.setSingleLine(); mText.setIncludeFontPadding(false); mText.setGravity(Gravity.CENTER); mText.setTextColor(Color.BLACK); LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); layout.addView(mText, textParams); } /** * 设置文本 * * @param text */ public void setText(CharSequence text) { mText.setText(text); } /** * 设置图片资源 * * @param resId */ public void setImage(int resId) { mImage.setVisibility(View.VISIBLE); mImage.setImageResource(resId); } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/widget/WheelView.java
New file @@ -0,0 +1,707 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import cn.com.basic.face.dialog.wheelview.adapter.ArrayWheelAdapter; import cn.com.basic.face.dialog.wheelview.adapter.BaseWheelAdapter; import cn.com.basic.face.dialog.wheelview.adapter.SimpleWheelAdapter; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.common.WheelViewException; import cn.com.basic.face.dialog.wheelview.graphics.DrawableFactory; import cn.com.basic.face.dialog.wheelview.util.WheelUtils; import cn.com.basic.face.dialog.wheelview.widget.IWheelView; import java.util.HashMap; import java.util.List; /** * 滚轮控件 * * @author venshine */ public class WheelView<T> extends ListView implements IWheelView<T> { private int mItemH = 0; // 每一项高度 private int mWheelSize = WHEEL_SIZE; // 滚轮个数 private boolean mLoop = LOOP; // 是否循环滚动 private List<T> mList = null; // 滚轮数据列表 private int mCurrentPositon = -1; // 记录滚轮当前刻度 private String mExtraText; // 添加滚轮选中位置附加文本 private int mExtraTextColor; // 附加文本颜色 private int mExtraTextSize; // 附加文本大小 private int mExtraMargin; // 附加文本外边距 private int mSelection = 0; // 选中位置 private boolean mClickable = CLICKABLE; // 是否可点击 private Paint mTextPaint; // 附加文本画笔 private Skin mSkin = Skin.None; // 皮肤风格 private WheelViewStyle mStyle; // 滚轮样式 private WheelView mJoinWheelView; // 副WheelView private HashMap<String, List<T>> mJoinMap; // 副滚轮数据列表 private BaseWheelAdapter<T> mWheelAdapter; private OnWheelItemSelectedListener<T> mOnWheelItemSelectedListener; private OnWheelItemClickListener<T> mOnWheelItemClickListener; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == WheelConstants.WHEEL_SCROLL_HANDLER_WHAT) { if (mOnWheelItemSelectedListener != null) { mOnWheelItemSelectedListener.onItemSelected (getCurrentPosition(), getSelectionItem()); } if (mJoinWheelView != null) { if (!mJoinMap.isEmpty()) { mJoinWheelView.resetDataFromTop(mJoinMap.get(mList.get (getCurrentPosition())) ); } else { throw new WheelViewException("JoinList is error."); } } } } }; private OnItemClickListener mOnItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mOnWheelItemClickListener != null) { mOnWheelItemClickListener.onItemClick(getCurrentPosition(), getSelectionItem()); } } }; private OnTouchListener mTouchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.getParent().requestDisallowInterceptTouchEvent(true); return false; } }; private OnScrollListener mOnScrollListener = new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { View itemView = getChildAt(0); if (itemView != null) { float deltaY = itemView.getY(); if (deltaY == 0 || mItemH == 0) { return; } if (Math.abs(deltaY) < mItemH / 2) { int d = getSmoothDistance(deltaY); smoothScrollBy(d, WheelConstants .WHEEL_SMOOTH_SCROLL_DURATION); } else { int d = getSmoothDistance(mItemH + deltaY); smoothScrollBy(d, WheelConstants .WHEEL_SMOOTH_SCROLL_DURATION); } } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (visibleItemCount != 0) { refreshCurrentPosition(false); } } }; public WheelView(Context context) { super(context); init(); } public WheelView(Context context, WheelViewStyle style) { super(context); setStyle(style); init(); } public WheelView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WheelView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 设置滚轮滑动停止时事件,监听滚轮选中项 * * @param onWheelItemSelectedListener */ public void setOnWheelItemSelectedListener(OnWheelItemSelectedListener<T> onWheelItemSelectedListener) { mOnWheelItemSelectedListener = onWheelItemSelectedListener; } /** * 设置滚轮选中项点击事件 * * @param onWheelItemClickListener */ public void setOnWheelItemClickListener(OnWheelItemClickListener<T> onWheelItemClickListener) { mOnWheelItemClickListener = onWheelItemClickListener; } /** * 初始化 */ private void init() { if (mStyle == null) { mStyle = new WheelViewStyle(); } mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); setTag(WheelConstants.TAG); setVerticalScrollBarEnabled(false); setScrollingCacheEnabled(false); setCacheColorHint(Color.TRANSPARENT); setFadingEdgeLength(0); setOverScrollMode(OVER_SCROLL_NEVER); setDividerHeight(0); setOnItemClickListener(mOnItemClickListener); setOnScrollListener(mOnScrollListener); setOnTouchListener(mTouchListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setNestedScrollingEnabled(true); } addOnGlobalLayoutListener(); } private void addOnGlobalLayoutListener() { getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver .OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { getViewTreeObserver().removeGlobalOnLayoutListener(this); } if (getChildCount() > 0 && mItemH == 0) { mItemH = getChildAt(0).getHeight(); if (mItemH != 0) { ViewGroup.LayoutParams params = getLayoutParams(); params.height = mItemH * mWheelSize; refreshVisibleItems(getFirstVisiblePosition(), getCurrentPosition() + mWheelSize / 2, mWheelSize / 2); setBackground(); } else { throw new WheelViewException("wheel item is error."); } } } }); } /** * 获得滚轮样式 * * @return */ public WheelViewStyle getStyle() { return mStyle; } /** * 设置滚轮样式 * * @param style */ public void setStyle(WheelViewStyle style) { mStyle = style; } /** * 设置背景 */ private void setBackground() { Drawable drawable = DrawableFactory.createDrawable(mSkin, getWidth(), mItemH * mWheelSize, mStyle, mWheelSize, mItemH); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(drawable); } else { setBackgroundDrawable(drawable); } } /** * 获得皮肤风格 * * @return */ public Skin getSkin() { return mSkin; } /** * 设置皮肤风格 * * @param skin */ public void setSkin(Skin skin) { mSkin = skin; } /** * 设置滚轮个数 * * @param wheelSize */ @Override public void setWheelSize(int wheelSize) { if ((wheelSize & 1) == 0) { throw new WheelViewException("wheel size must be an odd number."); } mWheelSize = wheelSize; if (mWheelAdapter != null) { mWheelAdapter.setWheelSize(wheelSize); } } /** * 设置滚轮是否循环滚动 * * @param loop */ @Override public void setLoop(boolean loop) { if (loop != mLoop) { mLoop = loop; setSelection(0); if (mWheelAdapter != null) { mWheelAdapter.setLoop(loop); } } } /** * 设置滚轮选中项是否可点击 * * @param clickable */ @Override public void setWheelClickable(boolean clickable) { if (clickable != mClickable) { mClickable = clickable; if (mWheelAdapter != null) { mWheelAdapter.setClickable(clickable); } } } /** * 重置数据 * * @param list */ public void resetDataFromTop(final List<T> list) { if (WheelUtils.isEmpty(list)) { throw new WheelViewException("join map data is error."); } WheelView.this.postDelayed(new Runnable() { @Override public void run() { setWheelData(list); WheelView.super.setSelection(0); refreshCurrentPosition(true); } }, 10); } /** * 获取滚轮位置 * * @return */ public int getSelection() { return mSelection; } /** * 设置滚轮位置 * * @param selection */ @Override public void setSelection(final int selection) { mSelection = selection; setVisibility(View.INVISIBLE); WheelView.this.postDelayed(new Runnable() { @Override public void run() { WheelView.super.setSelection(getRealPosition(selection)); refreshCurrentPosition(false); setVisibility(View.VISIBLE); } }, 500); } /** * 连接副WheelView * * @param wheelView */ @Override public void join(WheelView wheelView) { if (wheelView == null) { throw new WheelViewException("wheelview cannot be null."); } mJoinWheelView = wheelView; } /** * 副WheelView数据 * * @param map */ public void joinDatas(HashMap<String, List<T>> map) { mJoinMap = map; } /** * 获得滚轮当前真实位置 * * @param positon * @return */ private int getRealPosition(int positon) { if (WheelUtils.isEmpty(mList)) { return 0; } if (mLoop) { int d = Integer.MAX_VALUE / 2 / mList.size(); return positon + d * mList.size() - mWheelSize / 2; } return positon; } /** * 获取当前滚轮位置 * * @return */ public int getCurrentPosition() { return mCurrentPositon; } /** * 获取当前滚轮位置的数据 * * @return */ public T getSelectionItem() { int position = getCurrentPosition(); position = position < 0 ? 0 : position; if (mList != null && mList.size() > position) { return mList.get(position); } return null; } /** * 设置滚轮数据适配器,已弃用,具体使用{@link #setWheelAdapter(BaseWheelAdapter)} * * @param adapter */ @Override @Deprecated public void setAdapter(ListAdapter adapter) { if (adapter != null && adapter instanceof BaseWheelAdapter) { setWheelAdapter((BaseWheelAdapter) adapter); } else { throw new WheelViewException("please invoke setWheelAdapter " + "method."); } } /** * 设置滚轮数据源适配器 * * @param adapter */ @Override public void setWheelAdapter(BaseWheelAdapter<T> adapter) { super.setAdapter(adapter); mWheelAdapter = adapter; mWheelAdapter.setData(mList).setWheelSize(mWheelSize).setLoop(mLoop).setClickable(mClickable); } /** * 设置滚轮数据 * * @param list */ @Override public void setWheelData(List<T> list) { if (WheelUtils.isEmpty(list)) { throw new WheelViewException("wheel datas are error."); } mList = list; if (mWheelAdapter != null) { mWheelAdapter.setData(list); } } /** * 设置选中行附加文本 * * @param text * @param textColor * @param textSize * @param margin */ public void setExtraText(String text, int textColor, int textSize, int margin) { mExtraText = text; mExtraTextColor = textColor; mExtraTextSize = textSize; mExtraMargin = margin; } /** * 获得滚轮数据总数 * * @return */ public int getWheelCount() { return !WheelUtils.isEmpty(mList) ? mList.size() : 0; } /** * 平滑的滚动距离 * * @param scrollDistance * @return */ private int getSmoothDistance(float scrollDistance) { if (Math.abs(scrollDistance) <= 2) { return (int) scrollDistance; } else if (Math.abs(scrollDistance) < 12) { return scrollDistance > 0 ? 2 : -2; } else { return (int) (scrollDistance / 6); // 减缓平滑滑动速率 } } /** * 刷新当前位置 * * @param join */ private void refreshCurrentPosition(boolean join) { if (getChildAt(0) == null || mItemH == 0) { return; } int firstPosition = getFirstVisiblePosition(); if (mLoop && firstPosition == 0) { return; } int position = 0; if (Math.abs(getChildAt(0).getY()) <= mItemH / 2) { position = firstPosition; } else { position = firstPosition + 1; } refreshVisibleItems(firstPosition, position + mWheelSize / 2, mWheelSize / 2); if (mLoop) { position = (position + mWheelSize / 2) % getWheelCount(); } if (position == mCurrentPositon && !join) { return; } mCurrentPositon = position; mWheelAdapter.setCurrentPosition(position); mHandler.removeMessages(WheelConstants.WHEEL_SCROLL_HANDLER_WHAT); mHandler.sendEmptyMessageDelayed(WheelConstants .WHEEL_SCROLL_HANDLER_WHAT, WheelConstants .WHEEL_SCROLL_DELAY_DURATION); } /** * 刷新可见滚动列表 * * @param firstPosition * @param curPosition * @param offset */ private void refreshVisibleItems(int firstPosition, int curPosition, int offset) { for (int i = curPosition - offset; i <= curPosition + offset; i++) { View itemView = getChildAt(i - firstPosition); if (itemView == null) { continue; } if (mWheelAdapter instanceof ArrayWheelAdapter || mWheelAdapter instanceof SimpleWheelAdapter) { TextView textView = (TextView) itemView.findViewWithTag (WheelConstants.WHEEL_ITEM_TEXT_TAG); refreshTextView(i, curPosition, itemView, textView); } else { // 自定义类型 TextView textView = WheelUtils.findTextView(itemView); if (textView != null) { refreshTextView(i, curPosition, itemView, textView); } } } } /** * 刷新文本 * * @param position * @param curPosition * @param itemView * @param textView */ private void refreshTextView(int position, int curPosition, View itemView, TextView textView) { if (curPosition == position) { // 选中 int textColor = mStyle.selectedTextColor != -1 ? mStyle .selectedTextColor : (mStyle.textColor != -1 ? mStyle .textColor : WheelConstants.WHEEL_TEXT_COLOR); float defTextSize = mStyle.textSize != -1 ? mStyle.textSize : WheelConstants.WHEEL_TEXT_SIZE; float textSize = mStyle.selectedTextSize != -1 ? mStyle .selectedTextSize : (mStyle.selectedTextZoom != -1 ? (defTextSize * mStyle.selectedTextZoom) : defTextSize); setTextView(itemView, textView, textColor, textSize, 1.0f); } else { // 未选中 int textColor = mStyle.textColor != -1 ? mStyle.textColor : WheelConstants.WHEEL_TEXT_COLOR; float textSize = mStyle.textSize != -1 ? mStyle.textSize : WheelConstants.WHEEL_TEXT_SIZE; int delta = Math.abs(position - curPosition); float alpha = (float) Math.pow(mStyle.textAlpha != -1 ? mStyle.textAlpha : WheelConstants .WHEEL_TEXT_ALPHA, delta); setTextView(itemView, textView, textColor, textSize, alpha); } } /** * 设置TextView * * @param itemView * @param textView * @param textColor * @param textSize * @param textAlpha */ private void setTextView(View itemView, TextView textView, int textColor, float textSize, float textAlpha) { textView.setTextColor(textColor); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); itemView.setAlpha(textAlpha); } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (!TextUtils.isEmpty(mExtraText)) { Rect targetRect = new Rect(0, mItemH * (mWheelSize / 2), getWidth (), mItemH * (mWheelSize / 2 + 1)); mTextPaint.setTextSize(mExtraTextSize); mTextPaint.setColor(mExtraTextColor); Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt(); int baseline = (targetRect.bottom + targetRect.top - fontMetrics .bottom - fontMetrics.top) / 2; mTextPaint.setTextAlign(Paint.Align.CENTER); canvas.drawText(mExtraText, targetRect.centerX() + mExtraMargin, baseline, mTextPaint); } } public enum Skin { // 滚轮皮肤 Common, Holo, None } public interface OnWheelItemSelectedListener<T> { void onItemSelected(int position, T t); } public interface OnWheelItemClickListener<T> { void onItemClick(int position, T t); } public static class WheelViewStyle { public int backgroundColor = -1; // 背景颜色 public int holoBorderColor = -1; // holo样式边框颜色 public int holoBorderWidth = -1;//holo样式边框宽度 public int textColor = -1; // 文本颜色 public int selectedTextColor = -1; // 选中文本颜色 public int textSize = -1;// 文本大小 public int selectedTextSize = -1; // 选中文本大小 public float textAlpha = -1; // 文本透明度(0f ~ 1f) public float selectedTextZoom = -1; // 选中文本放大倍数 public WheelViewStyle() { } public WheelViewStyle(WheelViewStyle style) { this.backgroundColor = style.backgroundColor; this.holoBorderColor = style.holoBorderColor; this.holoBorderWidth = style.holoBorderWidth; this.textColor = style.textColor; this.selectedTextColor = style.selectedTextColor; this.textSize = style.textSize; this.selectedTextSize = style.selectedTextSize; this.textAlpha = style.textAlpha; this.selectedTextZoom = style.selectedTextZoom; } } } VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/dialog/wheelview/widget/WheelViewDialog.java
New file @@ -0,0 +1,302 @@ /* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.basic.face.dialog.wheelview.widget; import android.app.AlertDialog; import android.content.Context; import android.graphics.Color; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import cn.com.basic.face.dialog.wheelview.adapter.ArrayWheelAdapter; import cn.com.basic.face.dialog.wheelview.common.WheelConstants; import cn.com.basic.face.dialog.wheelview.util.WheelUtils; import cn.com.basic.face.dialog.wheelview.widget.*; import java.util.Arrays; import java.util.List; /** * 滚轮对话框 * * @author venshine */ public class WheelViewDialog<T> implements View.OnClickListener { private TextView mTitle; private View mLine1, mLine2; private cn.com.basic.face.dialog.wheelview.widget.WheelView<T> mWheelView; private cn.com.basic.face.dialog.wheelview.widget.WheelView.WheelViewStyle mStyle; private TextView mButton; private AlertDialog mDialog; private Context mContext; private OnDialogItemClickListener mOnDialogItemClickListener; private int mSelectedPos; private T mSelectedText; public WheelViewDialog(Context context) { mContext = context; init(); } private void init() { LinearLayout layout = new LinearLayout(mContext); layout.setOrientation(LinearLayout.VERTICAL); layout.setPadding(WheelUtils.dip2px(mContext, 20), 0, WheelUtils.dip2px(mContext, 20), 0); mTitle = new TextView(mContext); mTitle.setTextColor(WheelConstants.DIALOG_WHEEL_COLOR); mTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); mTitle.setGravity(Gravity.CENTER); LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, WheelUtils.dip2px(mContext, 50)); layout.addView(mTitle, titleParams); mLine1 = new View(mContext); mLine1.setBackgroundColor(WheelConstants.DIALOG_WHEEL_COLOR); LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, WheelUtils.dip2px(mContext, 2)); layout.addView(mLine1, lineParams); mWheelView = new cn.com.basic.face.dialog.wheelview.widget.WheelView(mContext); mWheelView.setSkin(cn.com.basic.face.dialog.wheelview.widget.WheelView.Skin.Holo); mWheelView.setWheelAdapter(new ArrayWheelAdapter(mContext)); mStyle = new cn.com.basic.face.dialog.wheelview.widget.WheelView.WheelViewStyle(); mStyle.textColor = Color.GRAY; mStyle.selectedTextZoom = 1.2f; mWheelView.setStyle(mStyle); mWheelView.setOnWheelItemSelectedListener(new cn.com.basic.face.dialog.wheelview.widget.WheelView.OnWheelItemSelectedListener<T>() { @Override public void onItemSelected(int position, T text) { mSelectedPos = position; mSelectedText = text; } }); ViewGroup.MarginLayoutParams wheelParams = new ViewGroup.MarginLayoutParams(LinearLayout.LayoutParams .MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.addView(mWheelView, wheelParams); mLine2 = new View(mContext); mLine2.setBackgroundColor(WheelConstants.DIALOG_WHEEL_COLOR); LinearLayout.LayoutParams line2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, WheelUtils.dip2px(mContext, 1f)); layout.addView(mLine2, line2Params); mButton = new TextView(mContext); mButton.setTextColor(WheelConstants.DIALOG_WHEEL_COLOR); mButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); mButton.setGravity(Gravity.CENTER); mButton.setClickable(true); mButton.setOnClickListener(this); mButton.setText("OK"); LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, WheelUtils.dip2px(mContext, 45)); layout.addView(mButton, buttonParams); mDialog = new AlertDialog.Builder(mContext).create(); mDialog.setView(layout); mDialog.setCanceledOnTouchOutside(false); } /** * 点击事件 * * @param onDialogItemClickListener * @return */ public WheelViewDialog setOnDialogItemClickListener(OnDialogItemClickListener onDialogItemClickListener) { mOnDialogItemClickListener = onDialogItemClickListener; return this; } /** * 设置dialog外观颜色 * * @param color * @return */ public WheelViewDialog setDialogStyle(int color) { mTitle.setTextColor(color); mLine1.setBackgroundColor(color); mLine2.setBackgroundColor(color); mButton.setTextColor(color); mStyle.selectedTextColor = color; mStyle.holoBorderColor = color; return this; } /** * 设置标题 * * @param title * @return */ public WheelViewDialog setTitle(String title) { mTitle.setText(title); return this; } /** * 设置标题颜色 * * @param color * @return */ public WheelViewDialog setTextColor(int color) { mTitle.setTextColor(color); return this; } /** * 设置标题大小 * * @param size * @return */ public WheelViewDialog setTextSize(int size) { mTitle.setTextSize(size); return this; } /** * 设置按钮文本 * * @param text * @return */ public WheelViewDialog setButtonText(String text) { mButton.setText(text); return this; } /** * 设置按钮文本颜色 * * @param color * @return */ public WheelViewDialog setButtonColor(int color) { mButton.setTextColor(color); return this; } /** * 设置按钮文本尺寸 * * @param size * @return */ public WheelViewDialog setButtonSize(int size) { mButton.setTextSize(size); return this; } /** * 设置数据项显示个数 * * @param count */ public WheelViewDialog setCount(int count) { mWheelView.setWheelSize(count); return this; } /** * 数据项是否循环显示 * * @param loop */ public WheelViewDialog setLoop(boolean loop) { mWheelView.setLoop(loop); return this; } /** * 设置数据项显示位置 * * @param selection */ public WheelViewDialog setSelection(int selection) { mWheelView.setSelection(selection); return this; } /** * 设置数据项 * * @param arrays */ public WheelViewDialog setItems(T[] arrays) { return setItems(Arrays.asList(arrays)); } /** * 设置数据项 * * @param list */ public WheelViewDialog setItems(List<T> list) { mWheelView.setWheelData(list); return this; } /** * 显示 */ public WheelViewDialog show() { if (!mDialog.isShowing()) { mDialog.show(); } return this; } /** * 隐藏 */ public WheelViewDialog dismiss() { if (mDialog.isShowing()) { mDialog.dismiss(); } return this; } @Override public void onClick(View v) { dismiss(); if (null != mOnDialogItemClickListener) { mOnDialogItemClickListener.onItemClick(mSelectedPos, mSelectedText); } } public interface OnDialogItemClickListener<T> { void onItemClick(int position, T s); } } VisitFace/DemoForBsk/app/src/main/res/layout/dialog_add.xml
@@ -18,13 +18,13 @@ android:padding="@dimen/w20dp" android:gravity="center" android:textColor="@android:color/white" android:textSize="@dimen/text_size_big" android:textSize="@dimen/w16dp" android:background="@color/colorPrimary"/> <EditText android:id="@+id/dialog_add_content" android:layout_width="@dimen/w200dp" android:layout_height="wrap_content" android:textSize="@dimen/text_size_big" android:textSize="@dimen/w16dp" android:textColor="@color/colorText_5" android:padding="@dimen/w22dp" android:gravity="center" VisitFace/DemoForBsk/app/src/main/res/layout/dialog_country.xml
@@ -18,7 +18,7 @@ android:gravity="center" android:text="@string/country" android:textColor="@android:color/white" android:textSize="@dimen/text_size_big" android:textSize="@dimen/w16dp" android:background="@color/colorPrimary"/> <LinearLayout android:layout_width="@dimen/w300dp" @@ -26,12 +26,12 @@ android:paddingLeft="@dimen/w10dp" android:paddingRight="@dimen/w10dp" android:orientation="horizontal"> <com.wx.wheelview.widget.WheelView <cn.com.basic.face.dialog.wheelview.widget.WheelView android:id="@+id/dialog_country_name_first_letter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> <com.wx.wheelview.widget.WheelView <cn.com.basic.face.dialog.wheelview.widget.WheelView android:id="@+id/dialog_country_name" android:layout_width="wrap_content" android:layout_height="wrap_content" @@ -53,7 +53,7 @@ android:layout_height="match_parent" android:gravity="center" android:text="@string/confirm" android:textSize="@dimen/text_size_big"/> android:textSize="@dimen/w16dp"/> <View android:layout_width="0.5dp" android:layout_height="match_parent" @@ -65,7 +65,7 @@ android:layout_height="match_parent" android:gravity="center" android:text="@string/cancel" android:textSize="@dimen/text_size_big"/> android:textSize="@dimen/w16dp"/> </LinearLayout> </LinearLayout> </LinearLayout> VisitFace/DemoForBsk/app/src/main/res/layout/dialog_date_select.xml
@@ -27,7 +27,9 @@ android:layout_height="@dimen/h50dp" android:gravity="center" android:text="@string/confirm" android:textColor="#000000" /> android:textColor="#000000" android:textSize="@dimen/w16dp" /> <TextView android:id="@+id/dialog_date_select_cancel" android:layout_weight="1" @@ -35,6 +37,8 @@ android:layout_height="@dimen/h50dp" android:gravity="center" android:text="@string/cancel" android:textColor="#000000" /> android:textColor="#000000" android:textSize="@dimen/w16dp" /> </LinearLayout> </LinearLayout> VisitFace/DemoForBsk/app/src/main/res/layout/dialog_row.xml
@@ -10,5 +10,5 @@ android:layout_margin="@dimen/w8dp" android:gravity="center" android:singleLine="true" android:textSize="@dimen/text_size_big"/> android:textSize="@dimen/w16dp"/> </RelativeLayout> VisitFace/DemoForBsk/app/src/main/res/layout/dialog_select.xml
@@ -18,12 +18,13 @@ android:padding="@dimen/w20dp" android:gravity="center" android:textColor="@android:color/white" android:textSize="@dimen/text_size_big" android:textSize="@dimen/w16dp" android:background="@color/colorPrimary"/> <com.wx.wheelview.widget.WheelView <cn.com.basic.face.dialog.wheelview.widget.WheelView android:id="@+id/dialog_select_list_view" android:layout_width="@dimen/w200dp" android:layout_height="wrap_content"/> android:layout_height="wrap_content" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" @@ -39,7 +40,7 @@ android:layout_height="match_parent" android:gravity="center" android:text="@string/confirm" android:textSize="@dimen/text_size_big"/> android:textSize="@dimen/w16dp"/> <View android:id="@+id/dialog_select_add_separator" android:layout_width="0.5dp" @@ -54,7 +55,7 @@ android:layout_height="match_parent" android:gravity="center" android:text="@string/add" android:textSize="@dimen/text_size_big" android:textSize="@dimen/w16dp" android:visibility="gone" /> <View @@ -68,7 +69,7 @@ android:layout_height="match_parent" android:gravity="center" android:text="@string/cancel" android:textSize="@dimen/text_size_big"/> android:textSize="@dimen/w16dp"/> </LinearLayout> </LinearLayout> </LinearLayout>