Android : 이미지 뷰에서 이미지를 각도로 회전 각도로 회전시킵니다. 더 간단하고 덜 복잡한

다음 코드를 사용하여 ImageView의 이미지를 각도로 회전시킵니다. 더 간단하고 덜 복잡한 방법이 있습니까?

ImageView iv = (ImageView)findViewById(imageviewid);
TextView tv = (TextView)findViewById(txtViewsid);
Matrix mat = new Matrix();
Bitmap bMap = BitmapFactory.decodeResource(getResources(),imageid);
mat.postRotate(Integer.parseInt(degree));===>angle to be rotated
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWidth(),bMap.getHeight(), mat, true);
iv.setImageBitmap(bMapRotate);


답변

ImageView: 회전하는 또 다른 간단한 방법 :
UPDATE :
필수 가져 오기 :

import android.graphics.Matrix;
import android.widget.ImageView;

코드 : (가정 imageView, angle, pivotXpivotY이미 정의되어 있습니다)

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);

이 방법은 매번 새 비트 맵을 만들 필요가 없습니다.

참고 :로는 회전 ImageViewontouch 설정할 수있는 런타임에 onTouchListener을ImageView&가 (즉, 마지막 두 줄을 추가하여 회전 postRotate 매트릭스가에 설정 이미지 뷰 터치 리스너에서) 위의 코드 섹션을 ACTION_MOVE의 일부입니다.


답변

mImageView.setRotation(angle) API> = 11


답변

API 11 이상을 지원하는 경우 다음 XML 속성을 사용할 수 있습니다.

android:rotation="90"

Android Studio xml 미리보기에서는 올바르게 표시되지 않을 수 있지만 예상대로 작동합니다.


답변

이를 수행하는 두 가지 방법이 있습니다.

1 Matrix새 비트 맵을 만드는 데 사용 :

imageView = (ImageView) findViewById(R.id.imageView);
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.image);

Matrix matrix = new Matrix();
matrix.postRotate(30);

Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
        matrix, true);

imageView.setImageBitmap(rotated);

2 사용 RotateAnimation상의 View회전 할 및 확인 애니메이션 세트를 만들기 위해 fillAfter=true, duration=0그리고fromDegrees=toDgrees

 <?xml version="1.0" encoding="utf-8"?>
<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="45"
  android:toDegrees="45"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="0"
  android:startOffset="0"
/>

코드에서 애니메이션을 부풀립니다.

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
myView.startAnimation(rotation);

답변

나는 이것이 미치게 늦다는 것을 알고 있지만 나에게 도움이되어 다른 사람들을 도울 수 있습니다.

API 11부터는 imageView.setRotation(angleInDegrees);메소드 를 사용하여 프로그래밍 방식으로 ImageView의 절대 회전을 설정할 수 있습니다 .

절대적으로 현재 회전을 추적하지 않고도이 함수를 반복해서 호출 할 수 있습니다. 15F즉, setRotation()메서드 를 전달 하여 회전 한 다음 setRotation()다시 호출 30F하면 이미지의 회전은 45 도가 아닌 30 도입니다.

참고 : 이것은 실제로 ImageView뿐만 아니라 View 객체 의 모든 하위 클래스에서 작동합니다 .


답변

이것이 RotatableImageView의 구현입니다 . 사용법은 매우 간단합니다. attrs.xmlRotatableImageView.java 를 프로젝트에 복사 하고 RotatableImageView 를 레이아웃에 추가하십시오. example : angle 매개 변수를 사용하여 원하는 회전 각도를 설정하십시오 .

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:example="http://schemas.android.com/apk/res/com.example"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.example.views.RotatableImageView
        android:id="@+id/layout_example_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_layout_arrow"
        example:angle="180" />
</FrameLayout>

이미지를 표시하는 데 문제가 있으면 RotatableImageView.onDraw () 메서드 에서 코드를 변경 하거나 draw () 메서드를 대신 사용하십시오.


답변

이 방법으로도 수행 할 수 있습니다 :-

imageView.animate().rotation(180).start();

여기서 왔어요