android 动画保持,Android:ImageView旋转动画 - 保持比例类型适合中心(示例代码)

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

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">

android:id="@+id/iv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="fitCenter"

android:src="@drawable/android"/>

android:id="@+id/btn_rotate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="rotate"/>

它看起来像这样:

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS81Rnp1Wi5wbmcifQ==

单击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();

}

});

}

}

现在它看起来像这样:eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS94aWZzdy5wbmcifQ==

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

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

答案

用过这个

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

代替

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

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

android:name=".youactivity"

android:label="@string/app_name"

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

>

android:adjustViewBounds true to 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"/>

另一答案

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

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中添加另一个三元运算符。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值