-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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);
}
}