2011-10-17 12 views
26

के साथ घूर्णन के बाद समन्वय की नई स्थिति प्राप्त करें मैं सोच रहा हूं कि रोटेशन के बाद आयताकार के भीतर समन्वय की नई स्थिति प्राप्त करने के लिए मैट्रिक्स का उपयोग कैसे करें। मुझे क्या करना चाहते हैं:मैट्रिक्स

  1. एक आयत
  2. परिभाषित परिभाषित करें कि आयत
  3. आयत
  4. घुमाएँ के भीतर एक समन्वय समन्वय रोटेशन के बाद
की नई स्थिति प्राप्त करें

जिन भागों को मैं नहीं समझ सकता वे 2 & हैं 4. कोई विचार?

उत्तर

32

मैंने इसके लिए एक सरल डेमो बनाया है। इसमें थोड़ा अतिरिक्त है, इसलिए आप यह भी देख सकते हैं कि ड्रॉइंग में इसका उपयोग कैसे करें।

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

और गतिविधि:

package nl.entreco.android.testrotation; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.RelativeLayout; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 

public class RotationActivity extends Activity implements OnSeekBarChangeListener { 


    private MyDrawing myDrawing; 
    private SeekBar mSeekbar; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Rect rect = new Rect(150,150,440,630); 

     int x = (int) (rect.left + Math.random() * rect.width()); 
     int y = (int) (rect.top + Math.random() * rect.height()); 
     Point coordinate = new Point(x, y); 


     // To draw the rect we create a CustomView 
     myDrawing = new MyDrawing(this, rect, coordinate); 

     RelativeLayout rl = (RelativeLayout)findViewById(R.id.container); 
     rl.addView(myDrawing); 


     mSeekbar = (SeekBar)findViewById(R.id.seekBar1); 
     mSeekbar.setMax(360); 
     mSeekbar.setOnSeekBarChangeListener(this); 
    } 

    private class MyDrawing extends View 
    { 
     private Rect myRect; 
     private Point myPoint; 
     private Paint rectPaint; 
     private Paint pointPaint; 

     private Matrix transform; 

     public MyDrawing(Context context, Rect rect, Point point) 
     { 
      super(context); 

      // Store the Rect and Point 
      myRect = rect; 
      myPoint = point; 

      // Create Paint so we can see something :) 
      rectPaint = new Paint(); 
      rectPaint.setColor(Color.GREEN); 
      pointPaint = new Paint(); 
      pointPaint.setColor(Color.YELLOW); 

      // Create a matrix to do rotation 
      transform = new Matrix(); 

     } 


     /** 
     * Add the Rotation to our Transform matrix. 
     * 
     * A new point, with the rotated coordinates will be returned 
     * @param degrees 
     * @return 
     */ 
     public Point rotate(float degrees) 
     { 
      // This is to rotate about the Rectangles center 
      transform.setRotate(degrees, myRect.exactCenterX(),  myRect.exactCenterY()); 

      // Create new float[] to hold the rotated coordinates 
      float[] pts = new float[2]; 

      // Initialize the array with our Coordinate 
      pts[0] = myPoint.x; 
      pts[1] = myPoint.y; 

      // Use the Matrix to map the points 
      transform.mapPoints(pts); 

      // NOTE: pts will be changed by transform.mapPoints call 
      // after the call, pts will hold the new cooridnates 

      // Now, create a new Point from our new coordinates 
      Point newPoint = new Point((int)pts[0], (int)pts[1]); 

      // Return the new point 
      return newPoint; 
     } 

     @Override 
     public void onDraw(Canvas canvas) 
     { 
      if(myRect != null && myPoint != null) 
      { 
       // This is an easy way to apply the same transformation (e.g. rotation) 
       // To the complete canvas. 
       canvas.setMatrix(transform); 

       // With the Canvas being rotated, we can simply draw 
       // All our elements (Rect and Point) 
       canvas.drawRect(myRect, rectPaint); 
       canvas.drawCircle(myPoint.x, myPoint.y, 5, pointPaint); 
      } 
     } 
    } 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { 

     Point newCoordinates = myDrawing.rotate(progress); 


     // Now -> our float[] pts contains the new x,y coordinates 
     Log.d("test", "Before Rotate myPoint("+newCoordinates.x+","+newCoordinates.y+")"); 
     myDrawing.invalidate(); 

    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) {} 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) {} 
} 
+0

rl.addView (myDrawing) के 'सार्वजनिक बिंदु घुमाने (फ्लोट डिग्री)' को पढ़ें, मुझे इस पंक्ति में त्रुटि मिली –

6

मैट्रिक्स द्वारा 2 डी बिंदुओं को बदलने के लिए Matrix.mapPoints का उपयोग करें।

+0

उत्तर के लिए धन्यवाद, लेकिन मैंने इसे देखा है और अभी भी समझ में नहीं आता कि समन्वय कैसे सेट करें और फिर घूर्णन के बाद इसे प्राप्त करें। – DecodeGnome

+0

मुझे एक ही समस्या है :( – donmj

+0

कृपया एंटर्रेको के उत्तर –

0

एक लंबे समय देर से मैं जानता हूँ कि, लेकिन यह कुछ ऐसा था मैं भी अभी भी बारे में उलझन में था। एपीआई का यह पूरा क्षेत्र वास्तव में क्या चल रहा है, इस पर हमें देने के बजाय चीजों को करने पर अधिक ध्यान केंद्रित करता है, इसमें कोई संदेह नहीं है क्योंकि यह दृश्यों के पीछे वास्तव में चालाक सामान कर रहा है।

अंक निर्धारित करना और उन्हें वापस लेना काफी अलग है।

एक विशेष बिंदु सेट करने के कई तरीके हैं, एंट्रेको का उत्कृष्ट उत्तर एक तरफ दिखाता है।

एक बिंदु वापस पाने के लिए आपको उस बिंदु से जुड़े मैट्रिक्स के मान प्राप्त करना होगा और उसके बाद सही हिस्सों को चुनें। यह भी उत्कृष्ट, उत्तर (Android Matrix, what does getValues() return?) मैट्रिक्स के साथ क्या स्पष्ट हो रहा है, और आप उससे देख सकते हैं कि x, y मान जो आप चाहते हैं वे तत्व 2 और 5 द्वारा अनुक्रमित तत्व हैं।

निम्नलिखित है (थोड़ा छद्म-) कोड मैं उन पर पहुंचने के लिए उपयोग करता हूं।

float [] theArray = { <nine float zeroes> } 
Matrix m = new Matrix(); 
boolean success = myPathMeasure.getMatrix(m, theArray, Matrix.MTRANS_X+Matrix.MTRANS_Y); 
m.getValues(theArray); 
x = theArray[2]; 
y = theArray[5]; 

मैं इस बारे में बहुत खुश नहीं हूं, लेकिन ऐसा करने का एक और औपचारिक तरीका प्रतीत नहीं होता है।