原来做项目的时候,实现选中效果,都是用selector来实现,虽然可以实现变化效果,但是比较死板。
1。先贴上布局文件
android:layout_height="46.0dip"
android:background="@drawable/bottom_bg" >
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/main_menu_press" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@drawable/transparent"
android:button="@null"
android:checked="false"
android:drawableTop="@drawable/bottom_homepage_selector"
android:gravity="center"
android:text="@string/homepage_str"
android:textColor="@color/text_selector"
android:textSize="11.0sp" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@drawable/transparent"
android:button="@null"
android:drawableTop="@drawable/bottom_sort_selector"
android:gravity="center"
android:text="@string/sort_str"
android:textColor="@color/text_selector"
android:textSize="11.0sp" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@drawable/transparent"
android:button="@null"
android:drawableTop="@drawable/bottom_specific_selector"
android:gravity="center"
android:text="@string/specific_str"
android:textColor="@color/text_selector"
android:textSize="11.0sp" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@drawable/transparent"
android:button="@null"
android:drawableTop="@drawable/bottom_actions_selector"
android:gravity="center"
android:text="@string/actions_str"
android:textColor="@color/text_selector"
android:textSize="11.0sp" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@drawable/transparent"
android:button="@null"
android:drawableTop="@drawable/bottom_manager_selector"
android:gravity="center"
android:text="@string/personal_str"
android:textColor="@color/text_selector"
android:textSize="11.0sp" />
2.设置按键响应事件
protected void initButtomView() {
buttom_bg_text = (TextView) findViewById(R.id.bottom_select);
buttom_radio_lay = (RadioGroup) findViewById(R.id.bottom_layout);
homepage_radio = (RadioButton) findViewById(R.id.bottom_homepage);
sort_radio = (RadioButton) findViewById(R.id.bottom_sort);
specific_radio = (RadioButton) findViewById(R.id.bottom_specific);
actions_radio = (RadioButton) findViewById(R.id.bottom_actions);
manager_radio = (RadioButton) findViewById(R.id.bottom_manager);
buttom_radio_lay.setOnCheckedChangeListener(mChangeRadio);
}
int avg_width = 0;// 用于记录平均每个标签的宽度,移动的时候需要
private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() {
int startX;//移动的起始位置
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
avg_width = findViewById(R.id.bottom_select).getWidth();
LogUtil.i("LogUtil.i(mChangeRadio);");
LogUtil.i("avg_width = "+avg_width);
switch (checkedId) {
case R.id.bottom_homepage:
MoveButtomBg.moveFrontBg(buttom_bg_text, startX, 0, 0, 0);
startX = 0;
break;
case R.id.bottom_sort:
MoveButtomBg.moveFrontBg(buttom_bg_text, startX, avg_width, 0, 0);
startX = avg_width;
break;
case R.id.bottom_specific:
MoveButtomBg.moveFrontBg(buttom_bg_text, startX, avg_width*2, 0, 0);
startX = avg_width*2;
break;
case R.id.bottom_actions:
MoveButtomBg.moveFrontBg(buttom_bg_text, startX, avg_width*3, 0, 0);
startX = avg_width*3;
break;
case R.id.bottom_manager:
MoveButtomBg.moveFrontBg(buttom_bg_text, startX, avg_width*4, 0, 0);
startX = avg_width*4;
break;
default:
break;
}
}
};
3.其实实现动画的重点是动画的实现
public class MoveButtomBg {
/**
* 移动方法
*
* @param v
* 需要移动的View
* @param startX
* 起始x坐标
* @param toX
* 终止x坐标
* @param startY
* 起始y坐标
* @param toY
* 终止y坐标
*/
public static void moveFrontBg(View v, int startX, int toX, int startY, int toY) {
TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
anim.setDuration(200);
anim.setFillAfter(true);
v.startAnimation(anim);
}
}