2014-06-24 9 views
5

मैं यूनिवर्सल छवि लोडर के साथ एक छवि लोड कर रहा हूं, और मैं इसे स्केल करना चाहता हूं ताकि चौड़ाई स्क्रीन की चौड़ाई हो और ऊंचाई तदनुसार स्केल हो, जिसका अर्थ है कि इससे पहले मैं उस छवि को लोड करता हूं जिसे मैं चाहता हूं कि चौड़ाई मुझे चाहिए, लेकिन ऊंचाई नहीं। तो जब मैं छवि लोड करता हूं तो मैं छवि की ऊंचाई और चौड़ाई प्राप्त करना चाहता हूं और स्क्रीन की चौड़ाई के अनुसार इसे स्केल करने के लिए उपयोग करता हूं। कोड मैं यह करने के लिए उपयोग यह है:सार्वभौमिक छवि लोडर के साथ इसे लोड करने के बाद बिटमैप का आकार बदलना

try { 
    display.getSize(size); 
    scaledWidth = size.x; 
} catch (java.lang.NoSuchMethodError ignore) { 
    scaledWidth = display.getWidth(); 
} 

String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png"; 
Bitmap bitmap = imageLoader.loadImageSync(filePath); 
int height = bitmap.getHeight(); 
int width = bitmap.getWidth(); 
scaledHeight = (int) (((scaledWidth * 1.0)/width) * height); 
//Code to resize Bitmap using scaledWidth and scaledHeight 

क्या यूनिवर्सल छवि लोडर भी बेहतर उपयोग करते हुए, या बिटमैप आकार बदलने के लिए सबसे अच्छा तरीका है, वहाँ एक रास्ता है कि मैं केवल चौड़ाई निर्दिष्ट कर सकते हैं और बिटमैप माप लिया जाता है ठीक से इसके अनुपात पर आधारित है?

+0

http://stackoverflow.com/questions/4837715/how-to-resize काम करेंगे पर विचार करें -ए-बिटमैप-इन-एंड्रॉइड – kupsef

+0

मैं सुझाव दूंगा कि आप [पिकासो लाइब्रेरी] (http://square.github.io/picasso/) का उपयोग करें। यह अभी भी लचीला होने के दौरान, बहुत आसान छवि लोडिंग और हेरफेर की अनुमति देता है। –

+0

मेरा समाधान @ nitesh-goel code और @hhaoyuanjie 'displayImageOptions' पर आधारित है लेकिन 'ImageScaleType.EXACTLY_STRETCHED' के साथ है। ध्यान दें कि यदि आप इसे बहुत अधिक स्केल करते हैं तो छवि धुंधली दिखाई देगी। – Leukipp

उत्तर

1

उपयोग

// Load image, decode it to Bitmap and return Bitmap to callback 
ImageSize targetSize = new ImageSize(120, 80); // result Bitmap will be fit to this size 
imageLoader.loadImage(imageUri, targetSize, displayOptions, new SimpleImageLoadingListener() { 
    @Override 
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
     // Do whatever you want with Bitmap 
    } 
}); 
0

उपयोग कर सकते हैं मैं अपने कोड नहीं पढ़ा है लेकिन ऊंचाई निर्धारित करने के लिए गणना बहुत आसान कर रहे हैं। हम जो चाहते हैं वह छवि के पहलू अनुपात को बरकरार रखना है। तो सबसे पहले गणना पीस हेतु यानी

imageHeight/imageWidth = aspt_ratio; 

तो यह ऑपरेशन लेने के लिए और ऊंचाई पाने के लिए वर्तमान स्क्रीन चौड़ाई के साथ गुणा करें।

scaledHeight = aspt_ratio*screen_width; 

क्योंकि हम जानते हैं कि छवि की स्केल की चौड़ाई हमेशा आपकी चौड़ाई के अनुसार स्क्रीन चौड़ाई के बराबर होगी।

0

ImageScaleType.EXACTLY का उपयोग कर यह आप की जरूरत के रूप में

scaledWidth = size.x; 

String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png"; 

    android.graphics.BitmapFactory.Options options= new Options(); 
     options.inJustDecodeBounds=true; 
//Just gets image size without allocating memory 
BitmapFactory.decodeFile(filePath, options); 


int height = options.outHeight; 
int width = bitmap.outWidth; 
scaledHeight = (int) (((scaledWidth * 1.0)/width) * height); 


ImageSize targetSize = new ImageSize(scaledWidth, scaledHeight); 
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, displayOptions); 
संबंधित मुद्दे