您好,登錄后才能下訂單哦!
如何在Android中實(shí)現(xiàn)一個(gè)動(dòng)畫效果的自定義下拉菜單功能?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
要實(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)畫:我通過 xml 的方式來(lái)定義這個(gè)動(dòng)畫,需要包含 translate(位移) 和 alpha(透明度) 兩個(gè)TAG,并設(shè)置相應(yīng)的屬性值下拉菜單的內(nèi)容列表要展示在 Spinner 里面,需要通過適配器 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) { } }); // 通過 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" > </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>
關(guān)于如何在Android中實(shí)現(xiàn)一個(gè)動(dòng)畫效果的自定義下拉菜單功能問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。