XML을 사용하면 드로어 블을 만들 수 있습니다. 드로어 블의 절반은 color1이고 다른 절반은 color2가 될 수 있습니다. 드로어 블을 뷰의 배경으로 설정하면 아래 그림과 같이 보입니다.두 가지 색상으로 구성된 android drawable을 만드는 방법은 무엇입니까?
2
A
답변
2
XML에 의해 그 일 :
<?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>
res/drawable
폴더에 넣고
0
당신은 각각의 색상이 개 모양을 만들어 시도하고 사용할 수 있습니다.
치수 및 색상을 제공하는 xml을 작성하여 이러한 모양을 만들 수 있습니다.
0
가능합니다. 당신은 할 수 있습니다
1) 모양
<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
)를 만들기 드로어 블 클래스를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;
}
}
소스를 확장 : 키랄 코드 - StackOverflow의
에
android:background
폴더로 지정하십시오. 이 솔루션은 제가 달성하고자하는 것에 가장 가깝습니다. – Egis