2010-06-15 20 views
7

मैं एक दृश्य में एक छवि आकर्षित करने के लिए कोशिश कर रहा हूँ, लेकिन मूल छवि के पैमाने को बनाए रखने की कोशिश में समस्या हो रही बंद करो। असल में, मेरे पास एक छोटा सा दृश्य है और मैं दृश्य में छवि का हिस्सा दिखाना चाहता हूं। इरादा तब छवि पर एक अनुवाद करना है ताकि दृश्य में एक अलग हिस्सा दिखाई दे।एंड्रॉयड: छवि का आकार छोटा

कोई फर्क नहीं पड़ता कि मैं क्या प्रयास करता हूं, या तो छवि को दृश्य में फिट करने के लिए स्वचालित रूप से डाउन-स्केल किया गया है या पूरी छवि देखने योग्य है। मैंने बिटमैप ड्रावेबल, इमेज व्यू और लेआउट पर सेटिंग्स के साथ खेलने का प्रयास नहीं किया है।

किसी को भी इस लक्ष्य को हासिल करने के लिए एक अच्छा तरीका पता है?

उत्तर

0

यह शायद सबसे कारगर तरीका नहीं होगा, लेकिन यदि आप इसे बहुत ज्यादा आगे बढ़ जा करने के लिए नहीं जा रहे हैं, यह करना होगा। एक spritesheet है जैसे कि यह इलाज, Bitmap.createBitmap (बिटमैप स्रोत, int x, int y, int चौड़ाई, पूर्णांक ऊंचाई) का उपयोग अनुभाग में आप अपने स्वयं के बिटमैप के रूप में मूल बिटमैप से चाहते हैं, तो पारित कि बजाय में। तब पूरे बिटमैप के बजाय में पारित है कि, और यह केवल हिस्सा आप इसे दिखाना चाहते हैं के साथ काम कर रहे होंगे।

0

लगता है जैसे आप एक छोटे से दृश्य (खिड़की की तरह अभिनय) के अंदर एक बड़ा छवि दृश्य नियंत्रण रखना चाहते हैं, और फिर उसके अंदर छवि दृश्य नियंत्रण को स्थानांतरित करें, इसलिए एक समय में इसका केवल एक हिस्सा दिखाई दे रहा है।

नहीं बिल्कुल यकीन है कि आप कैसे करना चाहते हैं कि हालांकि, लेकिन यकीन है कि यह AbsoluteLayout और फेरबदल का एक सा के साथ संभव है।

2

आशा है कि कोड का यह टुकड़ा मदद करता है। मैंने इसे एक महीने पहले गुगल किया है। यह बड़ी छवियों के लिए प्रदर्शन स्क्रॉलिंग। यहां पूरा प्रदर्शन आकार दृश्य की ऊंचाई और चौड़ाई के रूप में सेट किया गया है। आप आपको बदल सकते हैं। और ज़ूम नियंत्रण भी बनाए रख सकते हैं।

public class LargeImageScroller extends Activity { 

// Physical display width and height. 
private static int displayWidth = 0; 
private static int displayHeight = 0; 

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

    // displayWidth and displayHeight will change depending on screen 
    // orientation. To get these dynamically, we should hook onSizeChanged(). 
    // This simple example uses only landscape mode, so it's ok to get them 
    // once on startup and use those values throughout. 
    Display display = ((WindowManager) 
      getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    displayWidth = display.getWidth(); 
    displayHeight = display.getHeight(); 

    // SampleView constructor must be constructed last as it needs the 
    // displayWidth and displayHeight we just got. 
    setContentView(new SampleView(this)); 
} 

private static class SampleView extends View { 
    private static Bitmap bmLargeImage; //bitmap large enough to be scrolled 
    private static Rect displayRect = null; //rect we display to 
    private Rect scrollRect = null; //rect we scroll over our bitmap with 
    private int scrollRectX = 0; //current left location of scroll rect 
    private int scrollRectY = 0; //current top location of scroll rect 
    private float scrollByX = 0; //x amount to scroll by 
    private float scrollByY = 0; //y amount to scroll by 
    private float startX = 0; //track x from one ACTION_MOVE to the next 
    private float startY = 0; //track y from one ACTION_MOVE to the next 

    public SampleView(Context context) { 
      super(context); 

      // Destination rect for our main canvas draw. It never changes. 
      displayRect = new Rect(0, 0, displayWidth, displayHeight); 
      // Scroll rect: this will be used to 'scroll around' over the 
      // bitmap in memory. Initialize as above. 
      scrollRect = new Rect(0, 0, displayWidth, displayHeight); 

      // Load a large bitmap into an offscreen area of memory. 
      bmLargeImage = BitmapFactory.decodeResource(getResources(), 
       R.drawable.testlargeimage); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        // Remember our initial down event location. 
        startX = event.getRawX(); 
        startY = event.getRawY(); 
        break; 

       case MotionEvent.ACTION_MOVE: 
        float x = event.getRawX(); 
        float y = event.getRawY(); 
        // Calculate move update. This will happen many times 
        // during the course of a single movement gesture. 
        scrollByX = x - startX; //move update x increment 
        scrollByY = y - startY; //move update y increment 
        startX = x; //reset initial values to latest 
        startY = y; 
        invalidate(); //force a redraw 
        break; 
      } 
      return true; //done with this event so consume it 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

      // Our move updates are calculated in ACTION_MOVE in the opposite direction 
      // from how we want to move the scroll rect. Think of this as dragging to 
      // the left being the same as sliding the scroll rect to the right. 
      int newScrollRectX = scrollRectX - (int)scrollByX; 
      int newScrollRectY = scrollRectY - (int)scrollByY; 

      // Don't scroll off the left or right edges of the bitmap. 
      if (newScrollRectX < 0) 
       newScrollRectX = 0; 
      else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth)) 
       newScrollRectX = (bmLargeImage.getWidth() - displayWidth); 

      // Don't scroll off the top or bottom edges of the bitmap. 
      if (newScrollRectY < 0) 
       newScrollRectY = 0; 
      else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight)) 
       newScrollRectY = (bmLargeImage.getHeight() - displayHeight); 

      // We have our updated scroll rect coordinates, set them and draw. 
      scrollRect.set(newScrollRectX, newScrollRectY, 
       newScrollRectX + displayWidth, newScrollRectY + displayHeight); 
      Paint paint = new Paint(); 
      canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint); 

      // Reset current scroll coordinates to reflect the latest updates, 
      // so we can repeat this update process. 
      scrollRectX = newScrollRectX; 
      scrollRectY = newScrollRectY; 

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