伸缩动画,顾名思义,就是能变大变小的意思。android的伸缩动画,可以伸缩到图片的任意位置,当然这个还需要你自己去设置了。闲话就不多说了,开始代码部分吧。
我们的实现方式仍是两种。首先来看第一种:
通过自定义anim文件来设置,anim文件包含在src文件里面。
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="0.5"
android:toYScale="1.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:duration="2000"
android:startOffset="500"
android:fillAfter="true">
</scale>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%p"
android:pivotY="50%p"
android:duration="2000"
android:fillAfter="true">
</scale>
上面两个文件代表着图片的放大与缩小,另外还设置了放大缩小后的位置,以及执行所需时间,执行后是否回到原效果。
布局文件很简单,一个图片一个按钮,有图展示,就自己操作吧。下面直接是activity文件,里面包含了第二种方法,另外有简单注释,不懂的可以多了解一下。
public class ScaleAnimationDemoActivity extends Activity implements
OnClickListener, AnimationListener {
private ImageView mImageView;
private int s = 1;
private ScaleAnimation scaleAnim1;
private ScaleAnimation scaleAnim2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btn_scale).setOnClickListener(this);
mImageView = (ImageView) findViewById(R.id.imageview);
}
public void onClick(View v) {
//startScaleAnimationJavaCode();
startScaleAnimationXml();
setlistener();
}
private void startScaleAnimationXml() {
// android:fromXScale="1.0" 动画的开始大小
// android:toXScale="0.5" 动画的结束大小
// android:fromYScale="1.0"
// android:toYScale="0.5"
// android:pivotX="50%p" 动画放大或者缩小后的位置
// android:pivotY="50%p"
scaleAnim1 = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale_anim);
mImageView.startAnimation(scaleAnim1);
scaleAnim2 = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale_anims);
}
private void setlistener() {
scaleAnim1.setAnimationListener(this);
scaleAnim2.setAnimationListener(this);
}
private void startScaleAnimationJavaCode() {
//Animation.RELATIVE_TO_SELF 动画相对于自身移动
ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnim.setDuration(2000);
scaleAnim.setFillAfter(true);
mImageView.startAnimation(scaleAnim);
}
private void startScaleAnimationJavaCodes() {
ScaleAnimation scaleAnims = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnims.setDuration(2000);
// scaleAnims.setStartOffset(2000);
mImageView.startAnimation(scaleAnims);
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
if(s == 1){
mImageView.startAnimation(scaleAnim2);
s = 2;
}
else if(s == 2){
mImageView.startAnimation(scaleAnim1);
s = 1;
}
else{
Log.e("ScaleAnimationDemoActivity", "大姐来了");
}
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
以上就是代码的全部了,要了解动画的还是多熟悉一下它的属性,用的多了自然也就熟练了。有高手的话还请提出更好的建议,一定积极汲取。
完毕!