Surfaceview和浮动操作按钮

时间:2016-12-27 05:41:31

标签: android surfaceview floating-action-button

如何将FloatingActionButton"置于顶部"一个SurfaceView

在使用这样的代码之前我使用了FloatingActionButton's

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

我的代码: - 开展活动 -

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle tmpExtras = this.getIntent().getExtras();
    if(tmpExtras != null) {

        Log.d("Hello", "We got fleet");
        fleet1 = tmpExtras.getParcelable("fleet1");
        fleet2 = tmpExtras.getParcelable("fleet2");
    }

    AnimationGenerator = new surfaceview_take_animation(this, fleet1, fleet2);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.BLACK);
    }
   // setContentView(AnimationGenerator);
    //setContentView(new surfaceview_take_animation(this, fleet1, fleet2));
    setContentView(R.layout.take_turn);
    LinearLayout surface = (LinearLayout)findViewById(R.id.GameSurface);
    surface.addView(new surfaceview_take_animation(this,fleet1,fleet2));

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabTakeTurn);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}

-SurfaceView-

public class surfaceview_take_animation extends SurfaceView implements SurfaceHolder.Callback{
    final static String TAG = "CanvasView";
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ScaleGestureDetector mScaleDetector;
    GestureDetector mGestures;
    int mode = Constants.INTERACTION_MODE_NONE;
    Matrix mMatrix = new Matrix();
    float mScaleFactor = 1.0f;
    float mTouchX;
    float mTouchY;
    float mScaleTouchX;
    float mScaleTouchY;
    float mTouchBackupX;
    float mTouchBackupY;
    float mTouchDownX;
    float mTouchDownY;

    Matrix drawMatrix = new Matrix();

    Rect canvasBox = new Rect();
    Rect boundingBox = new Rect();
    Rect gameBox = new Rect();
    Bitmap lampsBitmap[] = new Bitmap[12];
    Drawable fab;
    float lampsX[] = new float[12];
    float lampsY[] = new float[12];
    float lampsScreenX[] = new float[12];
    float lampsScreenY[] = new float[12];

    boolean selections[] = new boolean[12];
    boolean mMultiSelect = false;

    int gameMode = Constants.TAKE_TURN_NO_SELECTION;

    String imagename="";
    Canvas canvas;
    Resources res;
    int arenaX = 1080;
    int arenaY = 1920;
    Matrix m = new Matrix();
    int mActionBarSize = 0;

    data_type_parceable_fleet fleet1, fleet2;

    public surfaceview_take_animation(Context context, data_type_parceable_fleet tmpFleet1, data_type_parceable_fleet tmpFleet2) {
        super(context);

        // we need to get a call for onSurfaceCreated
        SurfaceHolder sh = this.getHolder();
        sh.addCallback(this);

        if(tmpFleet1 != null) {
            Log.d("SMURF", "made it here");
            fleet1 = tmpFleet1;
            fleet2 = tmpFleet2;
        }

        // for zooming (scaling) the view with two fingers
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());

        res = this.getResources();

        canvas = sh.lockCanvas();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth((float) 5);
        paint.setStyle(Paint.Style.STROKE);

        for(int i=0; i<12; i++) {
            //ultimately will be a class switcher
            switch (i) {
                case 0://Green
                    imagename = "class_green_red_128";
                    break;
                case 6://Green
                    imagename = "class_green_purple_128";
                    break;
                case 1://Red
                    imagename = "class_red_128";
                    break;
                case 7://Red
                    imagename = "class_red_128";
                    break;
                case 2://Blue
                    imagename = "class_blue_128";
                    break;
                case 8://Blue
                    imagename = "class_blue_128";
                    break;
                case 3://Yellow
                    imagename = "class_yellow_128";
                    break;
                case 9://Yellow
                    imagename = "class_yellow_128";
                    break;
                case 4://Orange
                    imagename = "class_orange_128";
                    break;
                case 10://Orange
                    imagename = "class_orange_128";
                    break;
                case 5://Purple
                    imagename = "class_purple_128";
                    break;
                case 11://Purple
                    imagename = "class_purple_128";
                    break;
            }



            lampsX[i] = (i * 50) + 85; //(lampsBitmap[i].getWidth()/2);
            lampsY[i] = (i * 80) + 85; //(lampsBitmap[i].getHeight()/2);
            lampsScreenX[i] = lampsX[i];
            lampsScreenY[i] = lampsY[i];

            int resID = res.getIdentifier(imagename, "mipmap", MainActivity.PACKAGE_NAME);
            lampsBitmap[i] = BitmapFactory.decodeResource(getResources(), resID);
            final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
            mActionBarSize = (int) styledAttributes.getDimension(0,0);

        }

