Android--全屏点击移动物体



-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 MainActivity.java

public class MainActivity extends Activity {
 GameView gameView=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//在调用布局前进行设置全屏
//设置无标题      
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        gameView=(GameView) findViewById(R.id.gameView1);
                
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
//对点击事件进行重写
    @Override
    public boolean onTouchEvent(MotionEvent event) {
     float x=event.getX();
     float y=event.getY();
     gameView.setX((int)x);
     gameView.setY((int)y);

//重画

     gameView.invalidate();

     return super.onTouchEvent(event);
    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

GameView.java

public class GameView extends View {
 int x;
 int y;
 int w;
 int h;
 Bitmap bitmap=null;
 public GameView(Context context, AttributeSet attrs) {
  super(context, attrs);

//获取资源

  bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

  w=bitmap.getWidth();
  h=bitmap.getHeight();
 }
 @Override
 protected void onDraw(Canvas canvas) {

//进行绘画

  canvas.drawBitmap(bitmap, x-w/2, y-h/2, null);
  super.onDraw(canvas);
 }
 public float getX() {
  return x;
 }
 public void setX(int x) {
  this.x = x;
 }
 public float getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }
}



-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class CView extends View {

	public CView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	public CView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	float x,y;
	Paint p=new Paint();

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		p.setColor(Color.RED);
		canvas.drawCircle(x, y, 155, p);
		super.onDraw(canvas);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		x=event.getX();
		y=event.getY();
		
		invalidate();
		return true;
//		return super.onTouchEvent(event);
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值