【问题标题】:Rotate an imageView at an angle using Seekbar使用 Seekbar 以一定角度旋转 imageView
【发布时间】:2015-03-17 07:49:05
【问题描述】:

我只想旋转an image from min to max。但为此,我使用了多张图片来展示进度。有人可以建议我一种使用单个图像的方法。可以从最小到最大的角度旋转。

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

  1. 使用动画类
  2. 自定义视图

我只是想使用 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 = new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

我如何将开始和结束角度分别设置为 SeekBar 最小值和最大值。哪种方法更好,我是否必须把它放在FrameLayout 让它rotate freely

【问题讨论】:

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


    【解决方案1】:

    你可以这样做:

    // 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
    

    【讨论】:

      【解决方案2】:

      您可以按照here的建议使用矩阵

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

      【讨论】:

        【解决方案3】:

        这将是一个无限旋转,但您可以随意旋转它。 这个视图只是无限旋转 360 度。

                final RotateAnimation R = new RotateAnimation(0, 360, view.getWidth() / 2.0f, view.getHeight() / 2.0f);
                    R.setRepeatCount(Animation.INFINITE);
                    R.setDuration(800);
                    view.startAnimation(R);
        

        【讨论】:

          猜你喜欢
          • 2012-02-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-16
          • 2020-04-24
          • 1970-01-01
          相关资源
          最近更新 更多