        imagename = "ic_dialog_email";

        setFocusable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        //mGestures.onTouchEvent(event);
        boolean tmpFound;

        if (!this.mScaleDetector.isInProgress()) {
                final float x = event.getX();
                final float y = event.getY();
                final float diffX;
                final float diffY;

                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        mode = Constants.INTERACTION_MODE_NONE;
                        break;
                    case MotionEvent.ACTION_DOWN:
                        mTouchDownX = x;
                        mTouchDownY = y;

                        tmpFound = determineLampselection((mTouchDownX / mScaleFactor) + canvasBox.left,
                                (mTouchDownY / mScaleFactor) + canvasBox.top);

                        mTouchBackupX = mTouchX;
                        mTouchBackupY = mTouchY;

                        if (!tmpFound) {  //allow move if no selection
                            // pan/move started
                            mode = Constants.INTERACTION_MODE_PAN;
                        } else {
                            mode = Constants.INTERACTION_MODE_NONE;
                        }

                        CalculateMatrix(true);

                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == Constants.INTERACTION_MODE_PAN) {
                            if (x > mTouchDownX) {
                                diffX = x - mTouchDownX;
                                set_mTouchX(mTouchBackupX + (diffX));
                            } else {
                                diffX = mTouchDownX - x;
                                set_mTouchX(mTouchBackupX - (diffX));
                            }

                            if (y > mTouchDownY) {
                                diffY = y - mTouchDownY;
                                set_mTouchY(mTouchBackupY + (diffY));
                            } else {
                                diffY = mTouchDownY - y;
                                set_mTouchY(mTouchBackupY - (diffY));
                            }
                            CalculateMatrix(true);
                        }
                        break;
                }
        }

        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        float tmpLeftMost = 0;
        float tmpRightMost = 0;
        float tmpTopMost = 0;
        float tmpBottomMost = 0;
        canvas.drawColor(Color.BLACK);

        canvas.translate(mTouchX, mTouchY);
        canvas.scale(mScaleFactor, mScaleFactor, mScaleTouchX, mScaleTouchY);

        for(int i=0; i<12; i++) {
            if(i==0){
                tmpLeftMost = lampsScreenX[i];
                tmpRightMost = lampsScreenX[i];
                tmpTopMost = lampsScreenY[i];
                tmpBottomMost = lampsScreenY[i];
            }
            canvas.save();
            drawMatrix.reset();
            drawMatrix.postTranslate(lampsScreenX[i], lampsScreenY[i]);
            if(tmpLeftMost > lampsScreenX[i]){
                tmpLeftMost = lampsScreenX[i];
            }
            if(tmpRightMost < (lampsScreenX[i] +lampsBitmap[i].getWidth())){
                tmpRightMost = lampsScreenX[i]+ lampsBitmap[i].getWidth();
            }
            if(tmpTopMost > (lampsScreenY[i])){
                tmpTopMost = lampsScreenY[i];
            }
            if(tmpBottomMost < (lampsScreenY[i] + lampsBitmap[i].getHeight())){
                tmpBottomMost = lampsScreenY[i] +  lampsBitmap[i].getHeight();
            }

            canvas.drawBitmap(lampsBitmap[i], drawMatrix, paint);

            if(gameMode == Constants.TAKE_TURN_SINGLE_SELECTION){
                if(selections[i]) {
                    boundingBox.set((int) lampsScreenX[i], (int) lampsScreenY[i], (int) lampsScreenX[i] + lampsBitmap[i].getWidth(), (int) lampsScreenY[i] + lampsBitmap[i].getHeight());
                    canvas.drawRect(boundingBox, paint);
                }
            }
            canvas.restore();
        }
        canvasBox = canvas.getClipBounds();
        gameBox.set((int) (tmpLeftMost - 200*mScaleFactor), (int) (tmpTopMost - 200*mScaleFactor) , (int) (tmpRightMost + 200*mScaleFactor),(int) (tmpBottomMost + 200*mScaleFactor));
        canvas.drawRect(gameBox, paint);

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // otherwise onDraw(Canvas) won't be called
        this.setWillNotDraw(false);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
    }

    void CalculateMatrix(boolean invalidate) {
        if (invalidate)
            invalidate(); // re-draw
    }


    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        public ScaleListener() {
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mode = Constants.INTERACTION_MODE_ZOOM;
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            mode = Constants.INTERACTION_MODE_NONE;
            super.onScaleEnd(detector);
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float scale = detector.getScaleFactor();

            if (mode != Constants.INTERACTION_MODE_ZOOM)
                return true;

            mScaleFactor *= scale;
            mScaleFactor = Math.max(0.5f, Math.min(mScaleFactor, 3.0f));

            mScaleTouchX = (detector.getFocusX()/mScaleFactor) + canvasBox.left;
            mScaleTouchY = (detector.getFocusY()/mScaleFactor) + canvasBox.top;

            CalculateMatrix(true);

            return true;
        }
    }

    private boolean determineLampselection(float tmpDownX, float tmpDownY){
        float tmpX, tmpY, tmpHyp;
        float tmpShipX, tmpShipY;
        float tmpClosest = 0;
        boolean tmpFoundOne = false;
        mMultiSelect = false;

        for(int i =0; i<12; i++) {
            selections[i] = false;  //reset selections

            tmpShipX = lampsX[i] + lampsBitmap[i].getWidth()/2;
            tmpShipY = lampsY[i] + lampsBitmap[i].getHeight()/2;

            if(tmpDownX < tmpShipX){
                tmpX = tmpShipX - tmpDownX;
            }else{
                tmpX = tmpDownX - tmpShipX;
            }

            if(tmpDownY < tmpShipY){
                tmpY = tmpShipY - tmpDownY;
            }else{
                tmpY = tmpDownY - tmpShipY;
            }

            tmpHyp = (float) Math.sqrt( (double) (tmpX*tmpX) + (double)   (tmpY*tmpY) );

            if(tmpHyp < 60){
                selections[i] = true;
                if(!tmpFoundOne) {  //is there already a selection
                    tmpFoundOne = true;
                    gameMode = Constants.TAKE_TURN_SINGLE_SELECTION;
                }else{
                    mMultiSelect = true;
                    gameMode = Constants.TAKE_TURN_MULTI_SELECTION;
                }
            }
        }
        return tmpFoundOne;
    }

    private void set_mTouchX(float tmpX){
        mTouchX = tmpX;
    }

    private void set_mTouchY(float tmpY){
        mTouchY = tmpY;
    }

}

-layout xml for take_turn -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<LinearLayout
    android:id="@+id/GameSurface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="@dimen/margin_small"
    android:layout_marginTop="@dimen/margin_small"
    android:clickable="true"
    app:fabSize="mini"
    app:srcCompat="@mipmap/drone_squadron_64"
    android:id="@+id/fabTakeTurn"
    app:backgroundTint="@android:color/holo_blue_bright"
    app:elevation="7dp" />

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

试试这个:

surfaceView.setZOrderOnTop(false);

答案 1 :(得分:1)

我得到了它的工作。父布局需要是协调器布局。

谢谢!