简体   繁体   English

我如何使用形状而不是图像创建条纹背景

[英]How I create a background with stripes using shapes, not image

I was wondering how I can create a background with lines of different colors without actually add a background image. 我想知道如何在不实际添加背景图像的情况下使用不同颜色的线条创建背景。 I wanted to solve it using any shape or some code in java. 我想用java中的任何形状或代码来解决它。

Thank you very much! 非常感谢你! 在此输入图像描述

This is my 9 patch (I called it /res/drawable/stripes_vert_4.9.png ): 这是我的9补丁(我称之为/res/drawable/stripes_vert_4.9.png ):

在此输入图像描述 <= LOOK! <=看 It's here! 它在这里! It's 14*5 px in size, including the borders. 它的尺寸为14 * 5像素 ,包括边框。 Weight: 205 bytes 重量: 205个字节

This is the result when set as background: 这是设置为背景时的结果:

在此输入图像描述

Now a semi-opaque gradient (I called it /res/drawable/bg_stripes_vert_gradient ): 现在是一个半透明的渐变(我称之为/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 start|center|endColor to get the best balance. 使用start | center | endColor进行游戏以获得最佳平衡。
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 ultimate result: 最终的结果是:

在此输入图像描述

You can use dummy views in your layout file. 您可以在布局文件中使用虚拟视图。 Dummy views are just simple "view" objects in an xml layout file. 虚拟视图只是xml布局文件中的简单“视图”对象。 you can set width, heigh, backgroundcolor, padding, ... on these views. 你可以在这些视图上设置宽度,高度,背景颜色,填充...... They're especially useful when creating horizontal/vertical dividers or stripes in android. 它们在android中创建水平/垂直分隔线或条纹时特别有用。 This is how you can use them to achieve a background, just like the image in your question.Instead of color for the background of this views, you can define a gradient as an xml file in your drawable folder : 这就是你如何使用它们来实现背景,就像你问题中的图像一样。这个视图的背景不是颜色,你可以在你的drawable文件夹中将渐变定义为xml文件:


Here I have used a linear layout to put 4 of these views in vertical orientation, while each of them occupies 25% of screen-width real estate, on any screen size. 在这里,我使用线性布局将这些视图中的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 />

There are good answers here already. 这里有很好的答案。 This method is meant to show that it is not too hard to solve these kinds of problems with a custom view, if you know a few tricks. 这个方法旨在表明,如果您了解一些技巧,使用自定义视图解决这些问题并不太难。 Details can be found in this article: 详细信息可以在本文中找到:

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

Here's the code: 这是代码:

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);
}
}

The idea is to use basic 2d drawing commands to pre-draw a simple shape into a small bitmap that will be automatically scaled up to fit the dimensions of the view when it is drawn. 我们的想法是使用基本的2D绘图命令将一个简单的形状预先绘制成一个小的位图,该位图将在绘制时自动按比例放大以适合视图的尺寸。

The drawBitmap() call in the onDraw() method is fast, according to the article linked, so this method is compatible with dynamic or animated displays. 根据链接的文章, onDraw()方法中的drawBitmap()调用很快,因此该方法与动态或动画显示兼容。

Edit: a screenshot: 编辑:截图:

简单布局中的自定义视图

Edit: I didn't point out how to include the custom view in a layout. 编辑:我没有指出如何在布局中包含自定义视图。 Here is a basic example. 这是一个基本的例子。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建带有两个垂直彩色条纹的TextView背景? - How to create a TextView background with two vertical coloured stripes? 如何在App Inventor中创建背景变量,以便可以使用微调器更改画布的背景图像 - How to create a background variable in App Inventor, so i can change the background image of a canvas using spinner 如何为该图像创建9个贴片,角上有2个形状? - How to create nine patch for this image with 2 shapes in the corners? 如何清除图像中的条纹? android camera2 fps或banding问题 - How can I clear off the stripes in the image? android camera2 fps or banding problem 如何在android中创建形状并在该形状上添加文本和图像? - How to create shapes in android and add text and image on the shape? 如何针对可触摸和可拖动的背景图像创建小部件? - How can I create a widget against a background image that is touchable and dragable? 如何创建带有背景图像和居中缩略图的扩展工具栏? - How can I create an expanded toolbar with background image and a centered thumbnail? 如何在Eclipse中创建自动滑动图像的背景 - How do i create a background of auto sliding image in eclipse 如何在Android Studio中的活动中创建移动的背景图像 - How can I create a moving background image in an activity in android studio 了解如何创建形状 - understanding how to create shapes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM