1. 程式人生 > >android學習筆記之下拉動態彈出圖示選單的實現

android學習筆記之下拉動態彈出圖示選單的實現

首先在佈局上放幾個重疊的圖示

private int res[] = {R.id.i0, R.id.i1, R.id.i2, R.id.i3, R.id.i4, R.id.i5};
然後設定最上邊圖示的點選事件

應用屬性動畫,並設定插值器

animator.setInterpolator(new BounceInterpolator());
佈局如下
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.animatortest3.MainActivity" >

    <ImageView
        android:id="@+id/i0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
    <ImageView
        android:id="@+id/i1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
     <ImageView
         android:id="@+id/i2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
     
       <ImageView
           android:id="@+id/i3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
      
       <ImageView
           android:id="@+id/i4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
       
       <ImageView
           android:id="@+id/i5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    

</FrameLayout>


主活動如下
<span style="font-size:14px;">public class MainActivity extends Activity {

	private int res[] = {R.id.i0, R.id.i1, R.id.i2, R.id.i3, R.id.i4, R.id.i5};
	private List<ImageView> imageList = new ArrayList<ImageView>();
	private boolean flag = true;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<res.length;i++){
        	ImageView image = (ImageView) findViewById(res[i]);
        	image.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					switch (v.getId()){
					case R.id.i5:
						if(flag){
							startAnim();
						}
						else{
							closeAnim();
						}
						break;
					default:
						
					}
				}
        		
        	});
        	imageList.add(image);
        }
        
        
    }


    protected void closeAnim() {
		// TODO Auto-generated method stub
    	for(int i=0;i<res.length-1;i++){
			ObjectAnimator animator = ObjectAnimator.ofFloat(imageList.get(i), "translationY", i * 220, 0);
			animator.setDuration(500);
			animator.setInterpolator(new BounceInterpolator());
			animator.setStartDelay(i*200);
			animator.start();
			flag = true;
		}
	}


	protected void startAnim() {
		// TODO Auto-generated method stub
		for(int i=0;i<res.length-1;i++){
			ObjectAnimator animator = ObjectAnimator.ofFloat(imageList.get(i), "translationY", 0f, i * 220);
			animator.setDuration(500);
			animator.setInterpolator(new BounceInterpolator());
			animator.setStartDelay(i*200);
			animator.start();
			flag = false;
		}
	}


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}</span>