android 拖拉控件

最近在研究一个类似屏保开锁的一个拖拉控件的activity,查网上的资料,在github找到一个很炫的,可是实在是不知道怎么用啊,所以就自己简单的做了一个,下面是源代码,很简单,视觉效果没那么好

这是MianActivity:

public class MainActivity extends Activity {
	ImageView basketball;
	ImageView circle;
	TextView arowleft,arowright,player_login,coach_login;
	boolean isLogin=false;
	int x,y;	//Imageview原来的位置


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//全屏显示
        setContentView(R.layout.activity_main);
        basketball=(ImageView)findViewById(R.id.basketball);
        arowleft=(TextView)findViewById(R.id.arowleft);
        arowright=(TextView)findViewById(R.id.arowright);
        circle=(ImageView)findViewById(R.id.circle);
        player_login=(TextView)findViewById(R.id.player_login);
        coach_login=(TextView)findViewById(R.id.coach_login);
        basketball.setOnTouchListener(moveListener);   //设置touch监听
    }
    private OnTouchListener moveListener=new OnTouchListener() {
		int lastX,lastY;
		
		@Override
		public boolean onTouch(View v, MotionEvent e) {
			// TODO Auto-generated method stub
			switch (e.getAction())
			{
				case MotionEvent.ACTION_DOWN:
					coach_login.setVisibility(View.VISIBLE);
					player_login.setVisibility(View.VISIBLE);
					circle.setVisibility(View.VISIBLE);
					arowleft.setVisibility(View.VISIBLE);
					arowright.setVisibility(View.VISIBLE);
					lastX=(int)e.getRawX();
					lastY=(int)e.getRawY();
					x=(int)e.getRawX();
					y=(int)e.getRawY();
					basketball.setImageResource(R.drawable.basketball_light);
					break;
				case MotionEvent.ACTION_MOVE:
					int distancX=(int)e.getRawX() - x;	//计算离开原来位置的距离
					int dx = (int) e.getRawX() - lastX;  
	                int dy = (int) e.getRawY() - lastY;
	                int left = v.getLeft() + dx;  
	                int top = v.getTop();  
	                int right = v.getRight() + dx;
	                int bottom = v.getBottom();
	                if(distancX>150||!isLogin)     //当移动到指定的位置就让对应的Textview变色
	                {
	                	coach_login.setTextColor(Color.YELLOW);
	                	arowright.setTextColor(Color.YELLOW);
	                	isLogin=true;
	                }else if(isLogin||distancX<150||distancX>0){
	                	coach_login.setTextColor(Color.WHITE);
	                	arowright.setTextColor(Color.WHITE);
	                }
	                if(distancX<-150||!isLogin)
	                {
	                	player_login.setTextColor(Color.YELLOW);
	                	arowleft.setTextColor(Color.YELLOW);
	                	isLogin=true;
	                }else if(distancX>-150||distancX<0)
	                {
	                	player_login.setTextColor(Color.WHITE);
	                	arowleft.setTextColor(Color.WHITE);
	                }
	                v.layout(left, top, right, bottom);//不断重新设置位置,达到移动的效果
	                lastX = (int) e.getRawX();  
	                lastY = (int) e.getRawY(); 
					break;
				case MotionEvent.ACTION_UP:
					coach_login.setVisibility(View.GONE);	//将控件还原
					player_login.setVisibility(View.GONE);
					circle.setVisibility(View.GONE);
					arowleft.setVisibility(View.GONE);
					arowright.setVisibility(View.GONE);
					basketball.setImageResource(R.drawable.basketball);
					player_login.setTextColor(Color.WHITE);
					arowleft.setTextColor(Color.WHITE);
					coach_login.setTextColor(Color.WHITE);
					arowright.setTextColor(Color.WHITE);
					break;
			}
			return true;
		}
	};
    
}

这是布局文件




    

    
    

    

    

    

    
    
    


android 拖拉控件_第1张图片 android 拖拉控件_第2张图片


你可能感兴趣的:(android)