2012-11-29 14 views
10

मैं ImageView दिखाने की कोशिश कर रहा हूं जो हमेशा एक संवाद के अंदर आकार में वर्ग होता है। आयाम के प्रदर्शन के आधार पर आयाम भिन्न हो सकते हैं (चित्र में चौड़ाई सटीक होने के लिए) लेकिन छवि दृश्य को इसके अंदर एक वर्ग छवि को समायोजित करने के लिए सबसे बड़ा स्क्वायर संभव बनाने के लिए आवश्यक है। यह मेरा एक्सएमएल कोड है।स्क्वायर छवि दृश्य

<ImageView 
    android:id="@+id/dialog_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="centerInside" 
    android:adjustViewBounds="true" 
    /> 

लेकिन समस्या यह है कि Square छवि कार्यावधि में गतिशील रूप से सेट कर दिया जाता है और imageView स्क्वायर छवि का आकार के रूप में विस्तार के साथ एक आयत किया जा रहा समाप्त होता है। मैं इसे प्राप्त करने के लिए क्या कर सकता हूं?

संपादित करें: मैं जो हासिल करने की कोशिश कर रहा हूं उसका एक विचार देने के लिए संपर्क अनुप्रयोग में संपर्क का चयन करते समय दिखाया गया है और प्रोफ़ाइल चित्र क्लिक किया गया है। यह ज़ूम आउट करता है और गतिविधि के शीर्ष पर एक वर्ग के रूप में रहता है।

उत्तर

13

रन टाइम पर चित्र दृश्य ऊंचाई सेट लेकिन पहले इस कोड

Display display = getWindowManager().getDefaultDisplay(); 
int swidth = display.getWidth(); 

के साथ प्रदर्शन चौड़ाई हो और अब इस

LayoutParams params = eventImage.getLayoutParams(); 
params.width = LayoutParams.FILL_PARENT; 
params.height = swidth ; 
eventImage.setLayoutParams(params); 
+0

तुम बहुत धन्यवाद प्रतिक्रिया के लिए बहुत कुछ। एक जादू की तरह काम करता है। – Shakti

3

की तरह छवि देखने की ऊंचाई सेट के लिए एक अलग वर्ग बनाएं अपनी छवि दृश्य के लिए गतिशील रूप से आकार को परिभाषित करना जो कि नीचे दिखाए गए अनुसार छवि दृश्य वर्ग का उत्तराधिकारी होना चाहिए:

public class SquareImageView extends ImageView { 

public SquareImageView(Context context) { 
    super(context); 
} 

public SquareImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 

int width = getMeasuredWidth(); 
    setMeasuredDimension(width, width); 

} 

}

सेटमेस्चरडिमेंशन (चौड़ाई, चौड़ाई); स्वचालित रूप से चौड़ाई के रूप में अपनी ऊंचाई निर्धारित करेगा। जैसा कि नीचे दिखाया

xml फ़ाइल में imageView के स्थान पर एक दृश्य के रूप में इस वर्ग का उपयोग करें:

<com.packagepath.SquareImageView 
     android:id="@+id/Imageview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
+0

बहुत धन्यवाद, यह बहुत अच्छा काम किया –

+0

उर स्वागत .. :) – HAXM

0

बनाएं अपनी खुद की ImageView कि imageView वर्ग के रूप में नीचे दिखाया गया वारिस चाहिए:

public class SquareImageView extends AppCompatImageView { 
    public SquareImageView(Context context) { 
     super(context); 
    } 

    public SquareImageView(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 
     int size; 

     //If the layout_width or layout_width of this view is set as match_parent or any exact dimension, then this view will use that dimension 
     if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY^MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) { 
      if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) 
       size = width; 
      else 
       size = height; 
     } 
     //If the layout_width and layout_width of this view are not set or both set as match_parent or any exact dimension, 
     // then this view will use the minimum dimension 
     else 
      size = Math.min(width, height); 
     setMeasuredDimension(size, size); 
    } 
} 
संबंधित मुद्दे