Android开发之旅一简单的Animator动画使用

对于Path的菜单效果看着很不错,今天就使用动画做个没有幅度的试试?

练练手重写RelativeLayout


看主布局


  



MainActivity

package com.example.testpath;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
      TestLayout layout=(TestLayout) findViewById(R.id.layout);
      
      List imgRes=new ArrayList();
      imgRes.add(R.drawable.composer_music);
      imgRes.add(R.drawable.composer_music);
      imgRes.add(R.drawable.composer_music);
      imgRes.add(R.drawable.composer_music);
      imgRes.add(R.drawable.composer_music);
      
      
      layout.init(R.drawable.composer_music, imgRes, TestLayout.LEFT_BOTTOM);
      
      layout.setOnPKButClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
		 System.out.println("----getId-----"+v.getId());
		}
	});
      
   /*   layout.setOnTouchListener(new OnTouchListener() {
		
		@Override
		public boolean onTouch(View view, MotionEvent event) {
			// TODO Auto-generated method stub
			
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				System.out.println("----dd------");
				return false;

			case MotionEvent.ACTION_MOVE:
				System.out.println("----mm------");
				return false;
				
			case MotionEvent.ACTION_UP:
				System.out.println("----uu------");
				return false;
			}
			
			return 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);
    }
}


重写的layout

package com.example.testpath;

import java.util.ArrayList;
import java.util.List;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class TestLayout extends RelativeLayout{
	
	private Context mContext;
	public static int LEFT_BOTTOM=1,RIGHT_BOTTOM=2;
	
	private ImageView mainButton;// 主按鈕
	
	//存放有多少个子按钮
	private List listImg=new ArrayList();
	
	private boolean isShow=false;
	
	//子按钮点击动画
	private Animation childAnim;

	public TestLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext=context;
	}

	public TestLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext=context;
	}

	public TestLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext=context;
	}

	
	/**
	 * 
	 * @param imgRes
	 * @param place
	 */
	public void init(int resMain,List imgRes,int place){
		
		int align1 = 1,align2 = 2;
		
		if(place == LEFT_BOTTOM){
			
			align1 = ALIGN_PARENT_LEFT;
			align2 = ALIGN_PARENT_BOTTOM;
			
		}else if(place == RIGHT_BOTTOM){
			
			align1 = ALIGN_PARENT_RIGHT;
			align2 = ALIGN_PARENT_BOTTOM;
			
		}
		
		
		//子按钮layout
		RelativeLayout layout = new RelativeLayout(mContext);
		//childLayout设置参数
		LayoutParams rlps1 = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		rlps1.alignWithParent = true;
		layout.setLayoutParams(rlps1);
		
		listImg.clear();
		
		//创建子按钮
		for(int i=0;i l=new ArrayList();
			 l.add(animX);
			 l.add(animY);
			 
			 set.playTogether(l);
			 set.start();
			
		}
		
		
	}
	
	/**
	 * 子按钮收缩动画
	 * @param duration
	 */
	public void startAnimationsOut(int duration) {
		
		int x=0,y=0;
		
		for(int i=0;i l=new ArrayList();
			 l.add(animX);
			 l.add(animY);
			 
			 set.playTogether(l);
			 set.start();
			 
			 set.addListener(new AnimatorListener() {
				
				@Override
				public void onAnimationStart(Animator arg0) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationRepeat(Animator arg0) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationEnd(Animator arg0) {
					// TODO Auto-generated method stub
					img.setVisibility(View.INVISIBLE);
				}
				
				@Override
				public void onAnimationCancel(Animator arg0) {
					// TODO Auto-generated method stub
					
				}
			});
		}
		
		
	}
	
	
	/**
	 * 子按钮点击事件动画(会移动view)
	 * @param view
	 * @param duration
	 */
	public void startChildAnim(View view,int duration){
		AnimatorSet set = new AnimatorSet() ;             
        Animator scaleX = ObjectAnimator .ofFloat(view, "scaleX", 2f); 
        Animator scaleY = ObjectAnimator .ofFloat(view, "scaleY", 2f); 
        Animator alpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.2f); 
        
        List l=new ArrayList();
        l.add(scaleX);
        l.add(scaleY);
        l.add(alpha);
        
        set.setDuration(duration);
        set.playTogether(l);
        set.start();
        
        set.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator arg0) {
				// TODO Auto-generated method stub
				
				startAnimationsOut(500);
			}
			
			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	/**
	 * 设置子按钮click事件
	 */
	public void setOnPKButClickListener(final OnClickListener l) {

		if (listImg != null && listImg.size() > 0) {
		
			for (int i = 0; i < listImg.size(); i++) {
				if (listImg.get(i) != null){
					
					listImg.get(i).setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							
							System.out.println("------点击了------");
//							startChildAnim(v, 500);
							v.startAnimation(childAnim);
							l.onClick(v);
						}
					});
				}
			}
		}
	}
	
}


看看效果

Android开发之旅一简单的Animator动画使用_第1张图片     Android开发之旅一简单的Animator动画使用_第2张图片



你可能感兴趣的:(RelativeLayout,Animator,Android)