论坛首页 移动开发技术论坛

widget物体移动/控件拖动

浏览 7701 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-11   最后修改:2009-07-11

之前已经写过通过重绘整个背景的方式,实现物体移动。今天要讲的是,如何通过移动widget,view等控件,实现部分重绘。结合之前发Droiddraw工具,使用AbsoluteLayout,可以随意放置控件在任何位置。

 

package com.sky;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;

@SuppressWarnings("deprecation")
public class AbosoluteMove extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
	public Button button1;
	public Button button2;
	public Button button3;
	public Button button4;
	Button tmp;//临时保存,选择的BUTTON
	int x;
	int y;
	int flag;//用于标志选择哪个BUTTON
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1=(Button) findViewById(R.id.widget27);
        button2=(Button) findViewById(R.id.widget28);
        button3=(Button) findViewById(R.id.widget29);
        button4=(Button) findViewById(R.id.widget30);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }
	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.widget27:
				flag=R.id.widget27;
				setTitle("button1");
				break;
			case R.id.widget28:
				flag=R.id.widget28;
				setTitle("button2");
				break;
			case R.id.widget29:
				flag=R.id.widget29;
				setTitle("button3");
				break;
			case R.id.widget30:
				flag=R.id.widget30;
				setTitle("button4");
				break;
		}
	}
		
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		// TODO Auto-generated method stub
		x = (int) event.getX();
        y = (int) event.getY();
        tmp=(Button) findViewById(flag);//获取所选中的BUTTON
        AbsoluteLayout.LayoutParams params1=new AbsoluteLayout.LayoutParams(50,50,x-25,y-50);
        tmp.setLayoutParams(params1);//设置BUTTON的新位置
        switch(event.getAction())
        {   
            case MotionEvent.ACTION_DOWN:    
            	tmp.invalidate();  
            	tmp.setText("选中down");
                break;   
            case MotionEvent.ACTION_UP:    
            	tmp.invalidate();  
            	tmp.setText("选中UP");
                break;   
            case MotionEvent.ACTION_MOVE:   
            	tmp.invalidate();   
            	tmp.setText("选中move");
                break; 
        }
        return super.onTouchEvent(event);
	}
}

 

 

   发表时间:2009-07-12  
如果加上TranslateAnimation的效果就更好了
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics