我試圖使用SurfaceView
和Canvas
來繪製波形。我使用一個SurfaceView
和Canvas
來繪製,並且它可以工作。如何使用SurfaceView疊加另一個SurfaceView以及爲什麼畫布爲空?
但是,當我想使第一個SurfaceView
覆蓋第二個SurfaceView
(使用FrameLayout
)。這是行不通的。
而且這兩個問題,在我看來:
1. If I use Canvas in second SurfaceView, then the second canvas becomes null;
2. If I don't use Canvas in second SurfaceView, but just call overlay, then the SurfaceView size will be overlay but the graph is same.
引用代碼如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("onCreate", "here");
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
l2.setVisibility(View.INVISIBLE);
sfv = (SurfaceView) findViewById(R.id.sfv);
sfh = sfv.getHolder();
sfv.getHolder().setFormat(PixelFormat.TRANSLUCENT);
sfv2 = (SurfaceView) findViewById(R.id.sfv2);
sfh2 = sfv2.getHolder();
sfv2.getHolder().setFormat(PixelFormat.TRANSLUCENT);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(3);
paint2.setColor(Color.RED);
paint2.setStrokeWidth(3);
Log.i("flow", "now at before init()");
tv = (TextView) findViewById(R.id.tv);
sfh.addCallback(this);
sfh2.addCallback(this);
//init();
}
public void init() {
Log.e("init", "here");
if (pic) {
canvas = sfh.lockCanvas(new Rect(xtime, 0, xtime + 2, getWindowManager().getDefaultDisplay().getHeight()));
canvas.drawARGB(255, 0, 0, 0);
for (int i = 0; i < 600; i++) {
a = 600 - i;
canvas.drawLine(xtime, oldy, xtime + 2, a, paint);
xtime += 2;
oldy = a;
if (xtime > 1000) {
xtime = 0;
oldy = 0;
}
}
sfh.unlockCanvasAndPost(canvas);
// tv.setText("in the init2");
}
else{
Log.e("sfh2", "here");
canvas = sfh2.lockCanvas(new Rect(xtime2, 0, xtime2 + 2, getWindowManager().getDefaultDisplay().getHeight()));
if(canvas == null){
Log.e("canvas", "null here");
}
else {
canvas.drawARGB(255, 255, 255, 255);
for (int i = 0; i < 300; i++) {
a = 300 - i;
canvas.drawLine(xtime2, oldy, xtime2 + 2, a, paint2);
xtime2 += 2;
oldy = a;
if (xtime2 > 1000) {
xtime2 = 0;
oldy2 = 0;
}
}
sfh2.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e("surfaceCreated", "here");
init();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.e("surfaceChanged", "here");
init();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("surfaceDestroyed", "here");
}
public void onchange(View view) {
if (pic) {
pic = false;
l1.setVisibility(View.VISIBLE);
l2.setVisibility(View.INVISIBLE);
tv.setText("change sf2");
Log.e("pic", "pic " + pic);
} else {
pic = true;
l1.setVisibility(View.INVISIBLE);
l2.setVisibility(View.VISIBLE);
tv.setText("change sf1");
Log.e("pic", "pic " + pic);
}
}
我一直在混淆了兩個星期,希望有人可以幫我解決這個問題。 謝謝大家。