#2
cloud__king2014-03-24 13:41
|
我的图形不能动,求大神们指出我的问题
下面是我的代码:
public class MainActivity extends Activity {
MyView myView = null;
Runnable runnable=new Runnable() {
@Override
public void run() {
myView.move();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = new MyView(this) ;
setContentView(myView);
}
@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;
}
}
绘图的类:
public class MyView extends View{
private Paint paint;
int x = 160;
int y = 160;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
}
@Override
public void draw(Canvas canvas) {
Path path = new Path();
path.moveTo(x, y);
path.lineTo(x - 60, y + 80);
path.lineTo(x + 60, y + 80);
path.close();
canvas.drawPath(path, paint);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (x > 0) {
x -= 4;
}
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (x < 319) {
x += 4;
}
}
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
// TODO Auto-generated method stub
return true;
}
public void move() {
this.invalidate();
}
}