Android 正确旋转ImageView和FrameLayout

Android 正确旋转ImageView和FrameLayout,android,rotation,imageview,android-framelayout,image-rotation,Android,Rotation,Imageview,Android Framelayout,Image Rotation,我在一个框架布局中有一个图像视图(两者都有布局页边距),我还得到了我的自定义框架布局(与布局页边距相同)。 那么我现在拥有的是: 源代码(.axml): 我想要的是什么(通过按钮以编程方式向左、向右旋转): 因此,我的主要目标是正确旋转图像(以及自定义框架布局),我还需要存储图像的源宽度/高度。如何实现这一点? 我试图设置scaleType.center/scaleType.centerInisde,但这是错误的想法。 有什么建议吗?谢谢 不知道我是否完全理解你的问题。 如果您希望以编程

我在一个框架布局中有一个图像视图(两者都有布局页边距),我还得到了我的自定义框架布局(与布局页边距相同)。
那么我现在拥有的是:

源代码(.axml):


我想要的是什么(通过按钮以编程方式向左、向右旋转):

因此,我的主要目标是正确旋转图像(以及自定义框架布局),我还需要存储图像的源宽度/高度。如何实现这一点?
我试图设置scaleType.center/scaleType.centerInisde,但这是错误的想法。

有什么建议吗?谢谢

不知道我是否完全理解你的问题。 如果您希望以编程方式旋转某些对象,可以尝试以下操作:

ImageView image.animate().rotation(90).start();
还是这个

FrameLayout frame.animate().rotation(90).start();
或者两者兼而有之。 存储您可以使用的宽度/高度

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

迟做总比不做好

我面临着完全相同的问题。 经过一番尝试,我得出了以下解决方案:

private void rotateImageBy90(){
    imageView.animate()
            .rotation(90)
            .setInterpolator(new AccelerateDecelerateInterpolator())
            .setDuration(250)
            .setListener(rotateListener)
            .start();
    Log.d("chatImage", "Rotating image by "+90+" degrees clockwise");
}
声明解决问题的AnimatorListener:

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) {

    }
};
旋转图像法:

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);
}

我的问题是:如何像我展示的那样旋转图像(参见图片)。将imageView和MyCustomFrameLayout放在这些边框中(请参见图2)。添加以下内容:。。。这是:yourbutton.setOnClickListener(newview.OnClickListener(){@Override public void onClick(View v){Img.animate().rotation(90.start();CustomView.animate().rotation(90.start();});//这可能适用于图像。由于内部没有图像,我不知道如何处理CustomFrameLayout。我只使用一个FrameLayout,但您肯定有充分的理由使用它们。感谢您的回复,但我需要知道我需要使用哪些参数(centerinsde/center等)。这不是简单的旋转。
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);
}