仿腾讯头部切换按钮优化版

仿QQ头部banner切换按钮组件

public class SwitchView extends LinearLayout implements OnClickListener {


    private static final int FLAG_MOVE_TRUE = 1; // 向左滑动标识
    private static final int FLAG_MOVE_FALSE = 2; // 向右滑动标识


    private static final int HANDLE_LAYOUT_CURSOR = 100; // 处理调用开关的layout方法


    private Context context; // 上下文对象
    private RelativeLayout sv_container; // SwitchView的外层Layout
    private ImageView iv_switch_cursor; // 开关邮标的ImageView
    private TextView switch_text_true; // true的文字信息控件
    private TextView switch_text_false; // false的文字信息控件


    private boolean isChecked = true; // 是否已开
    private boolean checkedChange = false; // isChecked是否有改变
    private OnCheckedChangeListener onCheckedChangeListener; // 用于监听isChecked是否有改变
    
    private boolean isNight = false;


    private int margin = 1; // 游标离边缘位置(这个值视图片而定, 主要是为了图片能显示正确)
    private int bg_left; // 背景左
    private int bg_right; // 背景右
    private int cursor_left; // 游标左部
    private int cursor_top; // 游标顶部
    private int cursor_right; // 游标右部
    private int cursor_bottom; // 游标底部


    private Animation animation; // 移动动画
    private int currentFlag = FLAG_MOVE_TRUE; // 当前移动方向flag


