Pejman
Pejman

Reputation: 2636

Android, special drawable

I need to create a drawable with some special behavior on resizing:

Pejman Chatrrooz

is that even possible ? and I don't want to use nine-patch and I've tried this :

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <shape android:shape="rectangle">
      <size android:width="10dp" />
      <solid android:color="@color/black" />
    </shape>
  </item>

  <item android:left="10dp">
    <shape android:shape="rectangle">
      <size android:width="10dp" />
      <solid android:color="@color/blue" />
    </shape>
  </item>

</layer-list>

Upvotes: 1

Views: 101

Answers (1)

pskink
pskink

Reputation: 24720

i dont think you can do it in pure xml, so try this custom Drawable:

public class StripDrawable extends Drawable {

    private static final int FIRST_COLOR = 0xff3a3a3a;
    private static final int SECOND_COLOR = 0xff04dfff;
    private static final float STRIP_WIDTH = 10;
    private Paint mPaint;
    private float mWidth;

    public StripDrawable(Context ctx) {
        mPaint = new Paint();
        mWidth = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            STRIP_WIDTH,
            ctx.getResources().getDisplayMetrics());
    }

    @Override
    public void draw(Canvas canvas) {
        Rect b = getBounds();
        float w = b.left;
        while (w < b.right) {
            mPaint.setColor(FIRST_COLOR);
            canvas.drawRect(w, b.top, w + mWidth, b.bottom, mPaint);
            w += mWidth;
            mPaint.setColor(SECOND_COLOR);
            canvas.drawRect(w, b.top, w + mWidth, b.bottom, mPaint);
            w += mWidth;
        }
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

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

Upvotes: 1

Related Questions