android学习笔记之下拉动态弹出图标菜单的实现

来源:互联网 发布:.商城 域名注册 编辑:程序博客网 时间:2024/05/21 22:09

首先在布局上放几个重叠的图标

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(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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 stubfor(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>


0 0