    private RelativeLayout relativeLayout;
    public SwitchView(Context context) {
        super(context);
        this.context = context;
        initView();
    }
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SwitchView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.context = context;
        initView();
    }






    public SwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        initView();
    }






    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        // 获取所需要的值
        bg_left = sv_container.getLeft();
        bg_right = sv_container.getRight();
        cursor_left = iv_switch_cursor.getLeft();
        cursor_top = iv_switch_cursor.getTop();
        cursor_right = iv_switch_cursor.getRight();
        cursor_bottom = iv_switch_cursor.getBottom();
    }




    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case HANDLE_LAYOUT_CURSOR:
                    iv_switch_cursor.layout(cursor_left, cursor_top, cursor_right, cursor_bottom);
                    break;
            }
        }
    };


    public void onClick(View v) {
        // 控件点击时触发改变checked值
        if(v == this) {
            changeChecked(!isChecked);
        }
    }


    /**
     * 初始化控件
     */
    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.switchview_layout, this);
        view.setOnClickListener(this);
        sv_container = (RelativeLayout) view.findViewById(R.id.switch_layout);
        switch_text_true = (TextView) view.findViewById(R.id.switch_text_true);
        switch_text_false = (TextView) view.findViewById(R.id.switch_text_false);
        changeTextColor();
        iv_switch_cursor = (ImageView) view.findViewById(R.id.iv_switch_cursor);
        iv_switch_cursor.setClickable(false);
        iv_switch_cursor.setOnTouchListener(new OnTouchListener() {
            int lastX; // 最后的X坐标


            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();


                        cursor_left = v.getLeft();
                        cursor_top = v.getTop();
                        cursor_right = v.getRight();
                        cursor_bottom = v.getBottom();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;


                        cursor_left = v.getLeft() + dx;
                        cursor_right = v.getRight() + dx;


                        // 超出边界处理
                        if(cursor_left <= bg_left + margin) {
                            cursor_left = bg_left + margin;
                            cursor_right = cursor_left + v.getWidth();
                        }
                        if(cursor_right >= bg_right - margin) {
                            cursor_right = bg_right - margin;
                            cursor_left = cursor_right - v.getWidth();
                        }
                        v.layout(cursor_left, cursor_top, cursor_right, cursor_bottom);


                        lastX = (int) event.getRawX();
                        break;
                    case MotionEvent.ACTION_UP:
                        calculateIscheck();
                        break;
                }
                return true;
            }
        });
    }


    /**
     * 计算处于true或是false区域, 并做改变处理
     */
    private void calculateIscheck() {
        float center = (float) ((bg_right - bg_left) / 2.0);
        float cursor_center = (float) ((cursor_right - cursor_left) / 2.0);
        if(cursor_left + cursor_center <= center) {
            changeChecked(true);
        } else {
            changeChecked(false);
        }
    }


    /**
     * 改变checked, 根据checked移动游标
     * @param isChecked
     */
    private void changeChecked(boolean isChecked) {
        if(this.isChecked != isChecked) {
            checkedChange = true;
        } else {
            checkedChange = false;
        }
        if(isChecked) {
            currentFlag = FLAG_MOVE_TRUE;
        } else {
            currentFlag = FLAG_MOVE_FALSE;
        }
        cursorMove();
    }


    /**
     * 游标移动
     */
    private void cursorMove() {
        // 这里说明一点, 动画本可设置animation.setFillAfter(true)
        // 令动画进行完后停在最后位置. 但这里使用这样方式的话.
        // 再次拖动图片会出现异常(具体原因我没找到)
        // 所以最后只能使用onAnimationEnd回调方式再layout游标
        animation = null;
        final int toX;
        if(currentFlag == FLAG_MOVE_TRUE) {
            toX = cursor_left - bg_left - margin;
            animation = new TranslateAnimation(0, -toX, 0, 0);
        } else {
            toX = bg_right - margin - cursor_right;
            animation = new TranslateAnimation(0, toX, 0, 0);
        }
        animation.setDuration(100);
        animation.setInterpolator(new LinearInterpolator());
        animation.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation animation) {


            }
            public void onAnimationRepeat(Animation animation) {


            }
            public void onAnimationEnd(Animation animation) {
                // 计算动画完成后游标应在的位置
                if(currentFlag == FLAG_MOVE_TRUE) {
                    cursor_left -= toX;
                    cursor_right = cursor_left + iv_switch_cursor.getWidth();
                } else {
                    cursor_right = bg_right - margin;
                    cursor_left = cursor_right - iv_switch_cursor.getWidth();
                }
                // 这里不能马上layout游标正确位置, 否则会有一点点闪屏
                // 为了美观, 这里迟了一点点调用layout方法, 便不会闪屏
                mHandler.sendEmptyMessageDelayed(HANDLE_LAYOUT_CURSOR, 5);
                // 这里是根据是不是改变了isChecked值进行一些操作
                if(checkedChange) {
                    isChecked = !isChecked;
                    if(onCheckedChangeListener != null) {
                        onCheckedChangeListener.onCheckedChanged(isChecked);
                    }
                    changeTextColor();
                }
            }
        });
        iv_switch_cursor.startAnimation(animation);
    }


    /**
     * 改变字体显示颜色
     */
    private void changeTextColor() {
        if(isChecked) {
            if(isNight){
                switch_text_true.setTextColor(Color.parseColor("#A8A8A8"));
                switch_text_false.setTextColor(Color.parseColor("#535353"));
            }else{
                 switch_text_true.setTextColor(Color.BLACK);
                 switch_text_false.setTextColor(Color.GRAY);
            }
        } else {
            if(isNight){
                switch_text_true.setTextColor(Color.parseColor("#535353"));
                switch_text_false.setTextColor(Color.parseColor("#A8A8A8"));
            }else{
                switch_text_true.setTextColor(Color.GRAY);
                switch_text_false.setTextColor(Color.BLACK);
            }
            
        }
    }


    /**
     * layout游标
     */
    private void layoutCursor() {
        if(isChecked) {
            cursor_left = bg_left + margin;
            cursor_right = bg_left + margin + iv_switch_cursor.getWidth();
        } else {
            cursor_left = bg_right - margin - iv_switch_cursor.getWidth();
            cursor_right = bg_right - margin;
        }
        iv_switch_cursor.layout(cursor_left, cursor_top, cursor_right, cursor_bottom);
        changeTextColor();
    }


    /**
     * isChecked值改变监听器
     */
    public interface OnCheckedChangeListener {
        void onCheckedChanged(boolean isChecked);
    }


    public boolean isChecked() {
        return isChecked;
    }


    public void setChecked(boolean isChecked) {
        if(this.isChecked != isChecked) {
            this.isChecked = isChecked;
            if(onCheckedChangeListener != null) {
                onCheckedChangeListener.onCheckedChanged(isChecked);
            }
            layoutCursor();
        }
    }


    public void setOnCheckedChangeListener(
            OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }
    
    public void changeMode(boolean isNight){
        this.isNight = isNight;
        if(isNight){
            sv_container.setBackgroundResource(R.drawable.bbs_posts_switch_bg_night);
            iv_switch_cursor.setBackgroundResource(R.drawable.bbs_posts_switch_content_bg_night);
        }else{
            sv_container.setBackgroundResource(R.drawable.bbs_posts_switch_bg);
            iv_switch_cursor.setBackgroundResource(R.drawable.bbs_posts_switch_content_bg);
        }
        changeTextColor();
    }


}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/switch_layout"
    android:layout_width="170dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/bbs_posts_switch_bg">
    <ImageView
	    android:id="@+id/iv_switch_cursor"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_vertical"
	    android:background="@drawable/bbs_posts_switch_content_bg" 
	    android:contentDescription="@null"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_centerVertical="true">
        <TextView
            android:id="@+id/switch_text_true"
            android:layout_width="85dp"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:gravity="center"
            android:textColor="#ff000000"            
            android:text="分组" />
        <TextView
            android:id="@+id/switch_text_false"
            android:layout_width="85dp"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:gravity="center"
            android:textColor="#ff000000" 
            android:text="全部" />
    </LinearLayout>
</RelativeLayout>
以上是布局文件。

用到的资源文件:

app_top_banner_layout_bg2.png               //背景


bbs_posts_switch_bg_night.png                //夜晚模式


bbs_posts_switch_bg.png                        //


bbs_posts_switch_content_bg_night.png


bbs_posts_switch_content_bg.png

转载于:https://my.oschina.net/mastere/blog/161194

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值