2011-11-13 11 views

उत्तर

85

आपका प्रश्न कर रहा है कि तुम क्या के बारे में जानकारी का एक सा कम है पूरा करना चाहते हैं, लेकिन मुझे लगता है कि आपके पास बिटमैप है और इसे एक नए आकार में स्केल करना चाहते हैं और स्केलिंग डॉन होना चाहिए ई "सेंटरक्रॉप" के रूप में ImageViews के लिए काम करता है।

से Docs

स्केल छवि समान रूप से (छवि के पक्ष अनुपात को बनाए रखने) ताकि छवि के दोनों आयाम (चौड़ाई और ऊंचाई) के बराबर या देखने की इसी आयाम से बड़ा होगा (माइनस पैडिंग)।

जहाँ तक मुझे पता है, कोई एक लाइनर यह करने के लिए (कृपया मुझे सही करें, अगर मैं गलत हूँ) है, लेकिन आप यह करने के लिए अपनी खुद की विधि लिख सकते हैं। निम्न विधि गणना करता है कि मूल बिटमैप को नए आकार में कैसे स्केल करें और परिणामी बिटमैप में इसे केंद्रित करें।

उम्मीद है कि यह मदद करता है!

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
    int sourceWidth = source.getWidth(); 
    int sourceHeight = source.getHeight(); 

    // Compute the scaling factors to fit the new height and width, respectively. 
    // To cover the final image, the final scaling will be the bigger 
    // of these two. 
    float xScale = (float) newWidth/sourceWidth; 
    float yScale = (float) newHeight/sourceHeight; 
    float scale = Math.max(xScale, yScale); 

    // Now get the size of the source bitmap when scaled 
    float scaledWidth = scale * sourceWidth; 
    float scaledHeight = scale * sourceHeight; 

    // Let's find out the upper left coordinates if the scaled bitmap 
    // should be centered in the new size give by the parameters 
    float left = (newWidth - scaledWidth)/2; 
    float top = (newHeight - scaledHeight)/2; 

    // The target rectangle for the new, scaled version of the source bitmap will now 
    // be 
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 

    // Finally, we create a new bitmap of the specified size and draw our new, 
    // scaled bitmap onto it. 
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
    Canvas canvas = new Canvas(dest); 
    canvas.drawBitmap(source, null, targetRect, null); 

    return dest; 
} 
+0

हाय, क्या मुझे पता है कि मैं इसे इस बिटमैप से एक सफेद सीमा कैसे दे सकता हूं? – ericlee

+0

क्या आप स्केल किए गए बिटमैप के शीर्ष पर एक सफेद सीमा चाहते हैं या क्या आप एक सफेद सीमा चाहते हैं, जैसे पैडिंग, इसके आसपास? – Albin

+0

@ericlee यह एक नया सवाल है। – StackOverflowed

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