How to create a background with stripes using shapes, not an image

I was wondering how I can create a background with lines of different colors without actually adding a background image. I wanted to solve it using any form or code in java.

Many thanks! enter image description here

+4
source share
3 answers

This is my 9th patch (I named it /res/drawable/stripes_vert_4.9.png):

enter image description here<= WATCH! He's here! It is 14 * 5 pixels in size, including borders. Weight: 205 bytes

This is the result given as the background:

enter image description here

Now a translucent gradient (I named it /res/drawable/bg_stripes_vert_gradient):

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <gradient
        android:startColor="#0000"
        android:endColor="#8000"
        android:gradientRadius="180"
        android:type="linear"
        android:angle="-90"
    />
</shape>

Play with the beginning | center | endColor to get a better balance.
Now the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/stripes_vert_4"
    >
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_stripes_vert_gradient"
    />
</RelativeLayout>

And the end result:

enter image description here

+5

. - - "" xml. , , backgroundcolor, padding,... . / . , , . xml :


, 4 , 25% .

   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:weightSum="1">

       <View 
         android:id="@+id/stripe_one"
         android:layout_width="0dp"
         android:layout_height="match_parent"
         android:layout_weight="0.25"
         android:background="@color/red"  />

       <View 
         android:id="@+id/stripe_two"
         android:layout_width="0dp"
         android:layout_height="match_parent"
         android:layout_weight="0.25"
         android:background="@color/orange"  />

       <View 
         android:id="@+id/stripe_three"
         android:layout_width="0dp"
         android:layout_height="match_parent"
         android:layout_weight="0.25"
         android:background="@color/velvet"  />

       <View 
         android:id="@+id/stripe_four"
         android:layout_width="0dp"
         android:layout_height="match_parent"
         android:layout_weight="0.25"
         android:background="@color/green"  />

  <LinearLayout />
+2

. , , . :

https://developer.android.com/guide/topics/graphics/2d-graphics.html

:

package com.example;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
Bitmap bitmap;
Paint paint;
Rect dst;

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomView(Context context) {
    super(context);
    init();
}

void init() {
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL_AND_STROKE);

    bitmap = Bitmap.createBitmap(4, 1, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);

    paint.setColor(Color.parseColor("#c43134"));
    c.drawRect(0, 0, 1, 1, paint);

    paint.setColor(Color.parseColor("#b3632e"));
    c.drawRect(1, 0, 2, 1, paint);

    paint.setColor(Color.parseColor("#ab2463"));
    c.drawRect(2, 0, 3, 1, paint);

    paint.setColor(Color.parseColor("#8e9e34"));
    c.drawRect(3, 0, 4, 1, paint);
}

protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(bitmap, (Rect) null, dst, paint);
}

protected void onSizeChanged(int x, int y, int ox, int oy) {
    dst = new Rect(0, 0, x, y);
}
}

, 2d, , , .

drawBitmap() onDraw() , , .

: :

The custom view in a simple layout

: , . .

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.CustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>
+1

Source: https://habr.com/ru/post/1542479/


All Articles