
वहाँ है एक समस्या, ओ बग, या वे बस इतना करना चाहता था, में ऑब्जेक्ट ImageView, में प्रोग्रामिंग Android के. यदि आप एक चौड़ाई असाइन करें match_parent उस ऑब्जेक्ट के लिए, और एक ऊँचाई wrap_content, घटना में है कि छवि आप के रूप में रजिस्टर करने के लिए जाओ स्रोत कुछ है उच्च आयाम करने के लिए की ImageView, वहाँ लगता है समस्या कुछ.
को समस्या यह पैदा होती है जब ImageView ऑब्जेक्ट को असाइन किया गया है जो छवि है एक निचले आयाम उस ऑब्जेक्ट के लिए. इस मामले में, छवि मूल आयामों के साथ प्रकट होता है, और कवर नहीं करता है ImageView पूरी तरह से.
ऐसे एक मामले में, रूप में ज्यादा के रूप में प्ले साथ विभिन्न विकल्प और विशेषताएँ ImageView ऑब्जेक्ट, कभी नहीं आप करने के लिए इच्छित परिणाम प्राप्त. आप गुण संशोधित कर सकते हैं scaleType, adjustViewBounds, layout_height, layout_width, … और कुछ भी नहीं, सफलता नहीं होगा.
कैसे एक ImageView की एक छवि बनाने के लिए ऑब्जेक्ट का आकार का आकार पूरी तरह से होगा
के लिए प्राप्त करें का उद्देश्य छवि के ImageView ऑब्जेक्ट का आकार का खोने के बिना इसके अनुपात, समाधान बनाने के लिए है एक नए जावा वर्ग हमारी परियोजना में कि का विस्तार ImageView वर्ग और ठीक करें हम है समस्या.
ऐसा करने के लिए, अपने प्रोजेक्ट में, Crea एक नया वर्ग. हम कॉल करने के लिए जा रहे हैं ResizableImageView, और आपके कोड निम्नानुसार है:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.mypackage; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } public ResizableImageView(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable d = getDrawable(); if (d == null) { super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); return; } int imageHeight = d.getIntrinsicHeight(); int imageWidth = d.getIntrinsicWidth(); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); float imageRatio = 0.0F; if (imageHeight > 0) { imageRatio = imageWidth / imageHeight; } float sizeRatio = 0.0F; if (heightSize > 0) { sizeRatio = widthSize / heightSize; } int width; int height; if (imageRatio >= sizeRatio) { width = widthSize; height = width * imageHeight / imageWidth; } else { height = heightSize; width = height * imageWidth / imageHeight; } setMeasuredDimension(width, height); } } |
एक बार बनाया गया ई लागू किया गया वर्ग ResizableImageView, हम अब क्या है संशोधित करें को XML के लेआउट देखें कहाँ है ImageView sobre el que queremos hacer que se redimensione la imagen.
हमें लगता है कि, पहले परिवर्तित करने के लिए, ImageView XML को बदलने के लिए निम्नानुसार है:
1 2 3 4 5 6 | <ImageView android:id="@+id/miImagen" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="https://cdn1.professor-falken.com/@drawable/imagen" /> |
ठीक है, बस करने के लिए है परिवर्तित करें वर्ग के लिए ऐसे ऑब्जेक्ट की नई बनाई गई वर्ग, निम्नलिखित तरीके में:
1 2 3 4 5 6 | <com.mypackage.ResizableImageView android:id="@+id/miImagen" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="https://cdn1.professor-falken.com/@drawable/imagen" /> |
इस के साथ वर्ग और में परिवर्तन XML कोड, हम उस ऑब्जेक्ट के भीतर निहित छवि हासिल होगा ImageView का आकार बदलेगा, इसकी समानता को बनाए रखने, यहां तक कि मामलों में एक आयाम से कंटेनर छोटे करने के लिए कौन-सी छवि में.
