2012-01-12 18 views
8

मैं इनपुटस्ट्रीम (संपत्ति, फ़ाइल सिस्टम) से ड्रॉ करने योग्य कैसे लोड कर सकता हूं और इसे स्क्रीन रिज़ॉल्यूशन hdpi, mdpi या ldpi के आधार पर गतिशील रूप से आकार बदल सकता हूं?एंड्रॉइड लोड प्रोग्रामेटिक रूप से ड्रॉइंग और इसे आकार बदलें

मूल छवि hdpi में है, मुझे केवल mdpi और ldpi के आकार बदलने की आवश्यकता है।

क्या कोई जानता है कि एंड्रॉइड ड्रॉबल्स में गतिशील आकार बदलने/res में कैसे करता है?

+0

जिज्ञासा से बाहर की तरह, किसी भी कारण नहीं के लिए यह पहले से आकार (के लिए 'mdpi' और' ldpi') और 'res' निर्देशिका में उससे लिंक कर रहे हैं? –

+0

नेटवर्क से डाउनलोड की गई छवियां। मैं सर्वर पर preprocesing कर सकते हैं लेकिन डाउनलोड धीमा हो जाएगा। – peceps

उत्तर

4

यह मिला: इस तरह

/** 
    * Loads image from file system. 
    * 
    * @param context the application context 
    * @param filename the filename of the image 
    * @param originalDensity the density of the image, it will be automatically 
    * resized to the device density 
    * @return image drawable or null if the image is not found or IO error occurs 
    */ 
    public static Drawable loadImageFromFilesystem(Context context, String filename, int originalDensity) { 
    Drawable drawable = null; 
    InputStream is = null; 

    // set options to resize the image 
    Options opts = new BitmapFactory.Options(); 
    opts.inDensity = originalDensity; 

    try { 
     is = context.openFileInput(filename); 
     drawable = Drawable.createFromResourceStream(context.getResources(), null, is, filename, opts);   
    } catch (Throwable e) { 
     // handle 
    } finally { 
     if (is != null) { 
     try { 
      is.close(); 
     } catch (Throwable e1) { 
      // ingore 
     } 
     } 
    } 
    return drawable; 
    } 

उपयोग:

loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM); 
+1

दुर्भाग्यवश यह कोड एचटीसी डिजायर एचडी और एचटीसी ईवो पर काम नहीं करता है। यहां समाधान देखें: http://stackoverflow.com/questions/7747089/exception-in-drawable-createfromresourcestream-htc-only/9195531#9195531 – peceps

1

आप एक छवि प्रदर्शित करना चाहते हैं लेकिन दुर्भाग्य से इस छवि को बड़े आकार का है, तो उदाहरण देता है, तो आप एक छवि प्रदर्शित करना चाहते हैं 30 से 30 प्रारूप में, उसके आकार की जांच करें यदि यह आपके आवश्यक आकार से अधिक है, तो इसे अपनी राशि (इस मामले में 30 * 30) से विभाजित करें, और आपको जो मिला वह फिर से छवि क्षेत्र को विभाजित करने के लिए लेता है ।

drawable = this.getResources().getDrawable(R.drawable.pirImg); 
int width = drawable.getIntrinsicWidth(); 
int height = drawable.getIntrinsicHeight(); 
if (width > 30)//means if the size of an image is greater than 30*30 
{ 
    width = drawable.getIntrinsicWidth()/30; 
    height = drawable.getIntrinsicWidth()/30; 
} 

drawable.setBounds(
    0, 0, 
    drawable.getIntrinsicWidth()/width, 
    drawable.getIntrinsicHeight()/height); 

//and now add the modified image in your overlay 
overlayitem[i].setMarker(drawable) 
8

यह अच्छा और आसान (अन्य उत्तर मेरे लिए काम नहीं कर रहे थे) है, पाया here:

ImageView iv = (ImageView) findViewById(R.id.imageView); 
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture); 
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true); 
    iv.setImageBitmap(bMapScaled); 

Android दस्तावेज़ उपलब्ध here है।

0

के बाद आप अपनी तस्वीर लोड और imageView आप layoutparamsto आकार का उपयोग कर सकते करने के लिए यह सेट अपनी छवि match_parent को

इस

android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); 
layoutParams.width =MATCH_PARENT; 
layoutParams.height =MATCH_PARENT; 
imageView.setLayoutParams(layoutParams); 
संबंधित मुद्दे