android 背景切换动画效果代码,关于Android shape gradient背景渐变
百度后,发现渐变色不仅可以根据xml来实现,也可以用java代码来实现,由于目前没有那么多时间,只记录xml实现的方法;以后在记录Java实现的代码。
通过Shape gradient标签来实现
首先来看gradient标签所有的渐变属性:
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
attributes:
android:angle Integer,代表渐变颜色的角度, 0 is left to right, 90 is bottom to top. 必须是45的整数倍. 默认是 0.该属性只有在type=linear情况下起作用,默认的type为linear。
android:startColor
Color. 颜色渐变的开始颜色
android:endColor
Color. 颜色渐变的结束颜色
android:centerColor
Color. 颜色渐变的中间颜色
android:centerX
Float.(0 - 1.0) 相对X的渐变位置。
android:centerY
Float.(0 - 1.0) 相对Y的渐变位置。
centerX和centerY,这两个属性只有在type不为linear情况下起作用。
android:gradientRadius
Float. 渐变颜色的半径,单位应该是像素点。需要 android:type="radial"。
如果android:type="radial",没有设置android:gradientRadius,将会报错,error inflating class。
android:type
linear : 线性渐变
radial:A radial gradient. 放射性渐变(圆形渐变),起始颜色从cenralX,centralY点开始。
sweep:A sweeping line gradient. 扫描式渐变(扇形渐变)。
android:useLevel
使用LevelListDrawable时就要设置为true,设为false时才有渐变效果。
gradient的属性介绍完毕,下面看一下各种渐变的效果。
渐变的效果:
线性渐变的xml代码:
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:startColor="#ff0000"
android:type="linear"/>
放射性渐变xml代码:
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:gradientRadius="100"
android:startColor="#ff0000"
android:type="radial"/>
扫描式渐变xml代码:
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:startColor="#ff0000"
android:type="sweep"/>
三种渐变效果,如图所示:
通过GradientDrawable来实现
了解GradientDrawable类
查看文档的时候,先找到ShapeDrawable类,发现xml属性只有几个,如图所示:
和shape的xml属性对应不上,随后又找到GradientDrawable类,发现许多shape在xml中熟悉的属性,如图所示:
在源码中找到:
setStroke(int width, @ColorInt int color)
setStroke(int width, ColorStateList colorStateList)
setStroke(int width, @ColorInt int color, float dashWidth, float dashGap)
setStroke(int width, ColorStateList colorStateList, float dashWidth, float dashGap)
对应的是shape xml里面的stroke标签。这里由于只是了解颜色渐变相关,所以仅仅对相关的方法做分析。
下面来看GradientDrawable颜色渐变相关的方法
setGradientType(@GradientType int gradient)
setGradientType对应gradient标签android:type属性,对应的值有:LINEAR_GRADIENT,RADIAL_GRADIENT,SWEEP_GRADIENT;分别是:线性渐变,放射性渐变,扫描式渐变。
setColor(@ColorInt int argb)
setColors(@ColorInt int[] colors)
setColor(@Nullable ColorStateList colorStateList)
setColors设置渐变的颜色,包含一种,至少两种颜色等。对应gradient标签android:startColor、android:centerColor和android:endColor;但是java方法好像更加灵活,可以放多于三种颜色。
setOrientation(Orientation orientation)
setOrientation设置线性渐变的方向。对应gradient标签android:angle,可以取的值有:
/**
* 控制渐变相对于可绘制边界的方向
*/
public enum Orientation {
/**
* 从顶部到底部绘制渐变
*/
TOP_BOTTOM,
/**
* 绘制从右上角到左下角的渐变
*/
TR_BL,
/**
* 从右到左绘制渐变
*/
RIGHT_LEFT,
/**
* 从右下角到左上角绘制渐变
*/
BR_TL,
/**
* 从底部到顶部绘制渐变
*/
BOTTOM_TOP,
/**
* 从左下角到右上角绘制渐变
*/
BL_TR,
/**
* 从左到右绘制渐变
*/
LEFT_RIGHT,
/**
* 绘制从左上角到右下角的渐变
*/
TL_BR,
}
setGradientRadius(float gradientRadius)
setGradientRadius设置渐变的半径。,只有当渐变类型设置为{RADIAL_GRADIENT}时,半径才有效。对应gradient标签android:gradientRadius。
Java代码中设置渐变效果
线性渐变的xml代码:
int[] colors = new int[]{Color.parseColor("#FF0000"), Color.parseColor("#00FF00"),
Color.parseColor("#0000FF")};
GradientDrawable linearDrawable = new GradientDrawable();
linearDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
linearDrawable.setColors(colors);
linearDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
mGradient1.setBackground(linearDrawable);
放射性渐变xml代码:
GradientDrawable radialDrawable = new GradientDrawable();
radialDrawable.setColors(colors);
radialDrawable.setShape(GradientDrawable.OVAL);
radialDrawable.setGradientRadius(10f);
radialDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
mGradient2.setBackground(radialDrawable);
扫描式渐变xml代码:
GradientDrawable sweepDrawable = new GradientDrawable();
sweepDrawable.setColors(colors);
sweepDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);
mGradient3.setBackground(sweepDrawable);
java代码设置的三种渐变效果,如图所示:
android 背景切换动画效果代码,关于Android shape gradient背景渐变相关推荐
- android 背景切换动画效果代码,在Android应用中以模糊效果设置背景图片
我试图使列表视图上的背景图像模糊,但是我尝试按照教程进行操作,但它不起作用.任何人都请指教,谢谢. 主要活动 public class IngredientCategoryMain extends A ...
- android 翻书动画效果怎么做,android ViewPager实现滑动翻页效果实例代码
实现ViewPager的滑动翻页效果可以使用ViewPager的setPageTransformer方法,如下: import android.content.Context; import andr ...
- android 仿ios动画效果代码,Android仿IOS上拉下拉弹性效果的实例代码
用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好:Android并没有给我们封装这样一个效果,我们来看下在Android里如何实现这个效果.先看效果,感觉有些时候还 ...
- android 图标切换动画效果,BezierCurve
运用贝塞尔曲线描绘android翻页效果(浏览图片时,图片间切换达到书本翻页的效果). 效果图: 如何使用首先你需要创建Pager实例,并添加到布局中DisplayMetrics displayMet ...
- Android 动画之View动画效果和Activity切换动画效果
View动画效果: 1.>>Tween动画 通过对View的内容进行一系列的图形变换(平移.缩放.旋转.透明度变换)实现动画效果,补间动画需要使用<set>节点作为根节点,子节 ...
- android动画篇——仿小米计算器切换动画效果
实现思路 此效果实现用到了:LayoutTransition和ObjectAnimator的相关知识 思路:当点击切换按钮时顶部ViewGroup和左侧ViewGroup可见性设置为Gone,并自动开 ...
- android学习笔记---56_activity切换动画与页面切换动画,自定义activity窗口切换动画效果的实现.
2013/5/17 Java技术qq交流群:JavaDream:251572072 56_activity切换动画与页面切换动画 ----------------------------------- ...
- android淡入淡出动画循环,Android应用开发之淡入淡出、缩放、旋转、平移、组合动画效果代码实现...
本文将带你了解Android应用开发Android动画开发之淡入淡出.缩放.旋转.平移.组合动画效果代码实现,希望本文对大家学Android有所帮助. 1.activity_main.xml文件 an ...
- android+百叶窗动画效果,js实现横向百叶窗效果网页切换动画效果的方法
本文实例讲述了js实现横向百叶窗效果网页切换动画效果的方法.分享给大家供大家参考.具体分析如下: 这是一款很简洁但是效果却不错的网页切换效果,点击新网页后,网页会出现蓝白相间的百叶窗线条自动切换,直到 ...
最新文章
- 写一个 iOS 复杂表单的正确姿势
- jvm六:主动使用(1.new一个对象, 2.反射)
- [POI2005]BAN-Bank Notes
- OpenCV SURF检测的实例(附完整代码)
- [转]CocoaPods安装和使用教程
- 从零配置webpack(react+less+typescript+mobx)
- spring源码 学习方法
- 【深入浅出】Java中 this关键字的四种用法
- 学刘红杰老师博客营销,知如何提高博客访问流量
- python函数之间变量的调用
- python k线斜率计算_在Python中计算斜率的方法
- 基于自适应决策算子的鲸鱼优化算法-附代码
- 腾讯云函数要收费了,试试阿里云的云效codeup吧,这里有教程
- Android开发两年,我要跳槽去阿里巴巴了,做个阶段总结,flutterrow高度对齐
- STM32cubeIDE生成HEX文件
- kettle carte子服务器的使用
- 关于手机系统。。。。
- python学习(一)数据类型和运算符
- matlab多项式计算题目,MATLAB数据分析与多项式计算-习题答案
- Linux入门学习(六)—— 怎么更改文件的所有者、所属组?以及怎么更改系统创建的默认权限?