Android 利用xml实现各种样式等
2016-05-21 16:10
323 查看
1. Android实现圆角边框### 转自
设置边框圆角可以在drawable-mdpi目录里定义一个xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#000000" /> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/> </shape>
解释:solid的表示填充颜色,为了简单,这里用的是黑色。
而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。
当然上面的效果也可以像下面一样设置,如下:
<corners android:radius="5dp" />
如果想引用这个xml,只需要@drawable/corners_bg.xml即可:
android:background="@drawable/corners_bg"
2.Android 使用XML做动画UI的深入解析
首先: 创建anim文件夹放置动画xml文件
在res文件夹下,创建一个anim的子文件夹。
以下是一些基本的动画XML:
1、淡入动画alpha_in: 淡入
alpha是渐变透明度效果,值由0到1
在anim目录下创建xml文件
alpha_in:.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <alpha android:duration="1000" android:fromAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="1.0" />
2、alpha_out: : 淡出
以alpha_in刚好相反,值由1到0.
alpha_out.xml
代码如下: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <alpha android:duration="1000" android:fromAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="0.0" />
3、 若隐若现效果
blink.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="600" android:repeatMode="reverse" android:repeatCount="infinite"/>
4、 放大效果
zoom_in.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="3" android:toYScale="3" > </scale>
5、缩小效果
zoom_out.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.5" android:toYScale="0.5" > </scale>
6、 旋转效果
rotate.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="600" android:repeatMode="restart" android:repeatCount="infinite" android:interpolator="@android:anim/cycle_interpolator"/>
三、动画的xml已经定义好了,那怎么用呢?
1、首先获取动画对象
private Animation animFadein;
2、在onCreate();或者其他地方
// 加载动画 animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
3、设置动画监听器
如果你要监听动画的事件,如开始,结束等,你需要实现AnimationListener监听器,重写以下方法。(implement AnimationListener) 实现以下方法。
onAnimationEnd(Animation animation) - 当动画结束时调用
onAnimationRepeat(Animation animation) - 当动画重复时调用
onAniamtionStart(Animation animation) - 当动画启动时调用
下面只写一个例子:
@Override public void onAnimationEnd(Animation animation) { // 在动画结束后使用 // check for fade in animation if (animation == animFadein) { Toast.makeText(getApplicationContext(), "Animation Stopped", Toast.LENGTH_SHORT).show(); } }
最后一步: 让动画动起来啦。可以使用任何UI元素调用startAnimation方法。
以下是一个Textview元素调用的。
只要拿对象调用startAnimation(Animation animation);
参数为定义的动画对象
txtMessage.startAnimation(animFadein);
相关文章推荐
- Android开发之Intent.Action
- Android动画的使用点滴
- AndroidStudio配置SVN以及使用代码管理
- Android自定义View之绘制闪动的文字
- [分享]mac如何把本地Android项目提交到github
- Android_滚动状态ScrollView实现标题的悬浮和渐隐
- Android中Widget (微件、小组件)
- android 动画 ——alpha、scale、translate、rotate、set的xml属性及用法
- Android错误记录——layout文件无法加载
- Android同一个TevtView显示不同颜色的文字,添加不同的点击事件。
- Android开发实践 数据存储 学习、应用、总结
- android图像处理(一)
- Android中实现定时器的3中方法
- Android获取本机IP地址,绝对可行
- Android Socket(简单的字符串)
- Android中drawable各个属性讲解
- Android Studio 报错:Your project path contains non-ASCII characters.
- [问题]mac执行adb命令出现adb: command not found解决方法
- Android Studio报错:Gradle DSL method not found: 'android()'
- ShareSDK-Android的使用总结