android - 在 SurfaceView 中添加两个以上的按钮

标签 android android-layout android-intent

我在 SurfaceView 中实现了两个以上的按钮并且可以点击。我在我的应用程序中创建了 customView 和 surfaceview 但是,我不知道如何将这些按钮放在我的代码中。我发现 surfaceview 不能有任何“ child ”,但我很震惊,我不知道应该怎么做我编辑我的代码,以便能够显示按钮。我尝试在线搜索它,但我并不真正了解它是如何工作的,因为我是 android java 的新手。 任何帮助将不胜感激!

SurfaceViewExample.Java

public class SurfaceViewExample extends Activity implements OnTouchListener{

OurView v;
Bitmap ball, blob;
float x,y;
Button btnClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    v = new OurView(this);
    v.setOnTouchListener(this);
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    x = y = 0;

    btnClick = new Button(this);
    btnClick.setText("Click");
    btnClick.setGravity(Gravity.TOP | Gravity.LEFT);
    btnClick.setVisibility(View.VISIBLE);

    //adding the btn to the Custom VIEW
    v.addView(btnClick);
    setContentView(v);

}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    v.pause();
}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    v.resume();
}


public class OurView extends SurfaceView implements Runnable{

    Thread t = null;
    //helps to change the dimension of the surfaceview
    SurfaceHolder holder;
    boolean isItOk = false;


    public OurView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = getHolder();
    }
    public void addView(Button button_clear) {
        // TODO Auto-generated method stub

    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(isItOk  == true){
            //perform canvas drawing
            //if surface view is NOT VALID
            if(!holder.getSurface().isValid()){

                continue; // if it is FALSE -- will executed the codes after this {}
            }
                Canvas c = holder.lockCanvas();
                //background of the canvas color!
                c.drawARGB(255, 150, 150, 10);
                //THREAD
                c.drawBitmap(ball, x - (ball.getWidth()/2), y - (ball.getHeight()/2), null);
                holder.unlockCanvasAndPost(c);
        }
    }

    public void pause() {
        isItOk = false;
        while(true){
            try{
                t.join();
            }
            catch ( InterruptedException e){

                e.printStackTrace();
            }
            break;
        }
        t = null;
    }

    public void resume(){
        isItOk = true;
        t = new Thread(this);
        t.start();
    }
}
@Override
public boolean onTouch(View arg0, MotionEvent me) {
    // TODO Auto-generated method stub

    //reduce the time taken for process
    try{

        Thread.sleep(50); //20 times per seconds
    }
    catch ( InterruptedException e){
        e.printStackTrace();
    }

    switch(me.getAction()){

    case MotionEvent.ACTION_DOWN:
        x = me.getX();
        y = me.getY();
        break;

    case MotionEvent.ACTION_UP:
        x = me.getX();
        y = me.getY();
        break;

    case MotionEvent.ACTION_MOVE:
        x = me.getX();
        y = me.getY();
        break;
    }

    return true;
}

------我的布局--------

activity_main.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

</RelativeLayout>

最佳答案

为什么不创建一个父级为 RelativeLayout 的 LinearLayout 并将 View 添加到该 LinearLayout。现在,创建另一个包含您的按钮的 LinearLayout。

通过这种方式,您可以在 surfaceView 上拥有更多按钮。

希望你明白我的意思。如果没有,请告诉我。我会帮你的。

关于android - 在 SurfaceView 中添加两个以上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13900063/

相关文章:

android - ListView 上方 ViewPager 中的 float 操作按钮

android - 如何使用 1 个 GradientDrawable xml 资源制作 10 个具有 10 个不同边框角半径的 ImageView

android - 使用 Intent 在类之间传递数组

java - Android - 无法使用 FileProvider 添加电子邮件附件

java - 当我已经使用 onBackPressed() 来让 webview 返回时,如何在 android 应用程序中弹出警报框以退出?

android - 将 GreenDao LazyList 添加到列表中是不可能的?

android - 如何在android中制作一个圆形搜索栏,其图像适合其圆圈

Android 无缝音频循环

android - 使用 Firebase 的 GoogleApiClient 中出现罕见的 NullPointerException

android - 如何清除启动 Activity 的 Intent?