2012-08-28 14 views
53

में कोण विशेषता मैं परीक्षण उदाहरण के माध्यम से जा रहा हूं। जहां वे ढाल का उपयोग कर रहे कुछ छवि पृष्ठभूमि के लिए, कोड इसएंड्रॉइड ग्रेडियेंट

<?xml version="1.0" encoding="utf-8"?> 


    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="#ff0000" 
     android:centerColor="#00ff00" 
     android:endColor="#0000ff" 
     android:angle="180"/> 
    <corners android:radius="5dp" /> 
    </shape> 

की तरह ऊपर एक्सएमएल में चला जाता है मैं angle विशेषता नहीं मिला। लेकिन जब मैं angle के पैटर्न को थोड़ा सा पैटर्न स्लंट बदलता हूं। क्या कोई मुझे बता सकता है कि यह वास्तव में कैसे काम करता है ........... :)

उत्तर

120

ग्रेडियंट मूल रूप से किसी भी मात्रा के स्थान (दिशा में) में भिन्नता का प्रतिनिधित्व करता है। रंग के साथ यह कोण द्वारा प्रतिनिधित्व दिशा में रंग तीव्रता की विविधता का प्रतिनिधित्व करता है। यहाँ कुछ चित्र इस अवधारणा का प्रतिनिधित्व कर रहे हैं:
enter image description here

यहाँ आंकड़ा (कोण 0 सेट किया गया है) क्षैतिज दिशा में रंग भिन्नता को दर्शाता है।
XML कोड:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="#000000" 
     android:angle="0"/> 
    </shape> 

enter image description here

यहाँ आंकड़ा क्षैतिज दिशा में रंग भिन्नता (कोण 90 सेट किया गया है) को दर्शाता है।
XML कोड:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<gradient 
    android:startColor="#000000" 
    android:angle="90"/> 
</shape> 

आप प्रारंभ, केंद्र और अंत रंग के रूप में अलग रंग का उपयोग कर सकते हैं। आपके द्वारा संलग्न कोड में इन सभी तत्व शामिल हैं।

+17

जवाब कर्ण लिए धन्यवाद, लेकिन एक बात मैंने पाया कि मैं कोण विशेषता में 45 में से केवल गुणकों, की तुलना में यह है कि यह यानी दुर्घटनाओं अन्य दे सकते हैं 20, 20, 50 आदि –

+0

ऐसे कई तरीके हैं, जिसमें ढाल हो सकता है व्यक्त की है। यहां हम वास्तव में रैखिक ढाल का उपयोग कर रहे हैं (एक सीधी रेखा ढलान एम = (वाई-वाई 1)/(x-x1) के साथ ढलान)। जब यह 45 का एकाधिक होता है तो एक्स और वाई में भिन्नता समान होती है। इस कारण का कारण हो सकता है। मुझे ज्यादा नहीं पता। – karn

+4

व्याख्यान का उत्कृष्ट उपयोग यह समझाते हुए! – Ralphleon

3

आकार के लिए एक ढाल रंग निर्दिष्ट करता है। गुण:

एंड्रॉयड: कोण पूर्णांक। ग्रेडियेंट के लिए कोण, डिग्री में। 0 दाएं से बाएं है, 90 नीचे से नीचे है। यह 45 का एक बहु होना चाहिए। डिफ़ॉल्ट 0.

ऐसा लगता है कि प्रलेखन में विवरण कर्ना के उत्तर के विपरीत है ??

आप documentation

4

में अधिक जानकारी के आप कोड से विकर्ण ढाल बनाने चाहते हो सकता है पा सकते हैं। यह बहुत आसान है और आपके पास वहां से बहुत सारे विकल्प खुले हैं। यह स्निपेट मुझे GradientDrawable वर्ग

/*public enum Orientation { 
     *//** draw the gradient from the top to the bottom *//* 
     TOP_BOTTOM, 
     *//** draw the gradient from the top-right to the bottom-left *//* 
     TR_BL, 
     *//** draw the gradient from the right to the left *//* 
     RIGHT_LEFT, 
     *//** draw the gradient from the bottom-right to the top-left *//* 
     BR_TL, 
     *//** draw the gradient from the bottom to the top *//* 
     BOTTOM_TOP, 
     *//** draw the gradient from the bottom-left to the top-right *//* 
     BL_TR, 
     *//** draw the gradient from the left to the right *//* 
     LEFT_RIGHT, 
     *//** draw the gradient from the top-left to the bottom-right *//* 
     TL_BR, 
    }*/ 

से

public void SetGradient(View view) { 
     GradientDrawable gd = new GradientDrawable(
       GradientDrawable.Orientation.TL_BR, 
       new int[]{0xFF141a24, 0xFF293f49, 0xFF72554c}); 
     view.setBackground(gd); 
    } 

उपलब्ध दिशाओं मदद की है और आप टुकड़ा में onCreate या onCreateView से विधि कॉल और (मेरे मामले में) जनक दृश्य गुजरती हैं।

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.dialog_view_parent, container);   
     ... 

     SetGradient(view); 

     return view; 
    } 
संबंधित मुद्दे