您的位置:首页 > 移动开发 > Android开发

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: