2017-06-25 7 views
9

मेरे पास कस्टम व्यू और एक छवि दृश्य है। कस्टमव्यू एक गेंद है जो स्क्रीन के चारों ओर घूमती है और दीवारों से उछालती है। छवि एक चौथाई चक्र है जिसे आप स्पर्श पर एक सर्कल में घूम सकते हैं। मैं अपना गेम बनाने की कोशिश कर रहा हूं ताकि जब ImageView से भरने वाले पिक्सल के साथ कस्टमव्यू क्रॉस पथ से भरे पिक्सेल एक टक्कर का पता लगाया जाए। मेरी समस्या यह है कि मुझे नहीं पता कि कैसे प्रत्येक पिक्सेल पर भरे पिक्सल हैं।कस्टम दृश्य और एक छवि दृश्य के बीच पिक्सेल परफेक्ट टकराव का पता लगाने

यहाँ मेरी XML कोड

<com.leytontaylor.bouncyballz.AnimatedView 
    android:id="@+id/anim_view" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    /> 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/quartCircle" 
    android:layout_gravity="center_horizontal" 
    android:src="@drawable/quartercircle" 
    android:scaleType="matrix"/> 

मेरे MainActivity है

public class MainActivity extends AppCompatActivity { 

private static Bitmap imageOriginal, imageScaled; 
private static Matrix matrix; 

private ImageView dialer; 
private int dialerHeight, dialerWidth; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // load the image only once 
    if (imageOriginal == null) { 
     imageOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.quartercircle); 
    } 

    // initialize the matrix only once 
    if (matrix == null) { 
     matrix = new Matrix(); 
    } else { 
     // not needed, you can also post the matrix immediately to restore the old state 
     matrix.reset(); 
    } 

    dialer = (ImageView) findViewById(R.id.quartCircle); 
    dialer.setOnTouchListener(new MyOnTouchListener()); 
    dialer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
      // method called more than once, but the values only need to be initialized one time 
      if (dialerHeight == 0 || dialerWidth == 0) { 
       dialerHeight = dialer.getHeight(); 
       dialerWidth = dialer.getWidth(); 

       // resize 
       Matrix resize = new Matrix(); 
       resize.postScale((float) Math.min(dialerWidth, dialerHeight)/(float) imageOriginal.getWidth(), (float) Math.min(dialerWidth, dialerHeight)/(float) imageOriginal.getHeight()); 
       imageScaled = Bitmap.createBitmap(imageOriginal, 0, 0, imageOriginal.getWidth(), imageOriginal.getHeight(), resize, false); 

       // translate to the image view's center 
       float translateX = dialerWidth/2 - imageScaled.getWidth()/2; 
       float translateY = dialerHeight/2 - imageScaled.getHeight()/2; 
       matrix.postTranslate(translateX, translateY); 

       dialer.setImageBitmap(imageScaled); 
       dialer.setImageMatrix(matrix); 

      } 
     } 
    }); 
} 

MyOnTouchListener वर्ग:

private class MyOnTouchListener implements View.OnTouchListener { 

    private double startAngle; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch (event.getAction()) { 

      case MotionEvent.ACTION_DOWN: 
       startAngle = getAngle(event.getX(), event.getY()); 
       break; 

      case MotionEvent.ACTION_MOVE: 
       double currentAngle = getAngle(event.getX(), event.getY()); 
       rotateDialer((float) (startAngle - currentAngle)); 
       startAngle = currentAngle; 
       break; 

      case MotionEvent.ACTION_UP: 
       break; 
     } 
     return true; 
    } 
} 

private double getAngle(double xTouch, double yTouch) { 
    double x = xTouch - (dialerWidth/2d); 
    double y = dialerHeight - yTouch - (dialerHeight/2d); 

    switch (getQuadrant(x, y)) { 
     case 1: 
      return Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     case 2: 
      return 180 - Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     case 3: 
      return 180 + (-1 * Math.asin(y/Math.hypot(x, y)) * 180/Math.PI); 
     case 4: 
      return 360 + Math.asin(y/Math.hypot(x, y)) * 180/Math.PI; 
     default: 
      return 0; 
    } 
} 

/** 
* @return The selected quadrant. 
*/ 
private static int getQuadrant(double x, double y) { 
    if (x >= 0) { 
     return y >= 0 ? 1 : 4; 
    } else { 
     return y >= 0 ? 2 : 3; 
    } 
} 

/** 
* Rotate the dialer. 
* 
* @param degrees The degrees, the dialer should get rotated. 
*/ 
private void rotateDialer(float degrees) { 
    matrix.postRotate(degrees, dialerWidth/2, dialerHeight/2); 

    dialer.setImageMatrix(matrix); 
} 

