我试图拦截onDraw()
Androidandroid.view.View
类的方法,以了解视图何时完成绘制自身(并随后启动其他操作)。
但是,这使我遇到了一些 Java 问题(我对 Java 的经验有限)。
我的 ActivityonCreate()
方法包含以下代码:
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){};
}
}