Android之ImageView控件旋转方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36347817/article/details/88669821

最近在一个安卓板子上实现一个人脸识别,关键还是横屏,拍照之后的获取的图片,显示的“头歪了”。来看看如何使ImageView控件显示正常的操作。

1.旋转bitmap:

Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
Matrix matrix  = new Matrix();
matrix.setRotate(90);
Bitmap new = Bitmap.create(bitmap,0,bitmap.getWidth(),0,bitmap.getHeight(),matrix);
image.setBitmapResource(bitmap);

如果程序不断获取新的bitmap重新设置给ImageView的话,那么bitmap在不断旋转,不回收内存,效率很低。

2.使用ImageView自带的旋转方法

可以通过在xml中设置ImageView的属性来实现(很简单)

android:rotation="90"

动态调用如下:

image.setPivotX(image.getWidth()/2);
image.setPivotY(image.getHeight()/2);//支点在图片中心
image.setRotation(90);

3.使用旋转动画

可以使用ImageView配合属性动画实现

rotateImage.animate().rotation(90);

或者普通动画:

Animation rotateAnimation  = new RotateAnimation(lastAngle, progress, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setDuration(50);
                rotateAnimation.setRepeatCount(0);
                rotateAnimation.setInterpolator(new LinearInterpolator());
                rotateImage.startAnimation(rotateAnimation);

猜你喜欢

转载自blog.csdn.net/qq_36347817/article/details/88669821