Android:一个 Activity 的多个视图

发布于 2024-11-13 05:39:21 字数 2759 浏览 0 评论 0原文

这是我的主要内容:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    LinearLayout layout = (LinearLayout) findViewById(R.id.mainView);
    TextView text=  (TextView) findViewById(R.id.text);
    text.setText("This is a Test!");

    firstCircle first = new firstCircle(this);
    secondCircle second = new secondCircle(this);
    layout.addView(first);
    layout.addView(second);

}

这是我的firstCircleClass:

public class firstCircle extends SurfaceView implements SurfaceHolder.Callback {

private firstThread _firstThread;
private secondThread _secondThread;
private SurfaceHolder surfaceHolder;


public firstCircle (Context context, AttributeSet attrs){
    super(context,attrs);
    getHolder().addCallback(this);
    setFocusable(true);
    _firstThread= new firstThread(getHolder(),this);

}

public firstCircle (Context context){
    super(context);
    getHolder().addCallback(this);
    setFocusable(true);
    _firstThread= new firstThread(getHolder(),this);

}

@Override
public void onDraw(Canvas c){

    String tag="My Activity";
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);

      c.drawCircle(100,100,100,paint);

}

@Override
public void surfaceCreated(SurfaceHolder holder){
    _firstThread.setRunning(true);
    _firstThread.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    boolean retry = true;
    _firstThread.setRunning(false);
    while (retry){
        try{
            _firstThread.join();
            retry = false;
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}




}

我有一个secondCircle类,它与上面的名称完全相同,并且圆圈的颜色为红色。

这是我的firstThread 类:

public class firstThread extends Thread {

private SurfaceHolder _surfaceHolder;
private firstClass _firstClass;
private boolean _run = false;

public firstThread (SurfaceHolder surfaceHolder, firstClass first){
    _surfaceHolder = surfaceHolder;
    _firstClass= first;

}


public void setRunning (boolean run){
    _run = run;
}

@Override
public void run(){

    while(_run){
        Canvas c=null;
        try{
            c = _surfaceHolder.lockCanvas(null);
            synchronized(_surfaceHolder){
                    _firstThread.onDraw(c);
            }


        } finally {
        }
            if (c!=null){
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }

}

我有一个与上面相同但名称不同的secondThread 类,它调用secondCircle 的onDraw()。

当我运行这个程序时,它只显示蓝色圆圈,而不显示红色圆圈。这是为什么呢?

Here is my main:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    LinearLayout layout = (LinearLayout) findViewById(R.id.mainView);
    TextView text=  (TextView) findViewById(R.id.text);
    text.setText("This is a Test!");

    firstCircle first = new firstCircle(this);
    secondCircle second = new secondCircle(this);
    layout.addView(first);
    layout.addView(second);

}

Here is my firstCircleClass:

public class firstCircle extends SurfaceView implements SurfaceHolder.Callback {

private firstThread _firstThread;
private secondThread _secondThread;
private SurfaceHolder surfaceHolder;


public firstCircle (Context context, AttributeSet attrs){
    super(context,attrs);
    getHolder().addCallback(this);
    setFocusable(true);
    _firstThread= new firstThread(getHolder(),this);

}

public firstCircle (Context context){
    super(context);
    getHolder().addCallback(this);
    setFocusable(true);
    _firstThread= new firstThread(getHolder(),this);

}

@Override
public void onDraw(Canvas c){

    String tag="My Activity";
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);

      c.drawCircle(100,100,100,paint);

}

@Override
public void surfaceCreated(SurfaceHolder holder){
    _firstThread.setRunning(true);
    _firstThread.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    boolean retry = true;
    _firstThread.setRunning(false);
    while (retry){
        try{
            _firstThread.join();
            retry = false;
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}




}

I have a secondCircle class that is the exact same as above with the name changed and the color of the circle being Red.

Here is my firstThread class:

public class firstThread extends Thread {

private SurfaceHolder _surfaceHolder;
private firstClass _firstClass;
private boolean _run = false;

public firstThread (SurfaceHolder surfaceHolder, firstClass first){
    _surfaceHolder = surfaceHolder;
    _firstClass= first;

}


public void setRunning (boolean run){
    _run = run;
}

@Override
public void run(){

    while(_run){
        Canvas c=null;
        try{
            c = _surfaceHolder.lockCanvas(null);
            synchronized(_surfaceHolder){
                    _firstThread.onDraw(c);
            }


        } finally {
        }
            if (c!=null){
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }

}

I have a secondThread class that is the same as above with a different name and that calls secondCircle's onDraw().

When i run this program it only shows the blue circle and not the red circle. Why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2024-11-20 05:39:21

添加视图时,请使用布局参数添加它们。第一个视图可能会填充父视图,而没有为第二个视图留下空间。

你可以做类似的事情

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layout.addView(first,p);
    layout.addView(second,p);

when you add the views, add them with layout parameters. Its possible the 1st view is filling the parent, leaving no space for the second view.

you could do something like

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layout.addView(first,p);
    layout.addView(second,p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文