安卓利用shpae画图,用XML文件做出渐变的效果
2017-03-02 16:28
495 查看
需求:要完成颜色的渐变。
做法:使用 xml文件,结合 shape完成
————————————————————————————————————————————
设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml
写法儿如下:
[html] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
startColor : 设置渐变颜色的开始值
endColor: 设置渐变颜色的结束值
angle : 设置渐变的角度
90 :从下往上开始渐变
0 :从左往右开始渐变
-->
<gradient
android:startColor="#FFF"
android:endColor="#030"
android:angle="90"
/>
</shape>
shape是用来定义形状的,gradient定义该形状里面为渐变色填充,startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。
Shape文件实现颜色渐变在项目中的使用方法:
实现过程:
第一步:在 res/drawable/目录下定义 xml文件:
/TestColor/res/drawable/color_shape.xml
[html] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
startColor : 设置渐变颜色的开始值
endColor: 设置渐变颜色的结束值
angle : 设置渐变的角度
90 :从下往上开始渐变
0 :从左往右开始渐变
-->
<gradient
android:startColor="#FFF"
android:endColor="#030"
android:angle="90"
/>
</shape>
第二步:在项目布局文件中,加入引用/TestColor/res/layout/activity_main.xml
[html] view
plain copy
print?
<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"
android:background="@drawable/color_shape"
tools:context=".MainActivity" >
</RelativeLayout>
第三步:在代码中调用 activity_main 即可:
[java] view
plain copy
print?
package com.zhangeng.testcolor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这便是一个完整的,使用 shape 完成控制颜色渐变的实例。
效果图 如下:
做法:使用 xml文件,结合 shape完成
————————————————————————————————————————————
设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml
写法儿如下:
[html] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 矩形的圆角半径 --> <!--<corners android:radius="10dip" />-->
<!--
startColor : 设置渐变颜色的开始值
endColor: 设置渐变颜色的结束值
angle : 设置渐变的角度
90 :从下往上开始渐变
0 :从左往右开始渐变
-->
<gradient
android:startColor="#FFF"
android:endColor="#030"
android:angle="90"
/>
</shape>
shape是用来定义形状的,gradient定义该形状里面为渐变色填充,startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。
Shape文件实现颜色渐变在项目中的使用方法:
实现过程:
第一步:在 res/drawable/目录下定义 xml文件:
/TestColor/res/drawable/color_shape.xml
[html] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
startColor : 设置渐变颜色的开始值
endColor: 设置渐变颜色的结束值
angle : 设置渐变的角度
90 :从下往上开始渐变
0 :从左往右开始渐变
-->
<gradient
android:startColor="#FFF"
android:endColor="#030"
android:angle="90"
/>
</shape>
第二步:在项目布局文件中,加入引用/TestColor/res/layout/activity_main.xml
[html] view
plain copy
print?
<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"
android:background="@drawable/color_shape"
tools:context=".MainActivity" >
</RelativeLayout>
第三步:在代码中调用 activity_main 即可:
[java] view
plain copy
print?
package com.zhangeng.testcolor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这便是一个完整的,使用 shape 完成控制颜色渐变的实例。
效果图 如下:
相关文章推荐
- jq利用 animate和mouseenter 做出一些渐变效果
- (转)利用GradientFill函数实现颜色渐变效果
- 利用CSS实现渐变效果,使网站更美观
- 安卓中自定义view控件代替radiogroup实现颜色渐变效果的写法
- weex开发实战(5)--利用wxc-tabbar实现类似安卓fragment分页效果
- 利用原生js做出拖拽弹射效果
- 利用GradientFill函数实现颜色渐变效果
- ppt中怎么利用符号做出分层显示效果?
- 安卓开发笔记——PopupWindow,做出如弹出框效果
- 利用JPCT-AE在安卓设备上进行*.obj文件的加载和显示
- 个人安卓学习笔记---分别利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- 利用css3做出立体折叠效果
- 利用轮廓检测做出绘画效果
- cell的自适应 根据cell上面的内容 动态返回cell的高度 还有利用NSMutableAttributedString 做出效果
- 如何在一张图片上添加多个链接 ,如何利用js做出靠右侧浮动菜单栏的效果
- 利用CADisplayLink 和 CAGradientLayer来实现文字渐变效果
- silverlight画图 虚线 反射效果 射线渐变效果Radial Gradient
- jpct_ae 安卓创建3d视图效果 jpct的基本使用
- 安卓中利用Handler实现定时器的效果
- 安卓自定义View——网易颜色渐变效果指示器