2016-06-30 9 views

Antwort

2

es von xml tun:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:right="100dp"> 
    <shape android:shape="rectangle"> 
     <size android:height="100dp" android:width="100dp"/> 
     <solid android:color="@android:color/black"/> 
    </shape> 
</item> 

<item android:left="100dp"> 
    <shape android:shape="rectangle"> 
     <size android:height="100dp" android:width="100dp"/> 
     <solid android:color="@android:color/holo_green_light"/> 
    </shape> 
</item> 
</layer-list> 

Setzen Sie es in res/drawable Ordner und zuweisen als android:background zu Bild

+0

Vielen Dank. Diese Lösung kommt meinem Ziel am nächsten. – Egis

0

können Sie versuchen, durch zwei Formen mit entsprechenden Farben zu schaffen und sie dann verwenden.

Sie können diese Formen erstellen, indem Sie xml für sie schreiben und die Abmessungen und Farben angeben.

0

Es ist möglich. Sie können:

1) Erstellen Sie eine Form

<shape xmlns:android="http://schemas.android.com/apk/res/android" 

    android:shape="rectangle" > 

    <stroke 
    android:width="1dp" 
    android:color="@color/gray_light" /> 

    <gradient 
    android:type="linear" 
    android:centerX="0" 
    android:centerY="1" 
    android:startColor="#bff54a" 
    android:endColor="#88c010" /> 

    <corners 
    android:bottomLeftRadius="8dp" 
    android:bottomRightRadius="8dp" 
    android:topLeftRadius="8dp" 
    android:topRightRadius="8dp" /> 

</shape> 

2) erweitert die ziehbar Klasse

public class ColorBarDrawable extends Drawable { 

    private int[] themeColors; 

    public ColorBarDrawable(int[] themeColors) { 
     this.themeColors = themeColors; 
    } 

    @Override 
    public void draw(Canvas canvas) { 

     // get drawable dimensions 
     Rect bounds = getBounds(); 

     int width = bounds.right - bounds.left; 
     int height = bounds.bottom - bounds.top; 

     // draw background gradient 
     Paint backgroundPaint = new Paint(); 
     int barWidth = width/themeColors.length; 
     int barWidthRemainder = width % themeColors.length; 
     for (int i = 0; i < themeColors.length; i++) { 
      backgroundPaint.setColor(themeColors[i]); 
      canvas.drawRect(i * barWidth, 0, (i + 1) * barWidth, height, backgroundPaint); 
     } 

     // draw remainder, if exists 
     if (barWidthRemainder > 0) { 
      canvas.drawRect(themeColors.length * barWidth, 0, themeColors.length * barWidth + barWidthRemainder, height, backgroundPaint); 
     } 

    } 

    @Override 
    public void setAlpha(int alpha) { 
    } 

    @Override 
    public void setColorFilter(ColorFilter cf) { 

    } 

    @Override 
    public int getOpacity() { 
     return PixelFormat.OPAQUE; 
    } 

} 

Quelle: Chiral-Code - Stackoverflow