您好,登錄后才能下訂單哦!
public class Constants { public static String[] province = new String[] { "北京市", "天津市", "河北省", "山西省", "內(nèi)蒙古自治區(qū)", "遼寧省", "吉林省", "黑龍江省", "上海市", "江蘇省", "浙江省", "安徽省", "福建省", "江西省", "山東省", "河南省", "湖北省", "湖南省", "廣東省", "廣西壯族自治區(qū)", "海南省", "重慶市", "四川省", "貴州省", "云南省", "西藏自治區(qū)", "陜西省", "甘肅省", "青海省", "寧夏回族自治區(qū)", "新疆維吾爾自治區(qū)", "香港", "澳門", "臺灣" }; public static String[][] pandc = new String[][] { { "東城區(qū)", "西城區(qū)", "崇文區(qū)", "宣武區(qū)", "朝陽區(qū)", "豐臺區(qū)", "石景山區(qū)", "海淀區(qū)", "石頭溝區(qū)", "房山區(qū)", "通州區(qū)", "順義區(qū)", "昌平區(qū)", "大興區(qū)", "懷柔區(qū)", "平谷區(qū)", "密云區(qū)", "延慶區(qū)" }, { "和平區(qū)", "河?xùn)|區(qū)", "河西區(qū)", "南開區(qū)", "河北區(qū)", "紅橋區(qū)", "塘沽區(qū)", "漢沽區(qū)", "大港區(qū)", "東麗區(qū)", "西青區(qū)", "津南區(qū)", "北辰區(qū)", "武清區(qū)", "寶坻區(qū)", "寧河縣", "靜海縣", "薊縣" }, { "石家莊市", "唐山市", "秦皇島市", "邯鄲市", "邢臺市", "保定市", "張家口市", "承德市", "滄州市", "廊坊市", "衡水市" }, { "太原市", "大同市", "陽泉市", "長治市", "晉城市", "朔州市", "晉中市", "運城市", "忻州市", "臨汾市", "呂梁市" }, { "呼和浩特市", "包頭市", "烏海市", "赤峰市", "通遼市", "鄂爾多斯市", "呼倫貝爾市", "巴彥淖爾市", "烏蘭察布市", "興安盟", "錫林郭勒市", "阿拉善盟" }, { "沈陽市", "大連市", "鞍山市", "撫順市", "本溪市", "丹東市", "錦州市", "營口市", "阜新市", "遼陽市", "盤錦市", "鐵嶺市", "朝陽市", "葫蘆島市" }, { "長春市", "吉林市", "四平市", "遼源市", "白山市", "松原市", "白城市", "延邊州" }, { "哈爾濱市", "齊齊哈爾市", "雞西市", "鶴崗市", "雙鴨山市", "大慶市", "伊春市", "佳木斯市", "七臺河市", "牡丹江市", "黑河市", "綏化市", "大興安嶺地區(qū)" }, { "黃浦區(qū)", "盧灣區(qū)", "徐匯區(qū)", "長寧區(qū)", "靜安區(qū)", "普陀區(qū)", "閘北區(qū)", "虹口區(qū)", "楊浦區(qū)", "閔行區(qū)", "寶山區(qū)", "嘉定區(qū)", "浦東新區(qū)", "金山區(qū)", "松江區(qū)", "青浦區(qū)", "南匯區(qū)", "奉賢區(qū)", "崇明縣" }, { "南京市", "無錫市", "徐州市", "常州市", "蘇州市", "南通市", "連云港市", "淮安市", "鹽城市", "揚州市", "鎮(zhèn)江市", "泰州市", "宿遷市" }, { "杭州市", "寧波市", "溫州市", "嘉興市", "湖州市", "紹興市", "金華市", "衢州市", "舟山市", "臺州市", "麗水市" }, { "合肥市", "蕪湖市", "蚌埠市", "淮南市", "銅陵市", "安慶市", "黃山市", "滁州市", "阜陽市", "宿州市", "巢湖市", "六安市", "亳州市", "池州市", "宣城市" }, { "福州市", "廈門市", "莆田市", "三明市", "泉州市", "漳州市", "南平市", "龍巖市", "寧德市" }, { "南昌市", "景德鎮(zhèn)市", "萍鄉(xiāng)市", "九江市", "新余市", "鷹潭市", "贛州市", "吉安市", "宜春市", "撫州市", "上饒市" }, { "濟南市", "青島市", "淄博市", "棗莊市", "東營市", "煙臺市", "廊坊市", "濟寧市", "泰安市", "威海市", "照市", "萊蕪市", "臨沂市", "德州市", "聊城市", "濱州市", "菏澤市" }, { "鄭州市", "開封市", "洛陽市", "平頂山市", "安陽市", "鶴壁市", "新鄉(xiāng)市", "焦作市", "濮陽市", "許昌市", "漯河市", "三峽門市", "南陽市", "南丘市", "信陽市", "周口市", "駐馬店市" }, { "武漢市", "黃石市", "十堰市", "宜昌市", "襄樊市", "鄂州市", "荊門市", "孝感市", "荊州市", "黃岡市", "咸寧市", "隨州市", "恩施市", "仙桃市", "潛江市", "天門市", "神農(nóng)架林區(qū)" }, { "長沙市", "株洲市", "湘潭市", "衡陽市", "邵陽市", "岳陽市", "常德市", "張家界市", "益陽市", "郴州市", "永州市", "懷化市", "婁底市", "湘西市" }, { "廣州市", "韶關(guān)市", "深圳市", "珠海市", "汕頭市", "佛山市", "江門市", "湛江市", "茂名市", "肇慶市", "惠州市", "梅州市", "汕尾市", "河源市", "陽江市", "清遠市", "東莞市", "中山市", "潮州市", "揭陽市", "云浮市" }, { "南寧市", "柳州市", "桂林市", "梧州市", "北海市", "防城港市", "欽州市", "貴港市", "玉林市", "百色市", "賀州市", "河池市", "來賓市", "崇左市" }, { "??谑?, "三亞市", "五指山市", "瓊海市", "儋州市", "文昌市", "萬寧市", "東方市", "安定縣", "屯昌縣", "澄邁縣", "臨高縣", "白沙縣", "昌江縣", "樂東縣", "陵水縣", "保亭縣", "瓊中縣", "西沙群島", "南沙群島", "中沙群島" }, { "萬州區(qū)", "涪陵區(qū)", "渝中區(qū)", "大渡口區(qū)", "江北區(qū)", "沙坪壩區(qū)", "九龍坡區(qū)", "南岸區(qū)", "北碚區(qū)", "萬盛區(qū)", "雙橋區(qū)", "渝北區(qū)", "巴南區(qū)", "黔江區(qū)", "長壽區(qū)", "江津區(qū)", "合川區(qū)", "永川區(qū)", "南川區(qū)", "綦江縣", "潼南縣", "銅梁縣", "大足縣", "榮昌縣", "璧山縣", "梁平縣", "城口縣", "豐都縣", "墊江縣", "武隆縣", "忠縣", "開縣", "云陽縣", "奉節(jié)縣", "巫山縣", "巫溪縣", "石柱縣", "秀山縣" }, { "成都市", "自貢市", "攀枝花市", "瀘州市", "德陽市", "綿陽市", "廣元市", "遂寧市", "內(nèi)江市", "樂山市", "南充市", "眉山市", "宜賓市", "廣安市", "達州市", "雅安市", "巴中市", "資陽市", "阿壩州", "甘孜州", "涼山州" }, { "貴陽市", "六盤水市", "遵義市", "安順市", "銅仁地區(qū)", "黔西南州", "畢節(jié)地區(qū)", "黔東南州", "黔南州" }, { "昆明市", "曲靖市", "玉溪市", "保山市", "昭通市", "麗江市", "普洱市", "臨滄市", "文山州", "紅河州", "西雙版納州", "楚雄州", "大理州", "德宏州", "怒江州", "迪慶州" }, { "拉薩市", "昌都地區(qū)", "山南地區(qū)", "喀則地區(qū)", "那曲地區(qū)", "阿里地區(qū)", "林芝地區(qū)" }, { "西安市", "銅川市", "寶雞市", "咸陽市", "渭南市", "延安市", "漢中市", "榆林市", "安康市", "南洛市" }, { "蘭州市", "嘉峪關(guān)市", "金昌市", "白銀市", "天水市", "武威市", "張掖市", "平?jīng)鍪?, "酒泉市", "慶陽市", "定西市", "隴南市", "臨夏州", "甘南州" }, { "西寧市", "海東地區(qū)", "海北州", "黃南州", "海南州", "果洛州", "玉樹州", "海西州" }, { "銀川市", "石嘴山市", "吳忠市", "固原市", "中衛(wèi)市" }, { "烏魯木齊市", "克拉瑪依市", "吐魯番地區(qū)", "哈密地區(qū)", "昌吉州", "博爾塔拉州", "巴音郭楞州", "阿克蘇地區(qū)", "克孜勒蘇州", "喀什地區(qū)", "和田地區(qū)", "伊犁州", "塔城地區(qū)", "阿勒泰地區(qū)", "石河子市", "阿拉爾市", "圖木舒克", "五家渠市" }, { "香港" }, { "澳門" }, { "臺灣" } }; }
public class MainActivity extends Activity { PickerView minute_pv; PickerView second_pv; List<String> province = new ArrayList<String>(); List<String> province0 = new ArrayList<String>(); ArrayList<ArrayList<String>> cityList = new ArrayList<ArrayList<String>>(); String provinceString = "湖南省"; String cityString = "張家界市"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); minute_pv = (PickerView) findViewById(R.id.minute_pv); second_pv = (PickerView) findViewById(R.id.second_pv); Collections.addAll(province, Constants.province); Collections.addAll(province0, Constants.province); for (int i = 0; i < Constants.pandc.length; i++) { ArrayList<String> list2 = new ArrayList<String>(); for (int j = 0; j < Constants.pandc[i].length; j++) { list2.add(Constants.pandc[i][j]); } cityList.add(list2); } minute_pv.setData(province0); minute_pv.setOnSelectListener(new onSelectListener() { @Override public void onSelect(String text) { provinceString = text; for (int i = 0; i < province.size(); i++) { if (text.equals(province.get(i))) { second_pv.setData(cityList.get(i)); } } } }); for (int i = 0; i < province.size(); i++) { if ("湖南省".equals(province.get(i))) { second_pv.setData(cityList.get(i)); } } second_pv.setOnSelectListener(new onSelectListener() { @Override public void onSelect(String text) { cityString = text; Toast.makeText(MainActivity.this, "選擇了 " + provinceString + cityString, Toast.LENGTH_SHORT).show(); } }); }
public class PickerView extends View { public static final String TAG = "PickerView"; /** * text之間間距和minTextSize之比 */ public static final float MARGIN_ALPHA = 2.5f; /** * 自動回滾到中間的速度 */ public static final float SPEED = 2; private List<String> mDataList; /** * 選中的位置,這個位置是mDataList的中心位置,一直不變 */ private int mCurrentSelected; private Paint mPaint; private float mMaxTextSize = 50; 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的正負(fù)號,以實現(xiàn)上滾或下滾 mMoveLen = mMoveLen - mMoveLen / Math.abs(mMoveLen) * SPEED; invalidate(); } }; public PickerView(Context context) { super(context); init(); } public PickerView(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(); isInit = true; invalidate(); } private void init() { timer = new Timer(); mDataList = new ArrayList<String>(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Style.FILL); mPaint.setTextAlign(Align.CENTER); mPaint.setColor(mColorText); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 根據(jù)index繪制view if (isInit) drawData(canvas); } private void drawData(Canvas canvas) { mPaint.setColor(Color.GRAY); // 設(shè)置畫筆顏色 mPaint.setStrokeWidth((float) 0.7); // 設(shè)置線寬 canvas.drawLine(0, mViewHeight/2 - 50, mViewWidth, mViewHeight/2 - 50, mPaint); canvas.drawLine(0, mViewHeight/2 + 50, mViewWidth, mViewHeight/2 + 50, mPaint); // 先繪制選中的text再往上往下繪制其余的text float scale = parabola(mViewHeight / 4.0f, mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setColor(Color.parseColor("#FF7F28")); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中繪制,注意baseline的計算才能達到居中,y值是text中心坐標(biāo) float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); 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.setColor(Color.parseColor("#000000")); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); 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 * 零點坐標(biāo) * @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的位置由當(dāng)前位置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); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。