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