web-dev-qa-db-fra.com

Android: comment ajouter un bouton en vue de surface

Je dessine des graphiques et j'aimerais y ajouter quelques boutons. Mais avec la vue de surface, comment ajouter ces boutons par programmation?

25
m4n07

Joignez votre surfaceView avec un FrameLayout dans votre disposition xml. Ajoutez ensuite vos boutons au même FrameLayout. Assurez-vous qu'ils sont placés sous la vue de la surface afin qu'ils soient dessinés par-dessus. (Cela pourrait être une bonne idée de les regrouper dans une autre mise en page et de l'ajouter à FrameLayout.)

<FrameLayout
  xmlns:Android="http://schemas.Android.com/apk/res/Android"
  Android:layout_width="match_parent"
  Android:layout_height="match_parent">
    <SurfaceView Android:id="@+id/surfaceView1" Android:layout_width="wrap_content" Android:layout_height="wrap_content"></SurfaceView>
    <LinearLayout Android:id="@+id/linearLayout1" Android:layout_width="wrap_content" Android:layout_height="wrap_content">
        <Button Android:text="Button" Android:id="@+id/button1" Android:layout_width="wrap_content" Android:layout_height="wrap_content"></Button>
        <Button Android:text="Button" Android:id="@+id/button2" Android:layout_width="wrap_content" Android:layout_height="wrap_content"></Button>
    </LinearLayout>
</FrameLayout>
45
pumpkee

Merci beaucoup Androidica ..

Votre xml m'a aidé à trouver la solution suivante par programme sans utiliser de xml ..

public class LudoActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        FrameLayout game = new FrameLayout(this);
        GameView gameView = new GameView (this);
        LinearLayout gameWidgets = new LinearLayout (this);

        Button endGameButton = new Button(this);
        TextView myText = new TextView(this);

        endGameButton.setWidth(300);
        endGameButton.setText("Start Game");
        myText.setText("rIZ..i");

        gameWidgets.addView(myText);
        gameWidgets.addView(endGameButton);       

        game.addView(gameView);
        game.addView(gameWidgets);

        setContentView(game);
        endGameButton.setOnClickListener(this);
    }

    public void onClick(View v) {
         Intent intent = new Intent(this, LudoActivity.class);
         startActivity(intent);
         // re-starts this activity from game-view. add this.finish(); to remove from stack
    }
}

tandis que GameView est;

public class GameView extends SurfaceView {

    public GameView(Context context) {
        super(context);

        /*
         * your code
         */
    }
}
34
Rizwan Sohaib

Faites votre propre bouton:

import Android.graphics.Bitmap;
import Android.graphics.Canvas;
import Android.graphics.Matrix;
import Android.graphics.RectF;

    public class GButton
    {
        public Matrix btn_matrix = new Matrix();

        public RectF btn_rect;

        float width;
        float height;   
        Bitmap bg;

        public GButton(float width, float height, Bitmap bg)
        {
            this.width = width;
            this.height = height;
            this.bg = bg;

            btn_rect = new RectF(0, 0, width, height);
        }

        public void setPosition(float x, float y)
        {
            btn_matrix.setTranslate(x, y);
            btn_matrix.mapRect(btn_rect);
        }

        public void draw(Canvas canvas)
        {
            canvas.drawBitmap(bg, btn_matrix, null);
        }
    }

événement tactile:

float x = ev.getX();
float y = ev.getY();
if (my_button.btn_rect.contains(x, y))
{
    // handle on touch here
}

alternativement, encore mieux, si vous souhaitez également faire pivoter le bouton, il ne sera pas aligné sur l'axe, puis utilisez la matrice inversée, au lieu de mapRect, mappez les points de contact x, y:

float pts[] = {x, y};            
my_button.invert_matrix.mapPoints(pts);           
if (my_button.btn_rect.contains(pts[0], pts[1])
{
    // handle on touch here
}
8
alex

Nous pouvons très facilement utiliser la disposition des cadres pour dessiner une vue de surface. comme ça

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical">
<FrameLayout
     Android:id="@+id/frameLayout"
     Android:layout_width="fill_parent"
     Android:layout_height="430dp"/>
   <LinearLayout
        Android:layout_width="fill_parent"
        Android:layout_height="50dp"
        Android:gravity="center_horizontal"
        Android:layout_gravity="bottom"
        Android:background="#c2300f">

        <Button
            Android:id="@+id/buttonColor"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:text="Color" />
    </LinearLayout>     
</LinearLayout>

Et l'activité principale est

package com.example.surfacetuto;


 import Android.app.Activity;
 import Android.graphics.Paint;
 import Android.graphics.Point;
 import Android.os.Bundle;
 import Android.util.Log;
 import Android.view.MotionEvent;
 import Android.view.View;
 import Android.view.View.OnClickListener;
 import Android.widget.Button;
 import Android.widget.FrameLayout;
 import Android.widget.TextView;
 import Android.widget.Toast;

 public class MainActivity extends Activity implements OnClickListener{
    DrawingSurface ds;
    FrameLayout frm;
    Button btnC;
    int color=0xfff00000;
    @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ds=new DrawingSurface(this);
    setContentView(R.layout.activity_main);


    frm=(FrameLayout)findViewById(R.id.frameLayout);
    frm.addView(ds);

    btnC=(Button)findViewById(R.id.buttonColor);

    btnC.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    case R.id.buttonColor:
        Toast.makeText(getApplicationContext(), "Color", 2).show();
        ds.colorNew();

        break;

    default:
        break;
    }
}   
    }

