前提条件是先加一个球的图片在资源文件的drawable文件下
1 主类
package com.example.d;
import android.app.Activity;
import android.os.Bundle;
public class DrawViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(new BallView(this));
}
}
3 view
package com.example.d;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
public class BallView extends View{
Point point=null;
Bitmap img = null;
public BallView(Context context) {
super(context);
img = BitmapFactory.decodeResource(context.getResources(), R.drawable.bol_geel);
point = new Point();
point.x = 60;
point.y = 60;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(img, point.x, point.y,null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
}
3 效果图