2015-03-25 106 views
0

目前我有3個可繪製的XML文件定義3個獨立的漸變。這些漸變動態設置爲我的代碼中的imageView的背景顏色(這是工作正常)。我可以在一個Drawable XML中使用多個漸變嗎?

例如:繪製\ morningsky.xml

<?xml version="1.0" encoding="utf-8"?> 
<item android:bottom="4dp"> 
    <shape> 
    <gradient 
     android:startColor="@color/blue" 
     android:endColor="@color/dark_blue" 
     android:angle="270" /> 
    </shape> 

例如:繪製\ eveningsky.xml

<?xml version="1.0" encoding="utf-8"?> 
<item android:bottom="4dp"> 
    <shape> 
    <gradient 
     android:startColor="@color/orange" 
     android:endColor="@color/yellow" 
     android:angle="270" /> 
    </shape> 

我設置在我的ImageView這樣的背景:

iv.setBackgroundResource(R.drawable.morningsky); 

一切都很好,但是我真的需要爲每個漸變使用多個不同的可繪製資源文件嗎?有什麼辦法可以在單個可繪製文件中定義所有漸變,然後從我的代碼中加載該漸變?

+0

看到這個問題:http://stackoverflow.com/questions/5702143/multipe-shapes-inside-shapes-xml-in-android – samgak 2015-03-25 07:24:01

+0

嗨Samgak,我得到了一個XML中添加多個標籤的一部分,但我需要要知道如何從我的代碼中引用特定的? – BuggyCoder 2015-03-25 07:28:52

+1

iv.getBackground()。setLevel(1);設置爲正確的索引(不是資源ID)。您需要將級別列表設置爲背景已經 – samgak 2015-03-25 08:19:56

回答

0

您可以使用選擇

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_pressed="true" android:drawable="@drawable\eveningsky.xml"/> 
     <item android:state_selected="true" android:drawable="@drawable\morningsky.xml"/> 
     <item android:drawable="@drawable\morningsky.xml"/> 
    </selector> 
0

您可以通過編程做到這一點:

public void setShadeBackground(View v){ 
    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() { 
     @Override 
     public Shader resize(int width, int height) { 
      LinearGradient linearGradient = new LinearGradient(0, 0, width, height, 
        new int[] { 
          0xFF2583ED, 
          0xFF8220C9, 
          0xFFBC0BCC, 
          0xFFD625ED , 
          0xFFD407AE , 
          0xFFF2115C }, //substitute the correct colors for these 
        new float[] { 
          0, 0.30f,0.50f, 0.60f,0.70f, 1 }, 
        Shader.TileMode.REPEAT); 
      return linearGradient; 
     } 
    }; 
    PaintDrawable paint = new PaintDrawable(); 
    paint.setShape(new RectShape()); 
    paint.setShaderFactory(shaderFactory); 
    v.setBackgroundDrawable((Drawable)paint); 

} 
0

你可以使用這樣的事情

<?xml version="1.0" encoding="utf-8"?> 
 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
 
    <item> 
 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
 
      android:shape="rectangle"> 
 
      <gradient 
 
       android:type="radial" 
 
       android:gradientRadius="300" 
 
       android:startColor="@android:color/white" 
 
       android:endColor="@android:color/transparent" 
 
       android:centerX="0.25" 
 
       android:centerY="0.5"/> 
 
     </shape> 
 
    </item> 
 
    <item> 
 
     <shape android:shape="rectangle"> 
 
      <gradient 
 
       android:type="radial" 
 
       android:gradientRadius="300" 
 
       android:startColor="@android:color/white" 
 
       android:endColor="@android:color/transparent" 
 
       android:centerX="0.75" 
 
       android:centerY="0.5"/> 
 
     </shape> 
 
    </item> 
 
</layer-list>

相關問題