滑动开关
滑动开关呢 其实有很多种写法 我的这个比较简单 是一个滑动的动画 TranslateAnimation
原理 : 获取宽度 分成二分之一 点左边向左滑动二分之一距离 点右 反之 如果有三个的话 分成三分之一 原理一样
代码:
public class SwitchView extends LinearLayout implements OnClickListener {
private Context context; // 上下文对象
private LinearLayout sv_moff_container; // SwitchView的外层Layout
private ImageView iv_switch_cursor; // 开关邮标的ImageView
private TextView switch_moff_true; // true的文字信息控件
private TextView switch_moff_false; // false的文字信息控件
private boolean isChecked = true; // 是否已开
private int mSelectionLeft = 0; // 左边
private int mSelectionRight = 1;//右边
private OnCheckedChangeListener onCheckedChangeListener;
public SwitchView(Context context) {
super(context);
this.context = context;
initView();
}
public SwitchView(Context context, AttributeSet attrs) {
super(context);
this.context = context;
initView();
}
/**
* 初始化控件
*/
private void initView() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.switch_view, this);
sv_moff_container = (LinearLayout) view.findViewById(R.id.sv_moff_container);
switch_moff_true = (TextView) view.findViewById(R.id.moff_cityButton);
switch_moff_false = (TextView) view.findViewById(R.id.moff_localButton);
switch_moff_true.setOnClickListener(this);
switch_moff_false.setOnClickListener(this);
switch_moff_true.setTextColor(getResources().getColor(R.color.text_moffmap));
switch_moff_false.setTextColor(Color.WHITE);
iv_switch_cursor = (ImageView) view.findViewById(R.id.tab_moff_focus);
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
iv_switch_cursor.measure(w, h);
mSelectionLeft= iv_switch_cursor.getMeasuredWidth();//SportsApp.getInstance().dip2px(2);根据手机的分辨率从 dp 的单位 转成为 px(素)
}
//改变字体颜色
private void changeTextColor() {
if (isChecked) {
switch_moff_true.setTextColor(Color.WHITE);
switch_moff_false.setTextColor(getResources().getColor(R.color.text_moffmap));
//iv_switch_cursor.setBackgroundResource(R.drawable.sports_title_bg);
} else {
switch_moff_true.setTextColor(getResources().getColor(R.color.text_moffmap));
switch_moff_false.setTextColor(Color.WHITE);
//iv_switch_cursor.setBackgroundResource(R.drawable.sports_bg);
}
}
//动画
private void moveSelection(int position) {
TranslateAnimation anim = new TranslateAnimation((mSelectionLeft)
* (mSelectionRight - 1), ((mSelectionLeft)* (position - 1)), 0, 0);
anim.setFillAfter(true);
anim.setDuration(300);
anim.setFillEnabled(true);
iv_switch_cursor.startAnimation(anim);
mSelectionRight = position;
}
//设置监听的方法
public void setOnCheckedChangeListener(
OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
}
public interface OnCheckedChangeListener {
void onCheckedChanged(boolean isChecked);
}
public void onCheckPosition(int position){
moveSelection(position);
changeTextColor();
isChecked = !isChecked;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.moff_cityButton:
if(mSelectionRight != 1){
//1代表左边
moveSelection(1);
changeTextColor();
isChecked = !isChecked;
if (onCheckedChangeListener != null)
onCheckedChangeListener.onCheckedChanged(isChecked);//启动监听
}
break;
case R.id.moff_localButton:
if(mSelectionRight != 2){
//2 代表右边 滑动
moveSelection(2);
changeTextColor();
isChecked = !isChecked;
if (onCheckedChangeListener != null)
onCheckedChangeListener.onCheckedChanged(isChecked);//启动监
}
break;
default:
break;
}
}
这下面两张图片都是白色 可能会出现看不到的情况 直接复制吧
}
xml: switch_view //这是布局 可根据自己布局 背景 颜色 长度 想换样式 只需换图片<imageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sv_moff_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:layout_gravity="center_horizontal"
>
<ImageView
android:id="@+id/tab_moff_focus"
android:layout_width="wrap_content"
android:layout_height="wrap_content" //长度宽度自由设置
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:layout_alignLeft="@+id/tab_layout"
android:background="@drawable/tab_bar_focus"
android:scaleType="centerCrop" />
<LinearLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/tab_2"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/moff_cityButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="城市" />
<TextView
android:id="@+id/moff_localButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="列表" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
调用:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SwitchView switchView = (SwitchView)findViewById(R.id.switch_moff);
switchView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isChecked) {
// TODO Auto-generated method stub
Log.i("", "进来了"); //监听 isChecked 判断开 、关
}
});
}
}
// main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.main.SwitchView.SwitchView
android:id="@+id/switch_moff"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</LinearLayout>
完毕