2012-11-14 9 views
6

मैं जो करना चाहता हूं, वह मेरे चयन की स्थिति में स्क्रीन पर एक छवि का कटआउट खींचता है।स्क्रीन पर छवि का हिस्सा बनाएं (सभी को स्मृति में लोड किए बिना)

मैं इसे आसानी से बिटमैप में लोड कर सकता हूं। और उसके बाद एक उपधारा खींचें।

लेकिन जब छवि बड़ी है, तो यह स्पष्ट रूप से स्मृति को समाप्त कर देगा।

मेरी स्क्रीन एक सतहदृश्य है। तो एक कैनवास इत्यादि है

तो मैं किसी दिए गए ऑफ़सेट पर, छवि का हिस्सा कैसे आकर्षित कर सकता हूं और आकार बदल सकता हूं। उत्पत्ति को स्मृति के बिना लोड करने के बिना

मुझे एक उत्तर मिला जो सही रेखाओं के साथ देखा गया था, लेकिन यह ठीक से काम नहीं करता है। फ़ाइल से drawables के उपयोग के साथ। नीचे कोड प्रयास। यादृच्छिक आकार के अलावा यह उत्पादन करता है, यह अधूरा भी है।

उदाहरण:

Example

Drawable img = Drawable.createFromPath(Files.SDCARD + image.rasterName); 

    int drawWidth = (int) (image.GetOSXWidth()/(maxX - minX)) * m_canvas.getWidth();   
    int drawHeight = (int)(image.GetOSYHeight()/(maxY - minY)) * m_canvas.getHeight(); 

    // Calculate what part of image I need... 
    img.setBounds(0, 0, drawWidth, drawHeight); 

    // apply canvas matrix to move before draw...? 
    img.draw(m_canvas); 
+3

'बिटमैपरेगियन डिकोडर 'पर एक नज़र डालें। सोचो कि यह वही करता है जो आप खोज रहे हैं। – bobnoble

+0

@bobnoble धन्यवाद javadocs के माध्यम से देख रहा है, ऐसा लगता है कि यह चाल करेगा। और उपयोग करने के लिए काफी आगे है। – Doomsknight

+0

इसे उत्तर के रूप में लिखें @bobnoble - यह उपयोगी है और दूसरों के लिए जारी रहेगा। – Elemental

उत्तर

5

BitmapRegionDecoder एक छवि के एक निर्धारित क्षेत्र लोड करने के लिए इस्तेमाल किया जा सकता। बिटमैप को दो ImageView एस में सेट करने का एक उदाहरण विधि यहां दिया गया है।

private void configureImageViews() { 

    String path = externalDirectory() + File.separatorChar 
      + "sushi_plate_tokyo_20091119.png"; 

    ImageView fullImageView = (ImageView) findViewById(R.id.fullImageView); 
    ImageView bitmapRegionImageView = (ImageView) findViewById(R.id.bitmapRegionImageView); 

    Bitmap fullBitmap = null; 
    Bitmap regionBitmap = null; 

    try { 
     BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder 
       .newInstance(path, false); 

     // Get the width and height of the full image 
     int fullWidth = bitmapRegionDecoder.getWidth(); 
     int fullHeight = bitmapRegionDecoder.getHeight(); 

     // Get a bitmap of the entire image (full plate of sushi) 
     Rect fullRect = new Rect(0, 0, fullWidth, fullHeight); 
     fullBitmap = bitmapRegionDecoder.decodeRegion(fullRect, null); 

     // Get a bitmap of a region only (eel only) 
     Rect regionRect = new Rect(275, 545, 965, 1025); 
     regionBitmap = bitmapRegionDecoder.decodeRegion(regionRect, null); 

    } catch (IOException e) { 
     // Handle IOException as appropriate 
     e.printStackTrace(); 
    } 

    fullImageView.setImageBitmap(fullBitmap); 
    bitmapRegionImageView.setImageBitmap(regionBitmap); 

} 

// Get the external storage directory 
public static String externalDirectory() { 
    File file = Environment.getExternalStorageDirectory(); 
    return file.getAbsolutePath(); 
} 

परिणाम पूर्ण छवि (ऊपर) है और सिर्फ छवि (नीचे) के एक क्षेत्र:

enter image description here

पहली पूर्ण छवि है, तो भेजें सिर्फ एक पूर्ण छवि का क्षेत्र है
+1

उत्कृष्ट उत्तर धन्यवाद :) – Doomsknight

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

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