java - 使用 Seekbar 将 imageView 旋转一定角度

标签 java android android-animation android-custom-view

我只是想将图像从最小旋转到最大。但为此我使用了多个图像来显示进度。有人可以建议我一种使用单个图像的方法吗?它可以以最小到最大的角度旋转。

我知道有两种可能的方法来实现它。

  1. 使用动画类
  2. 自定义 View

enter image description here

我只是想使用 SeekBar 以任意步数旋转此图像

我怎样才能实现这个目标?

旋转图像

private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    imgview.startAnimation(rotateAnim);
}

第二种方法

imageView.setRotation(angle); // but requires API >= 11

我可以使用 Matrix

Matrix matrix = new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

如何分别将 Start 和 end 角度设置为 SeekBar 的最小值和最大值。哪种方法更好以及是否必须将其放入 FrameLayout 中以使其自由旋转

最佳答案

你可以这样做:

// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
       R.drawable.android);

int width = bitmapOrg.width();
int height = bitmapOrg.height();

// createa matrix for the manipulation
Matrix matrix = new Matrix();

// rotate the Bitmap
matrix.postRotate(45);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);

有关详细信息,请查看此。

http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

matrix.postRotate(45);  //here 45 is the degree of angle to rotate

关于java - 使用 Seekbar 将 imageView 旋转一定角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29093776/

相关文章:

java - 如何获取 Google 云端硬盘上已更改文件的 ID

java - 较旧的 java 项目的 GUI 工具问题

android - 在应用程序与 Activity 中重写 onTrimMemory

java - 如何使用 ValueAnimator#ofFloat

Android 创建动画 onClick 的自定义 ImageView

java - 为什么我们在 ArrayList<Integer> a1 = new ArrayList<Integer>() 中使用包装类 Integer?

java - 以编程方式使用 java 重命名 JKS 别名

java - 在 Android 上使用 Java 三元运算符的奇怪行为

java - 允许模拟位置#Android

java - 动画 View 移出我的布局