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

所属分类: 软件编程 / Android 阅读数: 1916
收藏 0 赞 0 分享

我们要实现的效果如下:

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

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

1文件存放位置

res/drawable/filename.xml

2、语法

<?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"] />

例子:

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

 

引用(\@drawable/bm):

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

 titileMode="repeat":

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

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

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

如果过小则如下:

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

2、自定义控件绘制

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

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

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

wave_item:

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

wave.xml:

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

<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软件编程有所帮助。

更多精彩内容其他人还在看

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多