2012-10-11 19 views
8

मैं छविदृश्य में छवि का केवल कुछ भाग दिखाना चाहता हूं। निम्नलिखित छवि देखें। image partएंड्रॉइड - छवि के कुछ हिस्से को कैसे कटौती करें और इसे छविदृश्य में दिखाएं

वही उदाहरण google + एप में पाया जा सकता है जहां आप छवियों के साथ सभी पोस्ट देखते हैं।

कोई भी लिंक, कोड सहायक होगा। धन्यवाद

उत्तर

4

इस कोड

int width = bitmapOrg.width(); 
int height = bitmapOrg.height(); 
int newWidth = 200; 
int newHeight = 200; 

// calculate the scale - in this case = 0.4f 
float scaleWidth = ((float) newWidth)/width; 
float scaleHeight = ((float) newHeight)/height; 

// createa matrix for the manipulation 
Matrix matrix = new Matrix(); 
// resize the bit map 
matrix.postScale(scaleWidth, scaleHeight); 

// recreate the new Bitmap 
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
        width, height, matrix, true); 

// make a Drawable from Bitmap to allow to set the BitMap 
// to the ImageView, ImageButton or what ever 
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

ImageView imageView = new ImageView(this); 

// set the Drawable on the ImageView 
imageView.setImageDrawable(bmd); 
+0

कैसे newWidth के बराबर सेट करने के लिए डिवाइस चौड़ाई ताकि छवि स्क्रीन चौड़ाई के अनुसार छवि स्केल की जाएगी? – rohit

+0

int newWidth = getWindowManager()। GetDefaultDisplay()। GetWidth(); – Deepika

+0

बीटीडब्ल्यू एक समस्या - यह छवि खींच रहा है, मैं छवि को खिंचाव नहीं करना चाहता, मैं छवि के हिस्से को काटना चाहता हूं जैसा कि मैंने उदाहरण में दिखाया है .. अधिक सामान्यतः मैं इसे फसल करना चाहता हूं :) – rohit

6
// Set some constants 
private static final Bitmap SOURCE_BITMAP = BitmapFactory.decodeFile(....); // Get the source Bitmap using your favorite method :-) 
private static final int START_X = 10; 
private static final int START_Y = 15; 
private static final int WIDTH_PX = 100; 
private static final int HEIGHT_PX = 100; 

// Crop bitmap 
Bitmap newBitmap = Bitmap.createBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false); 

// Assign new bitmap to ImageView 
ImageView image = (ImageView)findViewById(R.id.image_view); 
image.setImageBitmap(newBitmap); 
+0

मैंने कोशिश की है लेकिन मैं नहीं कर सकता डिवाइस स्क्रीन आकार के अनुसार नई छवि चौड़ाई सेट करें, गहरीका उत्तर – rohit

+0

पर प्रदर्शन संख्या 1 देखें डिस्प्ले = ((विंडोमैनेजर) getSystemService (Context.WINDOW_SERVICE) प्राप्त करें। getDefaultDisplay(); int screenWidth = display.getWidth(); –

0

कोई इस तर्क के साथ

y नीचे

// recreate the new Bitmap 
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, x, y, 
        width, height, matrix, true); 

से छवि में कटौती निम्नलिखित पैरामीटर को बदलने के लिए लग रही है, तो उपयोग: (bitmapOrg .getHeight() - 1) - (प्रश्न में मौजूद लाल बॉक्स की ऊंचाई)

ऊंचाई: (सवाल में लाल बॉक्स की ऊंचाई)

इस तरह आप जैसे अपवादों से बच सकते हैं (के लिए एक्स आप तदनुसार संशोधित करने की आवश्यकता)

IllegalArgumentException: x + width must be <= bitmap.width() in android 

IllegalArgumentException: y + width must be <= bitmap.height() in android 

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

  • कोई संबंधित समस्या नहीं^_^