[英]android older vs newer version
我對我的代碼在android 3和android 4中的工作方式有些困惑。我試圖每2秒拍攝一張圖片,每次找到它的Hue和Lum,然后在canvasView中使用這些數字作為X和Y坐標繪制在地圖上的十字架。 我使用的可怕方法在android 3中工作正常。這是我的代碼塊。
//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位置。
我自己整理出來,所以這就是答案。 問題在於canvas類中的代碼正在完成它的工作,但是只有一次。
X = Math.round(ProcessAnalyser.H);
Y = Math.round(ProcessAnalyser.L);
因此,在代碼行之后,在CanvasView類中看不到H和L正在更改其值的事實
canvas.drawBitmap(bmp, (float)(X *kx)-15, (float) (Y *ky)-15 , null);
被執行了。 答案是
protected void onDraw(Canvas canvas ) {
方法完成后必須擦拭,然后重做一次。 這可以通過編寫以下代碼來實現
invalidate();
在onDraw方法的末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.