安卓:在画布层的UI元素 [英] Android: UI elements over canvas layer

查看:126
本文介绍了安卓:在画布层的UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何设置了一些UI元素(在上面),印刷品吗?

我有一个简单的触摸游戏,有它的图形放在帆布自定义视图。然而,当我全屏幕面板处于的setContentView()我不能添加像进度或标识的任何UI项。我想在它有浮动整个画布层,一些对象(进度,标志)可见。

主要类:

 公共类GameClass扩展活动实现OnGestureListener {

面板面板;

公共无效的onCreate(包savedInstance){
  super.onCreate(savedInstance);

  面板=新的面板(这一点,1,2);
  的setContentView(面板);
  ...
}
 

面板类:

 公共类面板扩展视图{

  公共面板(上下文的背景下,整数变量1,诠释VAR2){
    超(上下文);
    ...
  }

  @覆盖
  保护无效的OnDraw(帆布油画){
    super.onDraw(画布);
    ...
  }
}
 

由于code显示,触摸在里面GameClass处理和变化的动画里面的面板的处理方式。

也许......有没有可能开始与进度条和按钮,新的透明的活动,这样既对底层活动覆盖的活动和对象的按钮?而且我可以需要一种方法来关闭所有的层(透明的活动,面板,GameClass)与透明的活动按钮。复杂? :D

解决方案

我也有同样的问题,这是我如何解决它

  @覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.game);

    drawView函数=新drawView函数(本);
    drawView.requestFocus();
    LinearLayout中上=(的LinearLayout)findViewById(R.id.LinearLayout01);
    upper.addView(drawView函数);
    的ImageButton菜单=(的ImageButton)findViewById(R.id.ImageButton01);
    menu.setOnClickListener(新OnClickListener()
    {
        @覆盖
        公共无效的onClick(视图v)
        {
            完();
        }
    });
    的ImageButton复位=(的ImageButton)findViewById(R.id.ImageButton02);
    reset.setOnClickListener(新OnClickListener()
    {
        @覆盖
        公共无效的onClick(视图v)
        {
            drawView.resetGame();
        }
    });
}
 

而不是设置内容查看到面板加入它的LinearLayout被添加在我的game.xml这里进入

  

game.xml

 < XML版本=1.0编码=UTF-8>< RelativeLayout的
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT
机器人:背景=@可绘制/ game_background>
<的LinearLayout
    机器人:ID =@ + ID / LinearLayout01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =350dip/>
<的LinearLayout
    机器人:ID =@ + ID / LinearLayout02
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / LinearLayout01>
    <的ImageButton
        机器人:ID =@ + ID / ImageButton01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:背景=@可绘制/菜单>< / ImageButton的>
    <的ImageButton
        机器人:ID =@ + ID / ImageButton02
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:背景=@可绘制/复位>< / ImageButton的>
< / LinearLayout中>
<的LinearLayout
    机器人:layout_below =@ + ID / LinearLayout01
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:ID =@ + ID / layout_ad
    机器人:layout_height =WRAP_CONTENT/>
 

How do I set some UI elements over (on top of) canvas?

I have a simple touch game that has its graphics placed on custom view with canvas. However as my full screen Panel is in the setContentView() I can't add any UI items like progressBar or logo. I would like to have whole canvas layer visible with some objects (progressBar, logo) "floating" over it.

Main class:

public class GameClass extends Activity implements OnGestureListener {

Panel panel;

public void onCreate(Bundle savedInstance) {
  super.onCreate(savedInstance);

  panel = new Panel(this, 1, 2);
  setContentView(Panel);
  ...
}

Panel class:

public class Panel extends View {   

  public Panel(Context context, int Var1, int Var2) {
    super(context);
    ...
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    ...
  }
}

As code shows, touches are handled inside GameClass and the changes in animation are handled inside Panel.

Or maybe.. Would it be possible to start new transparent activity with progress bar and button so that both the button on overlay activity AND objects on underlying activity? And I could need a way to close all the layers (transparent activity, Panel, GameClass) with the button on transparent activity. Complicated? :D

解决方案

I do have the same problem and here is how I solved it

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.game);

    drawView = new DrawView(this);
    drawView.requestFocus();
    LinearLayout upper = (LinearLayout) findViewById(R.id.LinearLayout01);
    upper.addView(drawView);
    ImageButton menu = (ImageButton) findViewById(R.id.ImageButton01);
    menu.setOnClickListener(new OnClickListener()
    {           
        @Override
        public void onClick(View v)
        {
            finish();
        }
    });
    ImageButton reset = (ImageButton) findViewById(R.id.ImageButton02);
    reset.setOnClickListener(new OnClickListener()
    {           
        @Override
        public void onClick(View v)
        {
            drawView.resetGame();
        }
    });
}

instead of setting contentview to panel adding it Linearlayout that is added in my game.xml and here goes my

game.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/game_background">
<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="350dip" />
<LinearLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/LinearLayout01">
    <ImageButton
        android:id="@+id/ImageButton01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/menu"></ImageButton>
    <ImageButton
        android:id="@+id/ImageButton02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/reset"></ImageButton>
</LinearLayout>
<LinearLayout
    android:layout_below="@+id/LinearLayout01"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:id="@+id/layout_ad"
    android:layout_height="wrap_content" />

这篇关于安卓:在画布层的UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