自定义switchButton
程序员文章站
2022-07-02 17:31:14
这篇博客要讲的是自定义switchButton,不过没有设置动画效果。 我用GradientDrawable来绘制switchButton,我们先看看最终的效果: 点击前: 点击后 接下来我们看看如何实现 首先:该类继承RelativeLayout 该类需要用到的成员: 构造方法: 设置点击事件 以 ......
这篇博客要讲的是自定义switchButton,不过没有设置动画效果。
我用GradientDrawable来绘制switchButton,我们先看看最终的效果:
点击前:
点击后
接下来我们看看如何实现
首先:该类继承RelativeLayout
SwitchButton extends RelativeLayout{
}
该类需要用到的成员:
private ImageView track; //滑块所在的轨道 private ImageView slider; //switchButton上面的滑块 private GradientDrawable trackDrawable; //用于绘制轨道 private GradientDrawable sliderDrawable; //用于绘制滑块 private LayoutParams trackLy; //轨道的布局 private LayoutParams sliderLy; //滑块的布局 private boolean isCkeck = false; //是否点击
构造方法:
public SwitchButton(Context context) { super(context); init(); } public SwitchButton(Context context, AttributeSet attrs) { super(context, attrs); init(); }
private void init(){ setClickable(true); setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); track = new ImageView(getContext()); slider = new ImageView(getContext()); trackLy = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); trackLy.addRule(CENTER_VERTICAL); sliderLy = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); sliderLy.addRule(CENTER_VERTICAL); track.setLayoutParams(trackLy); slider.setLayoutParams(sliderLy); trackDrawable = new GradientDrawable(); sliderDrawable = new GradientDrawable();
//轨道的形状,颜色,大小,边缘,角度 trackDrawable.setShape(GradientDrawable.RECTANGLE); trackDrawable.setColor(getResources().getColor(R.color.white_FFFFFFFF)); trackDrawable.setSize(getResources().getDimensionPixelOffset(R.dimen.track_width), getResources().getDimensionPixelOffset(R.dimen.track_height)); trackDrawable.setStroke(getResources().getDimensionPixelOffset(R.dimen.switch_button_stroke), getResources().getColor(R.color.gray)); trackDrawable.setCornerRadius(getResources().getDimension(R.dimen.switch_button_cornerRadius));
//设置滑块的形状,颜色,大小,边缘,角度 sliderDrawable.setShape(GradientDrawable.OVAL); sliderDrawable.setColor(getResources().getColor(R.color.white_FFFFFFFF)); sliderDrawable.setSize(getResources().getDimensionPixelOffset(R.dimen.slider_size), getResources().getDimensionPixelOffset(R.dimen.slider_size)); sliderDrawable.setStroke(getResources().getDimensionPixelOffset(R.dimen.switch_button_stroke), getResources().getColor(R.color.gray)); //设置给imageView track.setImageDrawable(trackDrawable); slider.setImageDrawable(sliderDrawable); addView(track); addView(slider); setOnClickListener(this); }
设置点击事件
@Override public void onClick(View view) { if (isCkeck) { isCkeck = false; }else { isCkeck = true; } setCheck(isCkeck); }
private void setCheck(boolean isCheck){ if (isCheck){
//轨道变为蓝色 trackDrawable.setColor(getResources().getColor(R.color.blue));
//注意这里进行版本判断。if与else里面代码的效果是相同的,只是有些代码在某些版本下才能够用 if (Build.VERSION.SDK_INT >= 17) { sliderLy.addRule(ALIGN_PARENT_END);
//设置新的布局之前,要先移除之前的布局,否则会有叠加的效果 sliderLy.removeRule(ALIGN_PARENT_START); } else { sliderLy.addRule(ALIGN_PARENT_RIGHT); sliderLy.addRule(ALIGN_PARENT_LEFT,0); } slider.setLayoutParams(sliderLy); }else { trackDrawable.setColor(getResources().getColor(R.color.white_FFFFFFFF)); if (Build.VERSION.SDK_INT >= 17) { sliderLy.removeRule(ALIGN_PARENT_END); sliderLy.addRule(ALIGN_PARENT_START); } else { sliderLy.addRule(ALIGN_PARENT_RIGHT,0); sliderLy.addRule(ALIGN_PARENT_LEFT); } slider.setLayoutParams(sliderLy); } }
以上便是所有的代码。
请尊重劳动成果,转载请标明出处:http://www.cnblogs.com/tangZH/p/8277428.html