Android:ImageView旋转动画 - 保持比例类型适合中心

问题描述 投票:0回答:2

我有一个有android:scaleType="fitCenter"的ImageView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="at.lukle.picturerotation.MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/android"/>

    <Button
        android:id="@+id/btn_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="rotate"/>

</RelativeLayout>

它看起来像这样:

Normal

单击Button时,我应用了一个旋转动画:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnRotate = (Button) findViewById(R.id.btn_rotate);
        final ImageView iv = (ImageView) findViewById(R.id.iv);

        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv.animate().rotationBy(90f).start();
            }
        });
    }
}

现在它看起来像这样:Rotated

图像在侧面被切割。我希望scaleType也应用于旋转的图像,这样ImageView不仅可以旋转,还可以缩放以适应宽度。我想我也需要缩放动画,但我不知道该怎么做。

我也尝试过使用iv.setRotation(90),但我在这里遇到同样的问题......

android animation rotation
2个回答
0
投票

用过这个

iv.setRotation(iv.getRotation() + 90);

代替

 iv.animate().rotationBy(90f).start();

另外两个更新像android:configChanges那样

 <activity
        android:name=".youactivity"
        android:label="@string/app_name"

        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        >

android:adjustViewBounds true to imageview

  <ImageView
    android:id="@+id/iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
    android:src="@mipmap/android"/>

0
投票

我的解决方案不优雅(我是开发中的菜鸟)我希望有人能提供更好的解决方案....

    int h,w;
    Boolean safe=true;

在初始化活动时无法获取imageView的参数要执行此操作,请参阅此solution或在按钮的onClick上设置尺寸像这样

    rotateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(imageView.getRotation()/90%2==0){
                h=imageView.getHeight();
                w=imageView.getWidth();

            }
        .
        .//Insert the code Snippet below here 
       }

当我们想要旋转ImageView时要运行的代码

if(safe)     
imageView.animate().rotationBy(90).scaleX(imageView.getRotation()/90%2==0?(w*1.0f/h):1).scaleY(imageView.getRotation()/90%2==0?(w*1.0f/h):1).setDuration(2000).setInterpolator(new LinearInterpolator()).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                      safe=false;
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                      safe=true;

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            }).start();
        }
    });

这个解决方案足以解决上面的问题。虽然它会缩小imageView,即使它没有必要(当高度小于宽度时)。如果它困扰你,你可以在scaleX / scaleY中添加另一个三元运算符。

© www.soinside.com 2019 - 2024. All rights reserved.