Et la classe Surface de dessin est

package com.example.surfacetuto;

 import Android.app.Activity;
 import Android.content.Context;
 import Android.graphics.Bitmap;
 import Android.graphics.BitmapFactory;
 import Android.graphics.Canvas;
 import Android.graphics.Color;
 import Android.graphics.Matrix;
 import Android.graphics.Paint;
 import Android.graphics.Paint.Cap;
 import Android.graphics.Rect;
 import Android.util.AttributeSet;
 import Android.util.DisplayMetrics;
 import Android.util.Log;
 import Android.view.MotionEvent;
 import Android.view.SurfaceHolder;
 import Android.view.SurfaceView;
 import Android.view.View;
 import Android.view.Window;
 import Android.view.WindowManager;
 import Android.widget.Toast;

   public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {

      Canvas cacheCanvas;
      Bitmap backBuffer;
      int width, height, clientHeight;
      Paint paint;
      Context context;
      SurfaceHolder mHolder;


public DrawingSurface(Context context) {
    super(context);
    this.context = context;
    init();
}
public DrawingSurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;      
    init();
}

private void init() {
    mHolder = getHolder();
    mHolder.addCallback(this);

}

int lastX, lastY, currX, currY;
boolean isDeleting;
@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    int action = event.getAction();
    switch(action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        lastX = (int) event.getX();
        lastY = (int) event.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if(isDeleting) break;

        currX = (int) event.getX();
        currY = (int) event.getY();
        cacheCanvas.drawLine(lastX, lastY, currX, currY, Paint);
        lastX = currX;
        lastY = currY;

        break;
    case MotionEvent.ACTION_UP:
        if(isDeleting) isDeleting = false;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        cacheCanvas.drawColor(Color.WHITE);
        isDeleting = true;
        break;
    case MotionEvent.ACTION_POINTER_UP:
        break;
    }
    draw(); 
    return true;
}

protected void draw() {

    if(clientHeight==0) {
        clientHeight = getClientHeight();
        height = clientHeight;
        backBuffer = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
        cacheCanvas.setBitmap(backBuffer);
        cacheCanvas.drawColor(Color.WHITE);
    }
    Canvas canvas = null;
    try{
        canvas = mHolder.lockCanvas(null);

        canvas.drawBitmap(backBuffer, 0,0, Paint);
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        if(mHolder!=null)  mHolder.unlockCanvasAndPost(canvas);
    }
}

private int getClientHeight() {
    Rect rect= new Rect();    
    Window window = ((Activity)context).getWindow();     
    window.getDecorView().getWindowVisibleDisplayFrame(rect);     
    int statusBarHeight= rect.top;    
    int contentViewTop= window.findViewById(Window.ID_Android_CONTENT).getTop();     
    int titleBarHeight= contentViewTop - statusBarHeight;
    return ((Activity)context).getWindowManager().getDefaultDisplay().
            getHeight() - statusBarHeight - titleBarHeight;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

public void surfaceCreated(SurfaceHolder holder) {

    width = getWidth();
    height = getHeight();
    cacheCanvas = new Canvas();
    backBuffer = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888); 
    cacheCanvas.setBitmap(backBuffer);
    cacheCanvas.drawColor(Color.WHITE);
    Paint = new Paint();
    Paint.setColor(Color.BLUE);
    Paint.setStrokeWidth(10);
    Paint.setStrokeCap(Paint.Cap.ROUND);
    Paint.setStrokeJoin(Paint.Join.ROUND);
    draw();

}

public void surfaceDestroyed(SurfaceHolder holder) {
     boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
}

public void colorNew() {
    // TODO Auto-generated method stub
    Paint.setColor(Color.GRAY);
}


   }
4
Android Help