我試圖攔截類的Android android.view.View
的onDraw()
方法,以瞭解視圖何時完成繪製自己(以及隨後啓動其他操作)的線索。超類的Java子類構造函數
但是,這讓我想到了一些Java問題(我對Java的經驗有限)。
我的動態onCreate()
方法包含以下代碼:
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.nessiean, null);
setContentView(view);
doSomeOperations();
我定義了一個子類,其主要目的是定義的內部狀態並且提供一個wait()
方法:
class MyView extends View{
int state=0;
public MyView(Context context){
super(context);
}
public MyView(Context context, AttributeSet attrs){
super(context,attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle);
}
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
state=1;
}
public void waitforDraw(){
while(state==0){};
}
}
的問題是:
我需要重新定義我的子類中的所有公共構造函數,如上所述? Java默認不會調用它們?
我不能在我的
onCreate()
方法更換行:
View view = inflater.inflate(R.layout.nessiean, null);
與
MyView view = inflater.inflate(R.layout.nessiean, null);
錯誤時正在:cannot convert from View to MyView
。
任何提示?
==========================完整代碼如下================= =============
package com.example;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
public class SoundActivity extends Activity {
private Thread mWorkerThread;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//START: ALTERNATIVE WAY FOR CREATING THE VIEW
//*first variant:
//setContentView(R.layout.nessiean);
//*second variant:
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MyView view = (MyView) inflater.inflate(R.layout.nessiean, null);
setContentView(view);
//STOP: ALTERNATIVE WAY FOR CREATING THE VIEW
System.loadLibrary("soundtest");
mWorkerThread = new Thread(new Runnable() {
public void run() {
execute();
}
},"Worker Thread");
try{
mWorkerThread.start();
mWorkerThread.join();
}
catch (Exception e) {
System.exit(1);
}
}
private native void execute();
}
class MyView extends View{
int state=0;
public MyView(Context context){
super(context);
}
public MyView(Context context, AttributeSet attrs){
super(context,attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle);
}
@Override
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
state=1;
}
public void waitforDraw(){
while(state==0){};
}
}
看到我的編輯..這可能有助於.. – ngesh 2012-04-16 13:46:49