Android学习之动画效果的实现、自定义控件皮肤
2016-08-10 13:22
417 查看
自定义控件皮肤(按钮正常、按下)
xml资源文件
Step1:skin.xml(Drawable Resource file)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--normal 状态下的按钮!-->
<item android:state_pressed="false" android:drawable="@drawable/tab_my_normal"></item>
<!--press 状态下的按钮!-->
<item android:state_pressed="true" android:drawable="@drawable/tab_my_pressed"></item>
</selector>
Step2:activity_main.xml
<Button
android:background="@drawable/skin"//使用skin.xml资源文件
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
透明的动画效果
第一种,直接使用代码进行配置
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlphaAnimation aa = new AlphaAnimation(0,1);//透明度0 -- 1
aa.setDuration(1000);//设置动画时间
view.startAnimation(aa);//启动动画
}
});
第二种:使用xml件来配置动画效果
action.xml(Animation resource file)
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
</alpha>
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.action));
}
});
}
旋转动画效果
第一种:直接在按钮的响应事件中添加动画
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
otateAnimation ra;
ra = new RotateAnimation(0,360);
ra.setDuration(1000);
view.startAnimation(ra);
}
});
}
第二种:使用xml文件进行配置动画效果
rotate.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
>
</rotate>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
}
});
}
移动效果
第一种:不使用资源文件
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TranslateAnimation tf = new TranslateAnimation(0,300,0,300);
tf.setDuration(1000);
view.startAnimation(tf);
}
});
}
第二种:使用xml 资源文件进行移动效果的资源配置
translate.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="300"
android:toYDelta="300"
android:duration="1000">
</translate>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate));
}
});
缩放效果
第一种:不使用xml资源文件
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ScaleAnimation sa = new ScaleAnimation(0,1,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
view.startAnimation(sa);
//view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate));
}
});
第二种:使用xml资源文件配置动画效果
scale.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
</scale>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale));
}
});
组合的动画效果
第一种:不使用xml资源文件
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
animationSet.setDuration(1000);
//缩放效果
ScaleAnimation sa = new ScaleAnimation(0,1,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
animationSet.addAnimation(sa);
//透明效果
AlphaAnimation aa = new AlphaAnimation(0,1);
aa.setDuration(1000);
animationSet.addAnimation(aa);
//执行组合动画效果
view.startAnimation(animationSet);
}
});
第二种:使用xml资源文件配置动画效果
animationSet.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:duration="1000">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
</alpha>
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
</scale>
</set>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//执行xml动画效果配置的资源文件
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.animationset));
}
});
动画效果帧听
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animationset);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override//动画开始
public void onAnimationStart(Animation animation) {
}
@Override//动画结束
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, "动画执行结束了", Toast.LENGTH_SHORT).show();
}
@Override//动画重复
public void onAnimationRepeat(Animation animation) {
}
});
view.startAnimation(animation);
}
});
自定义动画
Step1:创建一个类CustomAnimation让他继承自Animation
public class CustomAnimation extends Animation {
@Override//这个方法可以获得目标对象的宽高
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
//透明效果
// t.setAlpha(interpolatedTime);
//平移效果
// t.getMatrix().setTranslate(300*interpolatedTime,300*interpolatedTime);
//摇头效果
t.getMatrix().setTranslate((float)(Math.sin(interpolatedTime*20)*50),0);
}
}
Step2:在MainActivity中使用自定义的动画
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomAnimation animation = new CustomAnimation();
animation.setDuration(1000);
view.startAnimation(animation);
}
});
xml资源文件
Step1:skin.xml(Drawable Resource file)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--normal 状态下的按钮!-->
<item android:state_pressed="false" android:drawable="@drawable/tab_my_normal"></item>
<!--press 状态下的按钮!-->
<item android:state_pressed="true" android:drawable="@drawable/tab_my_pressed"></item>
</selector>
Step2:activity_main.xml
<Button
android:background="@drawable/skin"//使用skin.xml资源文件
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
透明的动画效果
第一种,直接使用代码进行配置
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlphaAnimation aa = new AlphaAnimation(0,1);//透明度0 -- 1
aa.setDuration(1000);//设置动画时间
view.startAnimation(aa);//启动动画
}
});
第二种:使用xml件来配置动画效果
action.xml(Animation resource file)
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
</alpha>
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.action));
}
});
}
旋转动画效果
第一种:直接在按钮的响应事件中添加动画
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
otateAnimation ra;
ra = new RotateAnimation(0,360);
ra.setDuration(1000);
view.startAnimation(ra);
}
});
}
第二种:使用xml文件进行配置动画效果
rotate.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
>
</rotate>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
}
});
}
移动效果
第一种:不使用资源文件
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TranslateAnimation tf = new TranslateAnimation(0,300,0,300);
tf.setDuration(1000);
view.startAnimation(tf);
}
});
}
第二种:使用xml 资源文件进行移动效果的资源配置
translate.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="300"
android:toYDelta="300"
android:duration="1000">
</translate>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate));
}
});
缩放效果
第一种:不使用xml资源文件
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ScaleAnimation sa = new ScaleAnimation(0,1,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
view.startAnimation(sa);
//view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate));
}
});
第二种:使用xml资源文件配置动画效果
scale.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
</scale>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale));
}
});
组合的动画效果
第一种:不使用xml资源文件
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
animationSet.setDuration(1000);
//缩放效果
ScaleAnimation sa = new ScaleAnimation(0,1,0,1, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
animationSet.addAnimation(sa);
//透明效果
AlphaAnimation aa = new AlphaAnimation(0,1);
aa.setDuration(1000);
animationSet.addAnimation(aa);
//执行组合动画效果
view.startAnimation(animationSet);
}
});
第二种:使用xml资源文件配置动画效果
animationSet.xml[b](Animation resource file)[/b]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:duration="1000">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
</alpha>
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
</scale>
</set>
MainActivity
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//执行xml动画效果配置的资源文件
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.animationset));
}
});
动画效果帧听
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animationset);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override//动画开始
public void onAnimationStart(Animation animation) {
}
@Override//动画结束
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, "动画执行结束了", Toast.LENGTH_SHORT).show();
}
@Override//动画重复
public void onAnimationRepeat(Animation animation) {
}
});
view.startAnimation(animation);
}
});
自定义动画
Step1:创建一个类CustomAnimation让他继承自Animation
public class CustomAnimation extends Animation {
@Override//这个方法可以获得目标对象的宽高
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
//透明效果
// t.setAlpha(interpolatedTime);
//平移效果
// t.getMatrix().setTranslate(300*interpolatedTime,300*interpolatedTime);
//摇头效果
t.getMatrix().setTranslate((float)(Math.sin(interpolatedTime*20)*50),0);
}
}
Step2:在MainActivity中使用自定义的动画
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomAnimation animation = new CustomAnimation();
animation.setDuration(1000);
view.startAnimation(animation);
}
});
相关文章推荐
- Android自定义控件View(实现控件的动画效果,自定义类画简略时钟,TextView中文字逐一显示,动态设置progress的圆环/扇形ProgressBar等)
- 实现android动画效果学习二
- Android学习之Android 5.0分享动画实现微信点击全屏效果
- Android学习-使用ViewFlipper实现屏幕切换的动画效果
- 2014-11-8Android学习------Android抽屉效果的实现案例--------动画Animation学习篇
- Android动画学习(五)之属性动画实现Tween的效果和高级属性示例
- Android学习之Android 5.0分享动画实现微信点击全屏效果
- 2014-11-3Android学习------利用ViewFlipper实现滑动翻页的效果--------GIF动画实现
- 2014-11-6Android学习------苹果切水果游戏手指滑动效果实现--------动画Animation学习篇
- Android插值器、动画、分解详解,实现View摆动效果
- android Tween Animations(动画效果-代码实现)的使用
- Android动画效果Animation之Tween实现简单动画
- Android中的动画效果学习之---Tween动画(硬编码方法创建)
- Android中使用Animation实现控件的动画效果以及Interpolator和AnimationListener的使用
- Android游戏开发学习笔记(一):tweened animation自定义动画的实现
- Android动画Animation之Tween用代码实现动画效果
- Android中的动画效果学习之二---Tween动画(使用XML文件进行配置动画效果)
- android Tween Animations(动画效果-XML文件实现)的使用
- android动画效果之Animations(代码实现动画)
- Android进阶:实现android系统自带查看照片动画效果 类似Gallery手势滑动