2012-01-15 15 views
6

मेरे पास एंड्रॉइड में निम्न श्रेणी है, जहां मैं स्क्रीन पर बुलबुले की गति की गणना करने के लिए कुछ फ़ंक्शंस को परिभाषित कर रहा हूं।मैं रन टाइम पर स्क्रीन ऊंचाई और चौड़ाई की गणना कैसे कर सकता हूं?

public class floatBubble { 
    private Bitmap img; // the image of the ball 
    private int coordX = 512; // the x coordinate at the canvas 
    private int coordY = 600; // the y coordinate at the canvas 
    private int id; // gives every ball his own id, for now not necessary 
    private static int count = 1; 
    private boolean goRight = true; 
    private boolean goDown = true; 
    public floatBubble(Context context, int drawable) { 
    BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inJustDecodeBounds = true; 
     img = BitmapFactory.decodeResource(context.getResources(), drawable); 
     id=count; 
    count++; 
    } 

    public static int getCount() { 
     return count; 
    } 
    void setX(int newValue) { 
     coordX = newValue; 
     } 
    public int getX() { 
     return coordX; 
    } 
    void setY(int newValue) { 
     coordY = newValue; 
     } 
    public int getY() { 
     return coordY; 
    } 

    public int getID() { 
     return id; 
    } 
    public Bitmap getBitmap() { 
     return img; 
    } 

    public void moveBall(int goX, int goY) { 
     // check the borders, and set the direction if a border has reached 

     if (coordX > 1024){ 
      goRight = false; 
     } 
     if (coordX < -100){ 
      goRight = false; 
      coordX = 512; 
      coordY = 600; 
     } 
     if (coordY > 600){ 
      goDown = false; 
     } 
     if (coordY < -100){ 
      coordY = 600; 
      goDown = false; 
     } 
     // move the x and y 
     if (goRight){ 
      coordX += goX; 
     }else 
     { 
      coordX -= goX; 
     } 
     if (goDown){ 
      coordY += goY; 
     }else 
     { 
      coordY -= goY; 
     } 
    } 
} 

स्क्रीन रिज़ॉल्यूशन 1024 * 600px के लिए यह है। लेकिन मैं रनटाइम पर स्क्रीनसाइज की गणना करना चाहता हूं। मतलब रनटाइम पर समन्वय और समन्वय को परिभाषित करने की आवश्यकता है। क्या आप कोड को संशोधित करने में मेरी मदद कर सकते हैं।

मैंने अपने moveBall() में निम्न कोड का उपयोग करने का प्रयास किया, लेकिन कोड वहां काम नहीं किया।

DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm);  
coordX = dm.widthPixels; 
coordY = dm.heightPixels; 

कोई भी समाधान सुझा सकता है?

उत्तर

10

आप पिक्सल में प्रदर्शन आयामों आप

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 

उपयोग कर सकते हैं आप एक गतिविधि पर getWindowManager कॉल करने के लिए के लिए उपयोग नहीं है, तो चाहते हैं। आप इसका उपयोग कर सकते हैं:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 
0

यह इस कक्षा में भी बनाया जा सकता है।

package com.example.android.fragments; 

import android.app.Activity; 
import android.util.DisplayMetrics; 
import android.view.Display; 

public class ScreenUtility { 

    private Activity activity; 
    private float dpWidth; 
    private float dpHeight; 

    public ScreenUtility(Activity activity) { 
     this.activity = activity; 

     Display display = activity.getWindowManager().getDefaultDisplay(); 
     DisplayMetrics outMetrics = new DisplayMetrics(); 
     display.getMetrics(outMetrics); 

     float density = activity.getResources().getDisplayMetrics().density; 
     dpHeight = outMetrics.heightPixels/density; 
     dpWidth = outMetrics.widthPixels/density; 
    } 

    public float getWidth() { 
     return dpWidth; 
    } 

    public float getHeight() { 
     return dpHeight; 
    } 

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

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