所谓的Level动画就是给图片设置不同的等级,在不同的等级下显示不同的图片,比如说手机电池的电量。
实现步骤:
一、
①在res文件夹下创建新的文件夹drawable,在其中新建level.xml文件:这里我们把标签改为level-list,因为新建中没有该项。
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
在level中显示不同的图片,当等级不同的时候,切换或者改变图片
一般用于管理电池电量的变化
定义对应的等级:android:maxLevel=100 android:minLevel=90
如果在给定的等级范围内则显示对应的图片android:drawable="@drawable/lamp_on"
-->
<item
android:drawable="@drawable/lamp_on"
android:maxLevel="50"
android:minLevel="25">
</item>
<item
android:drawable="@drawable/lamp_off"
android:maxLevel="25"
android:minLevel="1">
</item>
</level-list>
②、然后是布局文件:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LevelActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/leve" />
<!-- 开关 -->
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<!-- 点击改变状态 -->
<ToggleButton
android:id="@+id/tog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/switch1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textOff="关灯"
android:textOn="开灯" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="on"
android:text="开灯" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="off"
android:text="关灯" />
</RelativeLayout>
③、然后是在代码中的具体实现:
package com.example.text01;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.ToggleButton;
public class LevelActivity extends Activity {
private ImageView iv;
private Switch switch1;
private ToggleButton tog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
iv = (ImageView) findViewById(R.id.iv);
switch1 = (Switch) findViewById(R.id.switch1);
tog = (ToggleButton) findViewById(R.id.tog);
// 给ToggleButton添加点击事件
tog.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (!isChecked) {
// 设置ImageView的对应等级
iv.setImageLevel(30);
} else {
iv.setImageLevel(10);
}
}
});
// 给switch1添加点击事件
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// 进行判断,被选中是关灯
if (isChecked) {
iv.setImageLevel(30);
} else {
iv.setImageLevel(10);
}
}
});
}
// 开灯,设置ImageView等级
public void on(View view) {
iv.setImageLevel(30);
}
// 关灯
public void off(View view) {
iv.setImageLevel(10);
}
}
Level运行结果:
二、上面是Level动画。下面介绍一下Transition动画,就是设置图片的渐变过程。
①同样在drawable文件夹下创建transition.xml文件:这里把标签改了,只能包含两张图片
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 图片之间的平滑过度 在transition里面只能包含两张图片-->
<item
android:drawable="@drawable/lamp_off">
</item>
<item
android:drawable="@drawable/lamp_on">
</item>
</transition>
②下面是在代码中的具体实现:
package com.example.text01;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.TransitionDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class TransitionActivity extends Activity {
private ImageView iv;
private TransitionDrawable drawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
iv = (ImageView) findViewById(R.id.iv_transition);
drawable = (TransitionDrawable) iv.getDrawable();
}
public void on(View view) {
// 状态过度时间
drawable.startTransition(3000);
}
public void off(View view) {
// 反转过渡:
// 1、正在过渡中:从first--》second过渡中,从当前时间点向回反转过渡
// 2、已经过渡完毕:从second过渡到first
drawable.reverseTransition(3000);
}
}
运行结果:
三、接下来是drawable下的shape旋转,它用来实现的是设置按钮的形状,渐变颜色等属性。
首先在drawable文件夹下新建类型为shape的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
gradient:颜色变化渐变色
android:angle=""必须是45的倍数
-->
<gradient
android:angle="90"
android:endColor="#0f0"
android:startColor="#f00" />
<!-- 制作圆角效果
通过android:radius=""设置圆角,属性值越大圆角越圆,普通按钮最大50dp
给定的值就是半径值
-->
<corners
android:radius="40dp"/>
<!-- 用来实现描边
android:width=""用于定义线的粗细
-->
<stroke android:width="2dp"
android:color="#f00"/>
<!-- 单纯的颜色 -->
<solid android:color="#f00"/>
</shape>
然后在布局文件中引用我们定义的shape.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:padding="10dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="按钮一"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape2"
android:layout_marginTop="10dp"
android:text="按钮二"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:layout_marginTop="10dp"
android:text="按钮三"/>
</LinearLayout>
第三个按钮的selector标签:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false">
<shape >
<gradient android:startColor="#fff"
android:endColor="#fff"/>
<corners android:radius="20dp"/>
<stroke android:width="1dp"
android:color="#f00"/>
</shape>
</item>
<item android:state_pressed="true">
<shape >
<gradient android:startColor="#0f0"
android:endColor="#0f0"/>
<corners android:radius="20dp"/>
<stroke android:width="1dp"
android:color="#f00"/>
</shape>
</item>
</selector>
运行结果:
四、最后实现的是include,就是在一个布局中的子类布局LinearLayout或RelativeLayout引用另外一个布局文件。
首先我们定义一个activity_item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bd"/>
</LinearLayout>
然后是在另一个布局文件中引用该布局:
<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"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="inport"
android:text="点击导入布局" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 通过include标签将另外一个布局导入到当前的布局中 -->
<include layout="@layout/activity_item" />
</LinearLayout>
</LinearLayout>
当然我们也可以点击按钮通过切换item中的图片就可以改变引用的图片:
package com.example.text01;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class IncludeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_include);
}
//通过点击按钮改变导入布局中的View组件
public void inport(View view){
//能否获取一个ImageView对象
ImageView iv = (ImageView) findViewById(R.id.iv_item);
iv.setImageResource(R.drawable.lamp_on);
}
}
运行结果:
五、这里还有最后一个标签inset用来设置背景图片的压缩方向和压缩比例,我们可以在布局文件中的父类标签引用该标签。
同样,在drawable文件夹下新建标签为inset的xml文件: