服务器之家

服务器之家 > 正文

Android实现波浪线效果(xml bitmap)

时间:2021-05-07 16:12     来源/作者:lwklan

我们要实现的效果如下:

Android实现波浪线效果(xml bitmap)

在这之前先带大家了解一下xml bitmap,何为xml bitmap

xml bitmap 是一个用xml定义的文件放在资源目录,定义的对象是图片,为bitmap定义别名,这个文件可以给bitmap定义一些额外的属性。例如:抖动。

1文件存放位置
res/drawable/filename.xml

2、语法

?
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<bitmap
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:src="@[package:]drawable/drawable_resource"
  android:antialias=["true" | "false"]
  android:dither=["true" | "false"]
  android:filter=["true" | "false"]
  android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
           "fill_vertical" | "center_horizontal" | "fill_horizontal" |
           "center" | "fill" | "clip_vertical" | "clip_horizontal"]
  android:tilemode=["disabled" | "clamp" | "repeat" | "mirror"] />

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<relativelayout 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"
  tools:context=".mainactivity" >
  <!-- 与默认情况(@drawable/btn_default_pressed_holo_light)有差别 -->
  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerhorizontal="true"
    android:layout_centervertical="true"
    android:background="@drawable/bm"
    android:text="sssssssssssssssssss" />
 
</relativelayout>
 
<relativelayout 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"
  tools:context=".mainactivity" >
 <!-- 与默认情况(@drawable/btn_default_pressed_holo_light)有差别 -->
  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerhorizontal="true"
    android:layout_centervertical="true"
    android:background="@drawable/bm"
    android:text="sssssssssssssssssss" />
 
</relativelayout>

效果图:

 默认(@drawable/btn_default_pressed_holo_light):

 Android实现波浪线效果(xml bitmap)

引用(\@drawable/bm):

Android实现波浪线效果(xml bitmap)

从截图上就可以看出来差别。

 titilemode="repeat":

Android实现波浪线效果(xml bitmap)

之后找了一些关于实现波浪线的方法,总感觉不大满意,常见的方法分享给大家:

1、直接搞一个这样的波浪线的切图

      这种方式最简单,但是劣势也非常明显,如果view的宽度过大,则会出现图片变形,如下图所示:

 Android实现波浪线效果(xml bitmap)

如果过小则如下:

Android实现波浪线效果(xml bitmap)

要求不高的话,这样勉强可以蒙混过关,但是追求完美的话,这样的效果显然很不给力

2、自定义控件绘制

     这个方法的确可以达到不错的效果,但是实现比较麻烦。我也看到有朋友搞过,

3、用xml的bitmap标签实现波浪线效果

      首页要搞一个周期的波浪线,即包括波峰和波谷,如下图所示:

      wave_item:

      Android实现波浪线效果(xml bitmap)

    然后水平方向上平铺,从而实现波浪线效果,代码如下:
wave.xml:

?
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
  android:antialias="true"
  android:src="@drawable/wave_item"
  android:tilemode="repeat" />

activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<linearlayout 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"
  android:orientation="vertical"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingtop="@dimen/activity_vertical_margin" >
 
  <imageview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/wave" />
 
</linearlayout>

 注意:这个imageview通过background显示图片,src是显示不了这样的效果的。

希望本文所述对大家学习android软件编程有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部