Surface View 和线性布局以及 onClick 事件

Surface View and Linear layout and onClick event

我在将表面视图添加到线性布局时遇到问题,我已经尝试了已经可用的答案,但似乎仍然无法弄清楚。我的目标是在单击下一步按钮时在登录屏幕底部呈现动画。我尝试向屏幕添加一个线性布局,然后将我的 gameView 对象添加到它上面。

这是 loginActivity 和游戏视图 Class 为了清楚起见,我还添加了 xml。

    `package com.example.Combat;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.*;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.*;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class MyActivity extends Activity {
        /**
         * Called when the activity is first created.
         */

        public GameView gameView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            //setting the font

            Typeface myTypeFace = Typeface.createFromAsset(getAssets(),"Typo Oxin free promo.ttf");
            TextView myTextView = (TextView) findViewById(R.id.nameTextView);
            myTextView.setTypeface(myTypeFace);



            gameView = new GameView(this);

    //        LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    //        lp.gravity= Gravity.BOTTOM;


           LinearLayout myLayout = new LinearLayout(this);
            myLayout.findViewById(R.id.gameLayoutView);
           myLayout.addView(gameView);


        }

        @Override

        public void onResume(){
            super.onResume();
            gameView.resume();
        }

        @Override

        protected void onPause(){
            super.onPause();
            gameView.pause();
        }




        public void beginMotion(View view) {

            GameView.isMoving=!GameView.isMoving;

           // startActivity(new Intent(getApplicationContext(),transitionActivity.class));
        }





    }
    `


        package com.example.Combat;

    import android.content.Context;
    import android.graphics.*;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;

    /**
     * Created by vmuser on 2017/05/05.
     */
    public class GameView extends SurfaceView implements Runnable{

        private Thread gameThread;
        private SurfaceHolder ourHolder;
        public static volatile boolean playing=false;
        private Canvas canvas;
        private Bitmap bitmapRunningMan;
        public  static boolean isMoving=false;
        private float runSpeedPerSecond = 150;

        //LinearLayout screen=(LinearLayout)findViewById(R.id.theScreen);



        private float manXPos = 1020, manYPos = 950;
        private int frameWidth = 230, frameHeight = 274;
        private int frameCount = 6;
        private int currentFrame = 0;
        private long fps;
        private long timeThisFrame;
        private long lastFrameChangeTime = 0;
        private int frameLengthInMillisecond = 60;

        private Rect frameToDraw = new Rect(0, 0, frameWidth, frameHeight);
        private RectF whereToDraw = new RectF(manXPos, manYPos, manXPos + frameWidth, frameHeight);

        public GameView(Context context) {
            super(context);
            ourHolder = getHolder();
            bitmapRunningMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.perfectsoldier);
            bitmapRunningMan = Bitmap.createScaledBitmap(bitmapRunningMan,
                    frameWidth * frameCount, frameHeight, false);



        }

        @Override
        public void run() {
            while (playing) {
                long startFrameTime = System.currentTimeMillis();
                update();
                this.draw();

                Log.d("theOne","its Happennig");

                timeThisFrame = System.currentTimeMillis() - startFrameTime;

                if (timeThisFrame >= 1) {
                    fps = 1000 / timeThisFrame;
                }
            }
        }

        public void update() {
            if (isMoving) {
                manXPos = manXPos - runSpeedPerSecond / fps;



                if (manXPos > 0) {
                    //manYPos += (int) frameHeight;
                    //manXPos = 10;

                    isMoving=false;
                }

                if (manYPos + frameHeight > 0) {
                    // manYPos = 10;
                    isMoving=false;
                }
            }
        }


        public void manageCurrentFrame() {
            long time = System.currentTimeMillis();

            if (isMoving) {
                if (time > lastFrameChangeTime + frameLengthInMillisecond) {
                    lastFrameChangeTime = time;
                    currentFrame++;

                    if (currentFrame >= frameCount) {
                        currentFrame = 0;
                    }
                }
            }

            frameToDraw.left = currentFrame * frameWidth;
            frameToDraw.right = frameToDraw.left + frameWidth;
        }

        public void draw() {
            if (ourHolder.getSurface().isValid()) {
                canvas = ourHolder.lockCanvas();
                canvas.drawColor(Color.WHITE);
                whereToDraw.set((int) manXPos, (int) manYPos, (int) manXPos
                        + frameWidth, (int) manYPos + frameHeight);
                manageCurrentFrame();
                canvas.drawBitmap(bitmapRunningMan, frameToDraw, whereToDraw, null);
                ourHolder.unlockCanvasAndPost(canvas);
            }
        }


        public void pause() {
            playing = false;

            try {
                gameThread.join();
            } catch(InterruptedException e) {
                Log.e("ERR", "Joining Thread");
            }
        }

        public void resume() {
            playing = true;
            gameThread = new Thread(this);
            gameThread.start();
        }

    //    public boolean onTouchEvent(MotionEvent event) {
    //        switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
    //            case MotionEvent.ACTION_DOWN :
    //                isMoving = !isMoving;
    //                break;
    //        }
    //
    //        return true;
    //    }




    }


    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:focusableInTouchMode="true"
              android:color="#000000"
              android:weightSum="1" android:background="#ffffff" android:orientation="vertical"
              android:id="@+id/theScreen">
    <ImageView
            android:layout_width="113dp"
            android:layout_height="130dp"
            android:src="@drawable/thelogo"
            android:id="@+id/imageView" android:layout_gravity="center_horizontal"/>
    <TextView
            android:layout_width="145dp"
            android:layout_height="49dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="combat"
            android:textSize="30sp"
            android:textColor="#000000"

            android:id="@+id/nameTextView" android:layout_gravity="right" android:layout_weight="0.06"/>
    <EditText
            android:layout_width="270dp"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:textColorHint="#808080"
            android:textColor="#22272a"
            android:id="@+id/userNameEditText" android:layout_gravity="center_horizontal"/>
    <Button
            android:layout_width="261dp"
            android:layout_height="wrap_content"
            android:text="Next"
            android:drawableRight="@drawable/thearrow"
            android:textColor="#000000"
            android:onClick="beginMotion"
            android:id="@+id/NextButton" android:layout_gravity="center_horizontal" android:background="#638455"
    />

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Create an account"
            android:id="@+id/textView" android:layout_gravity="center_horizontal" android:layout_weight="0.07"
            android:textColor="#22272A"/>

<LinearLayout

        android:id="@+id/gameLayoutView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

</LinearLayout>


</LinearLayout>

您还没有将 myLayout 附加到您之前在 setContentView(R.layout.main) 上设置的 activity 主布局。

而不是:

 LinearLayout myLayout = new LinearLayout(this);
 myLayout.findViewById(R.id.gameLayoutView);
 myLayout.addView(gameView);

你应该这样写:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.gameLayoutView);
myLayout.addView(gameView);