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

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