看真正的猫眼效果图
接下来看自己写的粗略图(不足的地方是这里是2个切换选项,假设须要3个切换的话,须要自己定义控件,兴许在更新。。。)
源代码地址
自己定义控件(21)---自己定义控件之高仿猫眼小项目(3)
先看主页面xml布局文件--so easy
activity_main.xml
MainActivity--看主布局代码--so easy
package com.example.mytogglebutton; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private TextView tv_left; private TextView tv_right; private TextView tv_up; //表示是否要从购物切换到美食 private boolean tv_up_left_tran = false; //表示是否要从美食切换到购物 private boolean tv_up_right_tran = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); tv_right.setOnClickListener(this); tv_left.setOnClickListener(this); } private void initView() { tv_left = (TextView) findViewById(R.id.tv_left); tv_right = (TextView) findViewById(R.id.tv_right); tv_up = (TextView) findViewById(R.id.tv_up); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_left: //表示要从美食切换到购物 if (!tv_up_right_tran) { TranslateAnimation animation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 1.4f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); animation.setDuration(100); tv_up.setText("购物"); animation.setFillAfter(true); tv_up.startAnimation(animation); } //切换后,改动状态--能够从左边切换到右边。可是不能从右边切换到左边 tv_up_right_tran = true; tv_up_left_tran = false; Toast.makeText(this, ""+tv_up.getText(), 1).show(); break; case R.id.tv_right: //表示要从购物切换到美食 if (!tv_up_left_tran) { TranslateAnimation animation1 = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.4f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); animation1.setDuration(100); tv_up.setText("美食"); tv_up.startAnimation(animation1); animation1.setFillAfter(true); } //切换后,改动状态--能够从右边切换到左边,可是不能从左边切换到右边 tv_up_left_tran = true; tv_up_right_tran = false; Toast.makeText(this, ""+tv_up.getText(), 1).show(); break; default: break; } } }