這是一個簡單的覆蓋繪製圖像。 - 以及需要重繪的方法。
@Override
protected void onDraw(Canvas canvas){
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
paint.setARGB(10, 255, 255, 255);
if(Core.pic != null) {
canvas.drawBitmap(Core.pic, 0, 0, paint);
}
}
這是一個不同的類,對於重繪調用時,新的圖像可用:與
protected static volatile Bitmap pic;
public static void refreshDisplay(Bitmap img){
pic = img;
if(cameraDisplay != null) {
try{
cameraDisplay.invalidate();
}
catch(Exception e){
e.printStackTrace();
}
}
}
這裏是一個線程類,準備端口每50ms:
while(running){
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
if(in != null){
byte[] recieve = new byte[7000];
try {
in.read(recieve, 0, 7000);
Core.pic = BitmapFactory.decodeByteArray(recieve, 0, 7000, opt);
} catch (IOException e) {}
}
try {
sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
僅此一項工作正常。當我重疊這個視圖時,它會閃爍。在類似的方式,我讀加速度數據,並繪製它:
public void onAccelerationChanged(float x, float y, float z) {
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
velocityBars.DrawVelocity(x, -z);
}
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
velocityBars.DrawVelocity(y, -z);
}
}
velocityBars是我的變量,它是我的自定義視圖類型。此方法DrawVelocity調用invalidate()方法。這強制重繪。
如果您可以包含您在活動中使用的部分代碼 - 尤其是用您的日期更新視圖的代碼,這將會很有幫助。 – QRohlf 2010-08-16 22:33:14