仿IOS开关button 自定义ios开关控件

我是安卓一年新人,好像最近有那种突然入门了的感觉,觉得什么都可以做,什么都能做,什么都能做出来,做好,朋友说这是第一个门槛,我猜可能真的是,但是不管怎么样,这种感觉和手感一定要保留下来,所以漫无天日的开始写控件,觉得很有意思.就比如IOS的开关按钮.
IOS的开关按钮看起来很炫酷,我也仿了一个.先上图
关
开

个人觉得很容易实现的.一下是源码,不多解释了.

public class MainActivity extends Activity {

    private RelativeLayout rl1;
    private TextView tv1;
    private boolean fln;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rl1 = (RelativeLayout) findViewById(R.id.rl1);
        tv1 = (TextView) findViewById(R.id.tv1);
        fln = true;

        rl1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if(fln){
                    //开启的动画
                    Animation translateAnimation=new TranslateAnimation(0, 40, 0, 0);
                    translateAnimation.setDuration(200);//设置动画持续时间为3秒
                    translateAnimation.setInterpolator(MainActivity.this, android.R.anim.accelerate_decelerate_interpolator);//设置动画插入器
                    translateAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)
                    tv1.startAnimation(translateAnimation);
                    rl1.setBackgroundResource(R.drawable.corner02);
                    fln = false;
                }else{
                    //关闭的动画
                    Animation translateAnimation=new TranslateAnimation(40, 0, 0, 0);
                    translateAnimation.setDuration(200);//设置动画持续时间为3秒
                    translateAnimation.setInterpolator(MainActivity.this, android.R.anim.accelerate_decelerate_interpolator);//设置动画插入器
                    translateAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)
                    tv1.startAnimation(translateAnimation);
                    rl1.setBackgroundResource(R.drawable.corner01);
                    fln = true;
                }
            }
        });

    }

}
xml文件
corner00
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ffffff"/>

    <stroke android:width="0.5dp"
        android:color="#dddddd"
        />

    <corners 
        android:topLeftRadius="13dp"
        android:topRightRadius="13dp"
        android:bottomLeftRadius="13dp"
        android:bottomRightRadius="13dp"
        />


</shape>

corner01
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ffffff"/>

    <stroke android:width="1dp"
        android:color="#dfdfdf"
        />

    <corners 
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp"
        />


</shape>


corner02
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#4bd663"/>

    <stroke android:width="0.5dp"
        android:color="#4bd663"
        />

    <corners 
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp"
        />


</shape>

真的没有什么好解释的,两个动画,用fln决定开关属性.
我是安卓1年新人,请指教.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值