问题
I have an ImageView that has android:scaleType="fitCenter"
<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>
It looks like this:
When the Button gets clicked, I apply a rotation animation:
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();
}
});
}
}
Now it looks like this:
The image gets cut on the side. I want that the scaleType is also applied on the rotated image, so that the ImageView not just gets rotated, but also scaled to fit the width. I guess I need a scaling animation too, but I have no idea how to do that.
I also tried to just use iv.setRotation(90)
, but I have the same problem here...
回答1:
Used this
iv.setRotation(iv.getRotation() + 90);
Instead of
iv.animate().rotationBy(90f).start();
two more update take android:configChanges
like that
<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"/>
回答2:
My Solution is not elegant (i am a noob in developing) I hope someone can provide a better solution ....
int h,w;
Boolean safe=true;
Getting the parameters of imageView is not possible at initialisation of activity To do so please refer to this solution OR set the dimensions at onClick of a Button Like this
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
}
And the code to be run when we want to rotate 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();
}
});
This solution is sufficient for the Problem above.Although it will shrink the imageView even if it is not necessary(when height is smaller than Width).If it bothers you,you can add another ternary operator inside scaleX/scaleY.
来源:https://stackoverflow.com/questions/39466553/android-imageview-rotation-animation-keep-scale-type-fit-center