原文:https://blog.csdn.net/uyy203/article/details/53992816
package com.circle.ctrls;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.circle.utils.Utils;
import com.taotie.circle.R;
/**
* Created by xyz on 2016/12/20.
* 自定義switch開關(guān)
*/
public class CustomSwitch extends FrameLayout {
private Context context;
private final static int MP=ViewGroup.LayoutParams.MATCH_PARENT;
private final static int WC=ViewGroup.LayoutParams.WRAP_CONTENT;
private Drawable bg_on;
private Drawable bg_off;
private ImageView ball;
private ImageView switchBg;
private int local_state =0;//本地狀態(tài) 0:關(guān) 1:開
public CustomSwitch(Context context, AttributeSet attrs,int defStyle){
super(context,attrs,defStyle);
init(context);
this.context=context;
}
public CustomSwitch(Context context,AttributeSet attrs){
super(context,attrs);
init(context);
this.context=context;
}
public CustomSwitch(Context context){
super(context);
init(context);
this.context=context;
}
private void init(Context context){
/**整體布局預(yù)設(shè)**/
bg_on = getResources().getDrawable(R.drawable.switch_on_bg);
bg_off =getResources().getDrawable(R.drawable.switch_off_bg);
FrameLayout.LayoutParams FParams=new FrameLayout.LayoutParams(WC,Utils.getRealPixel(59));
setLayoutParams(FParams);
ImageView switch_default_Bg=new ImageView(context);
FParams=new FrameLayout.LayoutParams(WC,WC);
switch_default_Bg.setImageResource(R.drawable.switch_off_bg);
addView(switch_default_Bg,FParams);
switchBg=new ImageView(context);
FParams=new FrameLayout.LayoutParams(WC,WC);
addView(switchBg,FParams);
//開關(guān)圓球
LinearLayout ballLayout=new LinearLayout(context);
ballLayout.setOrientation(LinearLayout.HORIZONTAL);
FParams=new FrameLayout.LayoutParams(WC,WC);
FParams.gravity = Gravity.BOTTOM;
addView(ballLayout,FParams);
ball=new ImageView(context);
LinearLayout.LayoutParams lParams=new LinearLayout.LayoutParams(WC,WC);
ball.setImageResource(R.drawable.switch_thumb);
ballLayout.addView(ball,lParams);
// turnOff(0);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(local_state ==0){
turnOn(200);
}else{
turnOff(200);
}
if(onSwitchClickListener != null){
onSwitchClickListener.onClickSwitch(local_state);
}
}
});
}
/**
* 需要詳細(xì)配置請看 {@link #turnOn(int time)}
* 打開按鈕粤咪,帶漸變動(dòng)畫
* @param time 漸變時(shí)間
* 0 馬上設(shè)置成打開狀態(tài)
*
*/
private void turnOn(int time){
// ballLayout.setGravity(Gravity.RIGHT);
switchBg.setImageDrawable(getResources().getDrawable(android.R.color.transparent));
ball.setPadding(Utils.getRealPixel(40),0,0,0);
// switchBg.setBackgroundResource(R.drawable.switch_on_bg);
local_state =1;
TransitionDrawable td;
if(time==0) {
td = new TransitionDrawable(new Drawable[]{bg_on,bg_on});
}else{
td = new TransitionDrawable(new Drawable[]{getResources().getDrawable(android.R.color.transparent),bg_on});
}
td.setCrossFadeEnabled(true);
td.startTransition(time);
switchBg.setImageDrawable(td);
}
/**
* 需要詳細(xì)配置請看 {@link #turnOff(int time)}
* 打開按鈕捅儒,帶漸變動(dòng)畫
* @param time 漸變時(shí)間
* 0 馬上設(shè)置成關(guān)閉狀態(tài)
*
*/
private void turnOff(int time){
// ballLayout.setGravity(Gravity.RIGHT);
switchBg.setImageDrawable(getResources().getDrawable(android.R.color.transparent));
ball.setPadding(0,0,Utils.getRealPixel(40),0);
// switchBg.setBackgroundResource(R.drawable.switch_off_bg);
local_state =0;
TransitionDrawable td;
if(time==0) {
td = new TransitionDrawable(new Drawable[]{getResources().getDrawable(android.R.color.transparent),getResources().getDrawable(android.R.color.transparent)});
}else{
td = new TransitionDrawable(new Drawable[]{bg_on, getResources().getDrawable(android.R.color.transparent)});
}
td.setCrossFadeEnabled(true);
td.startTransition(time);
switchBg.setImageDrawable(td);
}
//以漸變效果沐悦,在200ms內(nèi)打開開關(guān)
public void transitionOn(){
turnOn(200);
}
//以漸變效果疤孕,在200ms內(nèi)關(guān)閉開關(guān)
public void transitionOff(){
turnOff(200);
}
//無漸變效果谈跛,強(qiáng)制打開開關(guān)
public void switchOn(){
turnOn(0);
}
//無漸變效果蜕猫,強(qiáng)制關(guān)閉開關(guān)
public void switchOff(){
turnOff(0);
}
//獲取開關(guān)本地狀態(tài)
public int getLocalState(){
return this.local_state;
}
//點(diǎn)擊時(shí)回傳switch 狀態(tài),0為關(guān)閉,1為打開
public interface OnSwitchClickListener{
void onClickSwitch(int local_state);
}
private OnSwitchClickListener onSwitchClickListener;
public void setOnSwitchClickListener(OnSwitchClickListener l){
this.onSwitchClickListener=l;
}
}
效果
使用到transitionDrawable 詳細(xì)使用方法腹备,下面是傳送門