public class SelectLayout extends LinearLayout { private AnimationSet mInAnime; private AnimationSet mOutAnime; private Animation mAlphaAnime; private View mCheckView; private View[] mViewArray; public SelectLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO 鑷姩鐢熸垚鐨勬瀯閫犲嚱鏁板瓨鏍? } public void iniSelectLayout(){ iniAnimation(); mViewArray=new View[getChildCount()]; for(int i=0;i<getChildCount();i++){ mViewArray[i]=getChildAt(i); if(i>0) mViewArray[i].startAnimation(mAlphaAnime); } mCheckView=mViewArray[0]; mCheckView.startAnimation(mInAnime); MyOnClickListener tmp=new MyOnClickListener(); for(View item:mViewArray) item.setOnClickListener(tmp); } private void iniAnimation(){ mInAnime=new AnimationSet(false); ScaleAnimation tmp=new ScaleAnimation(1, 1.2f, 1, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); tmp.setDuration(300); AlphaAnimation tmp2=new AlphaAnimation(0.7f, 1); tmp2.setDuration(300); mInAnime.addAnimation(tmp); mInAnime.addAnimation(tmp2); mInAnime.setFillAfter(true); tmp=new ScaleAnimation(1.2f, 1, 1.2f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); tmp.setDuration(300); tmp2=new AlphaAnimation(1, 0.7f); tmp2.setDuration(300); mOutAnime=new AnimationSet(false); mOutAnime.addAnimation(tmp); mOutAnime.addAnimation(tmp2); mOutAnime.setFillAfter(true); mAlphaAnime=new AlphaAnimation(0.7f,0.7f); mAlphaAnime.setFillAfter(true); } public View getSelectView(){ return mCheckView; } private class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍? if(!v.equals(mCheckView)){ for(int i=0;i<mViewArray.length;i++){ if(!mViewArray[i].equals(v)&&!mViewArray[i].equals(mCheckView)) mViewArray[i].startAnimation(mAlphaAnime); } v.startAnimation(mInAnime); mCheckView.startAnimation(mOutAnime); mCheckView=v; } } }
继承自LinearLayout,在xml里当LinearLayout用就行了,调用iniSelectLayout后会有类似RadioGroup的功能,被选中的子View会放大,没被选中的是半透明。注意如果子View的背景是jpg的话会出现闪烁,png不会。