और मेरे AnimatedView

public class AnimatedView extends ImageView { 

private Context mContext; 
int x = -1; 
int y = -1; 
private int xVelocity = 10; 
private int yVelocity = 5; 
private Handler h; 
private final int FRAME_RATE = 60; 

public AnimatedView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mContext = context; 
    h = new Handler(); 
} 

private Runnable r = new Runnable() { 
    @Override 
    public void run() { 
     invalidate(); 
    } 
}; 

protected void onDraw(Canvas c) { 
    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.smallerball); 

    if (x<0 && y <0) { 
     x = this.getWidth()/2; 
     y = this.getHeight()/2; 
    } else { 
     x += xVelocity; 
     y += yVelocity; 
     if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
      xVelocity = xVelocity*-1; 
     } 
     if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) { 
      yVelocity = yVelocity*-1; 
     } 
    } 

    c.drawBitmap(ball.getBitmap(), x, y, null); 
    h.postDelayed(r, FRAME_RATE); 
} 

public float getX() { 
    return x; 
} 

public float getY() { 
    return y; 
} 

} 

मेरा सवाल है: मैं इन दोनों विचारों से भरे पिक्सल को कैसे प्राप्त कर सकता हूं, और टकराव का पता लगाने वाले फ़ंक्शन के माध्यम से उन्हें पास कर सकता हूं।

मदद के लिए अग्रिम धन्यवाद! :)

+4

'पिक्सेल-परिपूर्ण' और 'एंड्रॉइड' कभी अच्छे दोस्त नहीं रहे हैं। –

+0

@ रोटवांग एनिमेटेड ऑब्जेक्ट्स की टकराव का पता लगाने के लिए आप क्या सुझाव देंगे? – Leyton

+0

मुझे नहीं पता। मैं गेमिंग में नहीं हूं। मुझे लगता है कि स्क्रीन घनत्व के आनुपातिक कुछ प्रकार की समन्वय प्रणाली। –

उत्तर

1

आपको वास्तव में "भरे पिक्सल" को परिभाषित करने की आवश्यकता है। मुझे लगता है कि आप गैर पारदर्शी पिक्सल का मतलब है। उनको ढूंढने का सबसे आसान तरीका, अपने पूरे दृश्य को बिटमैप में परिवर्तित करना और इसके पिक्सेल के माध्यम से पुनरावृत्ति करना है। आप इस प्रकार का Bitmap में एक View परिवर्तित कर सकते हैं: तुम सिर्फ प्रत्येक के पिक्सल के माध्यम से पुनरावृति करने के लिए है तो फिर

Point getViewLocationOnScreen(View v) { 
    int[] coor = new int[]{0, 0}; 
    v.getLocationOnScreen(coor); 
    return new Point(coor[0], coor[1]); 
} 

:

private Bitmap getBitmapFromView(View view) { 
    view.buildDrawingCache(); 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.measuredWidth, 
      view.measuredHeight, 
      Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(returnedBitmap); 
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN); 
    Drawable drawable = view.background; 
    drawable.draw(canvas); 
    view.draw(canvas); 
    return returnedBitmap; 
} 

तुम भी विचारों की पूर्ण स्थान प्राप्त करने की आवश्यकता होगी Bitmap, अपने रंगों को यह जानने के लिए जांचें कि क्या वे "भरे" हैं, और यह भी जांचें कि क्या वे बिटमैप्स के अंदर उनके समन्वय और स्क्रीन पर विचारों के पूर्ण स्थान के आधार पर ओवरलैपिंग कर रहे हैं या नहीं।

एक बिटमैप के माध्यम से बार-बार दोहराना इस तरह से किया जाता है:

for (int x = 0; x < myBitmap.getWidth(); x++) { 
    for (int y = 0; y < myBitmap.getHeight(); y++) { 
     int color = myBitmap.getPixel(x, y); 
    } 
} 

लेकिन मैं मैं यूआई धागे पर भारी संगणना की इस तरह के प्रदर्शन वास्तव में एक अच्छा विचार है नहीं लगता है, कहना होगा। पिक्सेल-सही जांच से टकराव का पता लगाने के दर्जनों बेहतर तरीके हैं। यह शायद बेहद कमजोर हो जाएगा।

1

आप अपने सीमा निर्देशांक अंकन अंक की सरणी के साथ अपने चित्रों को संपुटित सकता है (और/स्थानांतरित पर उन्हें अपडेट उन्हें मूल के आधार पर गणना) तो आप कर सकेंगे यह तय करें कि ऑब्जेक्ट ऑब्जेक्ट स्पर्श में है या नहीं (यदि कोई भी ओपनिंग एरे एक ही बिंदु साझा करता है)

संबंधित मुद्दे