android中View类的开发框架
2010-06-04 14:19:00 来源:WEB开发网mGameSurfaceView.y-=3;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
mGameSurfaceView.y+=3;
break;
}
return true;
}
}
package com.gameview;
import android.R.drawable;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;
public class GameView extends View {
int miCount = 0;
int y = 0;
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas)
{
if(miCount<100)
{
miCount++;
}
else
{
miCount = 0;
}
Paint mPaint = new Paint();
switch (miCount%4)
{
case 0:
mPaint.setColor(Color.BLUE);
break;
case 1:
mPaint.setColor(Color.GREEN);
break;
case 2:
mPaint.setColor(Color.RED);
break;
case 3:
mPaint.setColor(Color.YELLOW);
break;
default:
mPaint.setColor(Color.WHITE);
break;
}
//paint the rectangle
canvas.drawRect((500-80)/2, y, (500-80)/2+80, y+40, mPaint);
}
}
我们通过onDraw来绘制图,用invalidate的方法来实现界面的刷新,注意invalidate不能直接在进程中调用,因为它违背了单线程的模式:android的UI操作并不是线程安全的,并且这些操作必须在UI线程当中执行,因此android中最常用的方法是利用Handler来实现UI进程的更新
更多精彩
赞助商链接