Android 从布局 xml 文件中旋转 ImageView 源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11243314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotate ImageView source from layout xml file
提问by mehrmoudi
I have this ImageView in my layout:
我的布局中有这个 ImageView:
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/image_divider"
android:paddingBottom="8dp"
android:paddingTop="4dp"
android:scaleType="fitXY"
android:src="@android:drawable/divider_horizontal_textfield" />
It's a horizontal divider. I want to rotate it 90 degrees so I have a vertical divider.
Is there any possible way to do it right here from the layout and not the Activity class?
这是一个水平分隔线。我想将它旋转 90 度,所以我有一个垂直分隔线。
有没有什么可能的方法可以从布局而不是 Activity 类中做到这一点?
回答by Rinkal Bhanderi
You can use Available Since API Level 11
您可以使用自API 级别 11起可用
android:rotation="90"
Final Code to Put,
要放置的最终代码,
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:rotation="90"
android:contentDescription="@string/image_divider"
android:paddingBottom="8dp"
android:paddingTop="4dp"
android:scaleType="fitXY"
android:src="@android:drawable/divider_horizontal_textfield" />
回答by Stephane Mathis
You can do that in your code by creating a new bitmap object. Check this out : http://android-er.blogspot.fr/2010/07/rotate-bitmap-image-using-matrix.htmlAnd specifically this function
您可以通过创建一个新的位图对象在您的代码中做到这一点。看看这个:http: //android-er.blogspot.fr/2010/07/rotate-bitmap-image-using-matrix.html特别是这个功能
Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
matrix.postRotate(curRotate);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
myImageView.setImageBitmap(resizedBitmap);
回答by Levente Tiszberger
Add "id" at ImageView (if not generate auto):
在 ImageView 添加“id”(如果没有自动生成):
android:id="@+id/imageView"
and use the "id" (kotlin example):
并使用“id”(kotlin 示例):
val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setRotation(90f) // rotate 90 degree