正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)


問題描述

正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)

所以我的主要目標是正確旋轉圖像(以及 CustomFrameLayout),我還需要存儲圖像的源寬度/高度。我怎樣才能做到這一點?
我試圖設置 scaleType.center / scaletype.centerInisde ,但是這是錯誤的想法。
有什麼建議嗎?謝謝!</p>


參考解法

方法 1:

Don't know if i exactly understand your problem. If u want programmatically rotate something u can try this:

ImageView image.animate().rotation(90).start();

or this

FrameLayout frame.animate().rotation(90).start();

or both. To store Width/Height u can use

int image_h = image.getHeight();
int image_w = image.getWidth();

方法 2:

Better late than never.

I was facing the exactly same problem. After some trying, I came with this solution:

private void rotateImageBy90(){
    imageView.animate()
            .rotation(90)
            .setInterpolator(new AccelerateDecelerateInterpolator())
            .setDuration(250)
            .setListener(rotateListener)
            .start();
    Log.d("chatImage", "Rotating image by "+90+" degrees clockwise");
}

Declare an AnimatorListener, the one that solves the problem:

Animator.AnimatorListener rotateListener = new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {

    }

    @Override
    public void onAnimationEnd(Animator animator) {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
        params.height = FrameLayout.LayoutParams.MATCH_PARENT;
        params.width = FrameLayout.LayoutParams.MATCH_PARENT;
        imageView.setLayoutParams(params);
        imageView.requestLayout();

        //This is the trick: I get the current bitmap, rotate it 
        Bitmap newBitmap = rotateImage(((BitmapDrawable)imageView.getDrawable()).getBitmap(),90);
        //Then, rotate the imageview back to it's original position, without animation
        imageView.setRotation(0);
        //As I set the new, rotate bitmap, to it.
        imageView.setImageBitmap(newBitmap);
    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }
};

The rotateImage method:

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
            true);
}

(by XTLJotunheimVitor Hugo Schwaab)

參考文件

  1. Rotate ImageView and FrameLayout correctly (CC BY‑SA 2.5/3.0/4.0)

#imageview #image-rotation #Android #android-framelayout #rotation






相關問題

Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)

Android中的手勢 (Gestures in Android)

GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non-Uniformly)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

IndoorAtlas SDK 2.0:使用 Picasso 和自定義 ImageView (IndoorAtlas SDK 2.0: Using Picasso with custom ImageView)

當圖像的寬度小於顯示器的寬度時,如何在視圖尋呼機內對齊圖像視圖的中心? (How to align center an image view inside a view pager when the image's width is less than the display's?)

正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)

將旋轉的文本放在圖像視圖上 (Put rotated text on an imageview)

如何在底部設置圖像視圖 (how to set imageview in bottom)

我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)







留言討論