Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 拖拉控件

android 拖拉控件

編輯:關於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;
		}
	};
    
}

這是布局文件

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved