Wie kann ich dieses View zentrieren

  • Antworten:1
Worf01
  • Forum-Beiträge: 4

12.03.2015, 21:48:18 via Website

Hallo Community,

wie erreiche ich es, dass die Ausgabe zentriert wird?
Wichtig ist, dass die Zentrierung über das Layout erfolgen muss und nicht über "canvas.drawBitmap".
Ich möchte nämlich zu einem späteren Zeitpunkt eine Drehung des Bildes über das Layout - und nicht über das Bitmap erreichen.

Danke im Voraus.

import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

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

        GameView gameView = new GameView(this);

        RelativeLayout relativeLayout = new RelativeLayout(this);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

        relativeLayout.addView(gameView, layoutParams);

        setContentView(relativeLayout);
    }
}


import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

public class GameView extends View{
    public GameView(Context context) {
        super(context);
    }

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

        Resources resources = getResources();
        canvas.drawBitmap(BitmapFactory.decodeResource(resources, R.drawable.ic_launcher), 0, 0, null);
    }
}

— geändert am 13.03.2015, 19:46:01

Antworten
Frederik B.
  • Forum-Beiträge: 53

23.07.2015, 00:21:59 via Website

Also ich glaube die Antwort kommt ein bisschen zu spät, aber egal. :D

Wenn du nur ein einfaches Bild haben möchtest welches du drehen kannst,
wieso schreibst du dir dann eine GameView?
Du kannst genau so gut ein "ImageView" im normalen Layout benutzen.
Durch eine kleine Animation kannst du dann auch das Bild drehen.
Das ist auch nicht schwer ... falls es sich so anhören sollte. ;)

Antworten