Android實(shí)現(xiàn)動(dòng)畫效果的自定義下拉菜單功能
我們?cè)谫?gòu)物APP里面設(shè)置收貨地址時(shí),都會(huì)有讓我們選擇省份及城市的下拉菜單項(xiàng)。今天我將使用Android原生的 Spinner 控件來(lái)實(shí)現(xiàn)一個(gè)自定義的下拉菜單功能,并配上一個(gè)透明漸變動(dòng)畫效果。
要實(shí)現(xiàn)的功能及思路如下:
下拉菜單樣式是自定義的、非原生效果:需要使用 setDropDownViewResource 方法來(lái)設(shè)置下拉視圖的布局樣式。該方法需要傳入布局資源,該布局需要定義每個(gè) Item 的屬性,比如寬高和文字顏色等(為了使效果明顯,我將每個(gè) Item 的高度設(shè)置為 50 dp,文字設(shè)置為藍(lán)色)點(diǎn)擊這個(gè) Spinner 控件時(shí),讓其運(yùn)行一段“從左到右、逐漸顯示”的漸變動(dòng)畫:我通過(guò) xml 的方式來(lái)定義這個(gè)動(dòng)畫,需要包含 translate(位移) 和 alpha(透明度) 兩個(gè)TAG,并設(shè)置相應(yīng)的屬性值下拉菜單的內(nèi)容列表要展示在 Spinner 里面,需要通過(guò)適配器 Adapter 跟 Spinner 進(jìn)行綁定:可以直接使用Android原生的 ArrayAdapter選擇任意一個(gè) Item 后,將其內(nèi)容展示在界面上,告知用戶選擇的內(nèi)容:需要實(shí)現(xiàn) Spinner 的 onItemSelected 監(jiān)聽回調(diào)
源碼如下:
1、主Activity(注意代碼中的注釋,不然你會(huì)遇到一些坑?。?/p>
public class SpinnerDemo extends Activity { private static final String[] countries = {"北京", "上海", "廣州", "深圳", "成都", "杭州"}; private TextView mTextView; private Spinner mSpinner; private ArrayAdapter<String> mAdapter; private Animation mAnimation; @Override protected void onCreate(Bundle onSavedInstance) { super.onCreate(onSavedInstance); setContentView(R.layout.spinner_demo); mTextView = findViewById(R.id.textView9); mSpinner = findViewById(R.id.spinner); mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, countries); // 自定義的下拉視圖布局樣式 mAdapter.setDropDownViewResource(R.layout.spinner_drop_down); // 設(shè)置數(shù)據(jù)的適配器 mSpinner.setAdapter(mAdapter); mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mTextView.setText("你選擇的是:" + countries[position]); // 一定要設(shè)置父視圖可見,否則 在選擇后,Spinner會(huì)消失 parent.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // 通過(guò) xml 的形式來(lái)定義動(dòng)畫 mAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim); mSpinner.setOnTouchListener(new Spinner.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // 點(diǎn)擊 Spinner 后,運(yùn)行動(dòng)畫 v.startAnimation(mAnimation); return false; } }); } }
2、布局文件 spinner_demo.xml 如下:
<?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:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/textView9"/> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner" android:layout_gravity="center" android:layout_marginTop="15dp"/> </LinearLayout>
3、自定義的下拉視圖樣式布局文件 spinner_drop_down.xml 如下:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="50dp" android:textColor="@color/colorBlue" android:singleLine="true" style="?android:attr/spinnerDropDownItemStyle"> </TextView>
4、自定義動(dòng)畫 xml 文件如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="50%p" android:duration="2000"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000"/> </set>
5、效果圖如下:
到此這篇關(guān)于Android實(shí)現(xiàn)動(dòng)畫效果的自定義下拉菜單功能的文章就介紹到這了,更多相關(guān)android自定義下拉菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)微博菜單彈出效果
- Android 側(cè)滑抽屜菜單的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)上下菜單雙向滑動(dòng)
- Android如何實(shí)現(xiàn)底部菜單固定到底部
- Android實(shí)現(xiàn)微信加號(hào)菜單模式
- Android實(shí)現(xiàn)衛(wèi)星菜單效果
- Android自定義轉(zhuǎn)盤菜單效果
- Android底部菜單欄(RadioGroup+Fragment)美化
- Android recyclerView橫條指示器實(shí)現(xiàn)淘寶菜單模塊
- android studio 的下拉菜單Spinner使用詳解
- Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn)
相關(guān)文章
輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件
這篇文章主要為大家詳細(xì)介紹了可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件,結(jié)合WheelView實(shí)現(xiàn)滾輪選擇日期操作,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)雙向滑動(dòng)特效的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)雙向滑動(dòng)特效的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android 組件Gallery和GridView示例講解
本文主要講解Android 組件Gallery和GridView,這里詳細(xì)介紹組件Gallery和GridView的知識(shí)要點(diǎn),并附示例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-08-08Android實(shí)現(xiàn)水波紋效果實(shí)例代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)水波紋效果實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02Android通過(guò)自定義view實(shí)現(xiàn)刮刮樂效果詳解
這篇文章主要介紹了如何在Android中利用自定義的view實(shí)現(xiàn)刮刮樂的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟上小編一起動(dòng)手試一試2022-03-03Android使用MediaRecorder類進(jìn)行錄制視頻
這篇文章主要介紹了Android使用MediaRecorder類進(jìn)行錄制視頻的相關(guān)資料,需要的朋友可以參考下2015-10-10