rij*_*nrv 147 android bitmap rotation imageview
我使用以下代码在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);
Run Code Online (Sandbox Code Playgroud)
Aks*_*Aks 188
旋转的另一种简单方法ImageView
:
UPDATE:
必需的导入:
import android.graphics.Matrix;
import android.widget.ImageView;
Run Code Online (Sandbox Code Playgroud)
代码:(假设imageView
,angle
,pivotX
及pivotY
已经定义)
Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);
Run Code Online (Sandbox Code Playgroud)
此方法不需要每次都创建新的位图.
注意:要旋转
ImageView
上ontouch在运行时可以设置onTouchListener上ImageView
并旋转它通过添加最后两行(即postRotate矩阵设置上的ImageView)在上面的代码段在您触摸听众ACTION_MOVE一部分.
Fro*_*lik 170
mImageView.setRotation(angle)
API> = 11
Ole*_*siy 64
如果您支持API 11或更高版本,则可以使用以下XML属性:
android:rotation="90"
Run Code Online (Sandbox Code Playgroud)
它可能无法在Android Studio xml预览中正确显示,但它按预期工作.
Rot*_*miz 42
有两种方法可以做到这一点:
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);
Run Code Online (Sandbox Code Playgroud)
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"
/>
Run Code Online (Sandbox Code Playgroud)
并在代码中膨胀动画:
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
myView.startAnimation(rotation);
Run Code Online (Sandbox Code Playgroud)
我知道这是非常晚,但它对我有帮助,所以它可以帮助别人.
从API 11开始,您可以使用该imageView.setRotation(angleInDegrees);
方法以编程方式设置ImageView的绝对旋转.
绝对地,我的意思是你可以反复调用这个函数,而不必跟踪当前的旋转.意思是,如果我通过传递15F
给setRotation()
方法旋转,然后setRotation()
再次调用30F
,则图像的旋转为30度,而不是45度.
注意:这实际上适用于View对象的任何子类,而不仅仅是ImageView.
对于科特林,
mImageView.rotation = 90f //angle in float
Run Code Online (Sandbox Code Playgroud)
这将旋转 imageView 而不是旋转图像
另外,虽然它是View
课堂上的一个方法。所以你几乎可以使用它旋转任何视图。
小智 7
在android中延迟旋转图像:
imgSplash.animate().rotationBy(360f).setDuration(3000).setInterpolator(new LinearInterpolator()).start();
Run Code Online (Sandbox Code Playgroud)
这是我对RotatableImageView的实现.用法很简单:只要复制attrs.xml和RotatableImageView.java到您的项目,并添加RotatableImageView到您的布局.使用示例:角度参数设置所需的旋转角度.
<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>
Run Code Online (Sandbox Code Playgroud)
如果您在显示图像时遇到一些问题,请尝试在RotatableImageView.onDraw()方法中更改代码或使用draw()方法.
归档时间: |
|
查看次数: |
280752 次 |
最近记录: |