Kompass exended

L

linuxanton

Neues Mitglied
0
Hi,

Aufgabe ist folgende: ich habe eine Karte, Grundriss o.ä., die sich je nach Drehung des Geräts mitdreht. Meine Karte ist also immer genordet und die Richtung, in die ich schaue, stimmt mit der Richtung auf meinem Gerät überein.
So: jetzt will ich aber an bestimmten Stellen kleine Icons auf die Karte legen (nennen wir es einfach mal Points of Interest). Die Punkte bewegen sich also mit der Karte mit, die sich um ihren Mittelpunkt dreht. Bis hierher denke ich, habe ich das im Griff. Hier mal ein paar Snippets:

Code:
public class CompassView extends ImageView {
	private static final AtomicBoolean drawing = new AtomicBoolean(false);
	private static final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	
	private static int parentWidth = 0;
	private static int parentHeight = 0;
    private static Matrix matrix = null;
    private static Bitmap bitmap = null;
    private static Bitmap bobbel = null;
    private static Matrix bobbelmatrix = null;
    
    private int bobbelX, bobbelY, bobbelcenterX, bobbelcenterY; 
    

    public CompassView(Context context) {
        super(context);
        
        initialize();
    }    
    
    public CompassView(Context context, AttributeSet attr) {
        super(context,attr);
        
        initialize();
    }
    
    private void initialize() {
        matrix = new Matrix();
        bobbelmatrix = new Matrix();
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.grundriss);
        bobbel = BitmapFactory.decodeResource(getResources(), R.drawable.bobbel);

    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	int ausdehnung=0;
    	super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        ausdehnung = bitmap.getWidth();
        if(bitmap.getHeight()>ausdehnung)
        {
        	ausdehnung=bitmap.getHeight();
        }
        parentWidth=ausdehnung;
        parentHeight=ausdehnung;
        /*parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        parentHeight = MeasureSpec.getSize(heightMeasureSpec);*/
        setMeasuredDimension(parentWidth, parentHeight);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void onDraw(Canvas canvas) {
    	
    	
    	if (canvas==null) throw new NullPointerException();

        if (!drawing.compareAndSet(false, true)) return; 

        float bearing = GlobalData.getBearing();
       /* if (bitmap.getWidth()>canvasWidth || bitmap.getHeight()>canvasHeight) {        
            //Resize the bitmap to the size of the canvas
            bitmap = Bitmap.createScaledBitmap(bitmap, (int)(bitmapWidth*.9), (int)(bitmapHeight*.9), true);
        }*/
        
       
        int bitmapX = bitmap.getWidth()/2;
        int bitmapY = bitmap.getHeight()/2;
        
        int parentX = parentWidth/2;
        int parentY = parentHeight/2;
        
        int centerX = parentX-bitmapX;
        int centerY = parentY-bitmapY;
        
        int rotation = (int)(360-bearing);
        
        matrix.reset();
        //Rotate the bitmap around it's center point so it's always pointing north
        matrix.setRotate(rotation, bitmapX, bitmapY);
        //Move the bitmap to the center of the canvas
        matrix.postTranslate(centerX, centerY);

        canvas.drawBitmap(bitmap, matrix, paint);
        bobbelX=bobbel.getWidth()/2;
        bobbelY=bobbel.getHeight()/2;
        bobbelcenterX = parentX-bobbelX;
        bobbelcenterY = parentY-bobbelY;

        bobbelmatrix.reset();
        bobbelmatrix.setRotate(rotation,bobbelX,bobbelY);
        bobbelmatrix.postTranslate(bobbelcenterX, bobbelcenterY);
        
        
        canvas.drawBitmap(bobbel, bobbelmatrix, paint);
       
	    drawing.set(false);
    }

Bitmap ist mein Hintergrund, die Karte, bobbelmatrix ist einfach ein Point of Interest.

Und jetzt die Frage: Wie bekomme ich ein Click- oder TouchEvent hin, dass ich eindeutig sagen kann, ich habe auf meinen POI (bobbel) geclickt oder daneben? Bin mir nicht sicher, ob mein Gesamtansatz so zielführend ist oder ob es eine bessere Lösung gibt.

Danke schon mal.
linuxanton
 
Ich verstehe deine Frage nicht ganz. Du zeichnest ja dein Icon auf dein Bildschirm also weiß du auch wo du es hingezeichnet hast. Im OnTouch bekommst du die Info wo du "hingetoucht" hast. Jetzt musst du doch nur noch vergleichen, ob dein Touchevent in deinem Icon lag, oder habe ich da einen Denkfehler?
 
der Denkfehler liegt darin, dass sich das Icon ja mit der Karte mitdreht. Somit weiss ich nicht mehr, wo es ist ...
 
So: jetzt will ich aber an bestimmten Stellen kleine Icons auf die Karte legen (nennen wir es einfach mal Points of Interest). Die Punkte bewegen sich also mit der Karte mit, die sich um ihren Mittelpunkt dreht. Bis hierher denke ich, habe ich das im Griff. Hier mal ein paar Snippets:

Dies interpretiere ich als: Dein eigener Code berechnet laufend die Position des Icons neu. Also kennt dein Code doch immer die aktuelle Position des Icons.
 
ja, das "Programm" weiss schon, wo der Punkt ist, aber ich nicht :confused:

Code:
        bobbelmatrix.reset();         
bobbelmatrix.setRotate(rotation,bobbelX,bobbelY);         
bobbelmatrix.postTranslate(bobbelcenterX, bobbelcenterY);                           
canvas.drawBitmap(bobbel, bobbelmatrix, paint);

Hier wird die neue Position von bobbel berechnet. Wie aber komm ich an die realen xy-Koordinaten????
 
Nur zum Verständnis:

Du hast ein Bitmap mit der Karte. Du kopierst in die Karte dein Icon. Du drehst/rotierst die Karte incl. Icon, richtig?

Wenn ich das richtig verstanden habe können wir weiter machen. ;)

Allgemein schon vorab, mit cos und sin kannst du dir einen Kreis errechnen, also mit dem Winkel und dem Radius einen Punkt auf dem Kreis.
 

Ähnliche Themen

L
  • LoLmAnxD1998
Antworten
6
Aufrufe
2.286
LoLmAnxD1998
L
V
  • veitk
Antworten
0
Aufrufe
892
veitk
V
M
Antworten
2
Aufrufe
1.863
maho
M
Zurück
Oben Unten