2017-02-18 8 views
10

मैं यह पता लगाने की कोशिश कर रहा हूं कि एंड्रॉइड क्या कर रहा है जब यह छवि को स्केल करता है, खासकर "सेंटरक्रॉप" प्रकार। तो एक जवाब खोजने के लिए मैंने ImageView स्रोत कोड की खोज की और इसे here ढूंढें।छविदृश्य स्केलिंग विधि "सेंटरक्रॉप" कोड

तो क्या मैंने कोशिश की इस कोड है:

public Bitmap buildBluredBoxBackground() { 
     int [] screenSize = Utilities.getScreenSize(mainActivityContext); //screensize[0] = x and [1] is y 
     Matrix mDrawMatrix = new Matrix(); 

     Bitmap bitmap = ((BitmapDrawable)fullscreenViewHolder.imageViewArt.getDrawable()).getBitmap(); 

     float scale; 
     float dx = 0, dy = 0; 

     if (bitmap.getWidth() * screenSize[1] > screenSize[0] * bitmap.getHeight()) { 
      scale = (float) screenSize[1]/(float) bitmap.getHeight(); 
      dx = (screenSize[0] - bitmap.getWidth() * scale) * 0.5f; 
     } else { 
      scale = (float) screenSize[0]/(float) bitmap.getWidth(); 
      dy = (screenSize[1] - bitmap.getHeight() * scale) * 0.5f; 
     } 

     mDrawMatrix.setScale(scale, scale); 
     mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy)); 

     result = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),mDrawMatrix,true); 

     ... //Some processing work 

     return result; 
} 

लेकिन यह मुझे एक ही परिणाम दे रही है नहीं है। मैं क्या गलत कर रहा हूं ?

यहाँ एक उदाहरण है:

मूल चित्र

enter image description here

मूल imageView Centercrop

enter image description here

की कोशिश की संहिता

enter image description here

संपादित करें: imageView

<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/imageViewFullscreenArt"/> 
      <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/imageViewFullscreenArtBluredBox"/> 
</FrameLayout> 

तो मेरी imageView की एक्सएमएल fullscreened है। मैं इसे संसाधित करने के लिए स्क्रीन आकार का उपयोग क्यों कर रहा हूं।

कोड मैं इसे कैसे

Bitmap bluredBoxBackground = buildBluredBoxBackground(); 
imageViewBluredBox.setImageDrawable(new BitmapDrawable(getResources(),bluredBoxBackground)); 

विस्तृत विवरण को लागू करने हूँ: इम सिर्फ ImageView.setScaleType(ScaleType.CENTER_CROP) के रूप में एक ही प्रभाव प्राप्त करने की कोशिश। तो मेरा कोड मूल setScaleType विधि जैसा ही होना चाहिए। मुझे कोड के रूप में इसकी आवश्यकता क्यों है? क्योंकि मेरी स्थिति में मुझे अपने इमेज व्यू का ड्रॉइंग कैश नहीं मिल रहा है, लेकिन मुझे & को किसी भी तरह से संपादित करना है।

+0

पोस्ट पूरा drawable के लिए कोड? आप इसे ImageView पर कैसे लागू कर रहे हैं? –

+0

1) आपको 'ImageView' से बिटमैप मिल रहा है जहां ड्रायबल मूल से बदल दिया गया हो सकता है; 2) आप लक्ष्य दृश्य के आकार की बजाय स्क्रीन के आकार का उपयोग कर रहे हैं; 3) आप 'scaleType = "matrix" 'का उपयोग करने के बजाए एक स्केल्ड बिटमैप बना रहे हैं। मुझे यह समझने के लिए और अधिक कोड देखने की आवश्यकता है कि आप क्या हासिल करने की कोशिश कर रहे हैं, लेकिन यह एक साधारण फिक्स होना चाहिए। एक्सएमएल लेआउट के साथ अपनी गतिविधि के लिए कोड पोस्ट करें जिसमें लक्ष्य दृश्य है, जिसमें आप जिस समस्या को हल करने की कोशिश कर रहे हैं, उसके बारे में थोड़ा अधिक विस्तृत विवरण के साथ। –

+0

मैंने अपनी पोस्ट को कुछ कोड और मेरी समस्या के बेहतर विवरण के साथ संपादित किया। आशा है कि इस बार यह समझने के लिए स्पष्ट है। आपकी मदद के लिए धन्यवाद –

उत्तर

1

मैंने teh स्रोत से अनुकूलित किया। यह आपके साथ काम करेगा मैट्रिक्स में वापसी को कैसे बदलता है।

public Matrix buildBluredBoxBackground() { 

    int dwidth = imageView.getDrawable().getIntrinsicWidth(); 
    int dheight = imageView.getDrawable().getIntrinsicHeight(); 

    int vwidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight(); 
    int vheight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom(); 

    Matrix mDrawMatrix = imageView.getImageMatrix(); 
    Bitmap bMap = imageView.getDrawingCache(); 

    float scale; 
    float dx = 0, dy = 0; 

    if (dwidth * vheight > vwidth * dheight) { 
     scale = (float) vheight/(float) dheight; 
     dx = (vwidth - dwidth * scale) * 0.5f; 
    } else { 
     scale = (float) vwidth/(float) dwidth; 
     dy = (vheight - dheight * scale) * 0.5f; 
    } 
    mDrawMatrix.setScale(scale, scale); 
    mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy)); 
    return mDrawMatrix; 
} 

और फिर उपयोग करें:

Matrix bluredBoxBackground = buildBluredBoxBackground(); 
imageViewBluredBox.setImageMatrix(bluredBoxBackground)); 
imageViewBluredBox.invalidate(); 
संबंधित मुद्दे