2014-02-09 38 views
0

我有點困惑我的代碼在Android 3和Android 4的工作方式。我試圖每2秒拍一張照片,每次找到它的色相和Lum然後使用這些數字作爲X Y座標在canvasView中在地圖上畫一個十字。我使用可怕的方式在Android 3中工作正常。以下是我的代碼塊。android老相對較新版本

//this is the Activity to take the picture 
    public class ProcessAnalyser extends Activity implements OnClickListener, 
    SurfaceHolder.Callback, Camera.PictureCallback { 

    ... declare a bunch of variables here 
    static double H, L; 

    public void onPictureTaken(final byte[] data, Camera camera) { 
    ...code here 
    H = some value; 
    L = some value; 
    } 

經過長期RGB工作的H和L得到這是我從隔壁班

// this View draws the cross on a canvas 
    public class CanvasView extends View { 
    Bitmap bmp; 
    float X, Y; 
    static double kx, ky,width, height;// the code could be wrote whothout all these vars, but I try to split the drawing line of code into smaller chunks 

public CanvasView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if(inflater != null){  
     inflater.getContext();//this was the BIG problem 
    } 

    bmp=BitmapFactory.decodeResource(getResources(), R.drawable.position);  
} 

@Override 
protected void onDraw(Canvas canvas) { 

    super.onDraw(canvas); 

    width = (double) getWidth(); 
    height = (double) getHeight(); 
    kx = (double)width/360; 
    ky = (double)height/100; 
    X = Math.round(ProcessAnalyser.H); 
    Y = Math.round(ProcessAnalyser.L); 


    setBackgroundResource(R.drawable.spectrum); 

    canvas.drawBitmap(bmp, (float)(X *kx)-15, (float) (Y *ky)-15 , null);  

} 

} 

我想這種做法看起來很可怕的人訪問某些價值,但它的作品。每次在帶有android 3的三星平板電腦上拍攝新照片時,十字會重新繪製在一個新的位置。但是,如果我在帶有Android 4的設備上嘗試它,則十字架會保持在0.0位置。

+0

我試圖把H和L放在一個SQLite中,但是當試圖訪問它們時它無法從一個View中訪問數據庫,它看起來像SQLiteOpenHelper沒有將視圖作爲參數,它要求上下文。 – user3140889

回答

0

排序了我自己所以這裏是答案。問題在於畫布類中的代碼是在做它的工作,但只有一次。

X = Math.round(ProcessAnalyser.H); 
Y = Math.round(ProcessAnalyser.L); 

因此,事實上,H和L正在改變他們的價值觀並沒有出現在類CanvasView的代碼後,行

canvas.drawBitmap(bmp, (float)(X *kx)-15, (float) (Y *ky)-15 , null); 

被執行死刑。答案是

protected void onDraw(Canvas canvas) { 

方法必須擦拭完成後,重複一遍。這可以通過寫入線

invalidate(); 

在onDraw方法的結尾。

相關問題