Android的动画缩放图像,以适应屏幕宽度和高度 [英] Android Animate Scale Image to fit screen width and height

查看:121
本文介绍了Android的动画缩放图像,以适应屏幕宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有放置在屏幕的不同位置的四个图像。现在我想的动画缩放它们在点击以适应屏幕宽度和高度。当点击我想淡出屏幕的其他内容。目前我使用以下code扩展他们

I have four images placed at different locations of the screen. Now I would like to animate scale them upon click to fit screen width and height. Upon click I would like to fadeout other contents of the screen. Currently i am using following code to scale them

Animation logoMoveAnimation = AnimationUtils.loadAnimation(this, R.anim.zoom_image);
view.startAnimation(logoMoveAnimation);

在这里为zoom_image.xml为

where as zoom_image.xml is

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
   <scale android:fromXScale="0.0" android:fromYScale="0.0"
          android:toXScale="1.0" android:toYScale="1.0" 
          android:duration="700" android:fillBefore="false" />
   <translate android:fromXDelta="0.0" android:fromYDelta="1.0"
          android:duration="700" />
</set>

以上code不工作,因为我知道,XML是不正确的,我需要做一些额外的编码。

Above code is not working because I know that xml is incorrect and I need to do something extra in coding.

推荐答案

最后我用下面的code

I ended up using following code

final int initialWidth = v.getWidth();
        final int initialHeight = v.getHeight();
        Log.v("WIDTH_", Integer.toString(initialWidth));
        Log.v("HEIGHT_", Integer.toString(initialHeight));
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        final int finalHeight = displaymetrics.heightPixels;
        final int finalWidth = displaymetrics.widthPixels;
        Log.v("WIDTH_F", Integer.toString(finalWidth));
        Log.v("HEIGHT_F", Integer.toString(finalHeight));
        final int animationDuration = 1500;
        final float stepX = (float)((float)(finalWidth - initialWidth))/animationDuration;
        final float stepY = (float)((float)(finalHeight - initialHeight))/animationDuration;
        Log.v("stepX", Float.toString(stepX));
        Log.v("stepY", Float.toString(stepY));
        final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
             RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    Animation a = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    int newWidth, newHeight;
                    Log.v("INTERPOLATION_T", Float.toString(interpolatedTime));
                    newWidth = (int)Math.ceil((initialWidth + stepX*interpolatedTime*animationDuration));   
                    newHeight = (int)Math.ceil((initialHeight + stepY*interpolatedTime*animationDuration));                             
                    if(v.getTag().toString().equals("1")) {
                        //layoutParams.setMargins(0, (int)-interpolatedTime*initialHeight, 0, 0);
                    }
                    else if(v.getTag().toString().equals("2")) {
                        layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.separator1);
                        //layoutParams.setMargins((int)-interpolatedTime*initialWidth, (int)-interpolatedTime*initialHeight, 0, 0);
                    }
                    else if(v.getTag().toString().equals("3")) {
                        layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.separator2);
                        //layoutParams.setMargins((int)-interpolatedTime*initialWidth, (int)-interpolatedTime*initialHeight, 0, 0);
                    }
                    else if(v.getTag().toString().equals("4")) {
                        layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.separator3);
                        //layoutParams.setMargins((int)-interpolatedTime*initialWidth, (int)-interpolatedTime*initialHeight, 0, 0);
                    }
                    v.setLayoutParams(layoutParams);
                    v.getLayoutParams().height = newHeight; 
                    v.getLayoutParams().width = newWidth;   
                    v.requestLayout();
                }
                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };

            a.setInterpolator(new LinearInterpolator());
            a.setDuration(animationDuration);
            a.setFillAfter(true);
            v.startAnimation(a);

这篇关于Android的动画缩放图像,以适应屏幕宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