简体   繁体   English

如何在Android中使用Seekbar旋转图像360度?

[英]How to Rotate the Image 360 Degree by Using Seekbar in android?

I am creating an Android App to rotate an image by 360 degrees. 我正在创建一个Android应用程序以将图像旋转360度。 When I track the Seek bar the image should Rotate. 当我跟踪搜索栏时,图像应旋转。

Could you please help me how to achieve this? 你能帮我实现这个目标吗?

Imageview selectedimage;
final int width = selectedimage.width;
final int height = selectedimage.height;      

final Bitmap scaledBitmap = Bitmap.createScaledBitmap(selectedimage,width, height, true); 
selectedimage.setImageBitmap(getRotatedBitmap(scaledBitmap));

seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
  @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
          // TODO Auto-generated method stub
      }

 @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
          // TODO Auto-generated method stub
      }

 @Override
     public void onProgressChanged(SeekBar seekBar, int progress,
                            boolean fromUser) {
            // TODO Auto-generated method stub

             rotation += 90;
             rotation %= 360;
             Bitmap bitmap = getRotatedBitmap(scaledBitmap);
             selectedImage.setImageBitmap(bitmap);
     }
   });     

private Bitmap getRotatedBitmap(Bitmap bitmap) {
    if (rotation % 360 == 0) {
        return bitmap;
    }
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation, bitmap.getWidth() / 2,
                      bitmap.getHeight() / 2);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                        bitmap.getHeight() / 2, matrix, true);
}

I don't know how to calculate Rotation value and apply to seek bar when track forward in which case the seek bar that image should rotate gradually and when track backward it should rotate gradually reverse. 我不知道如何计算“旋转”值并在向前跟踪时应用于搜索栏,在这种情况下,图像应该逐渐旋转的搜索栏,而在向后跟踪时它应该逐渐反向旋转。

You can try this. 你可以试试看 I have used drawable image and get bitmap from it. 我使用了可绘制图像并从中获取位图。

 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int progress = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
                progress = progresValue;

                Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                Matrix mat = new Matrix();
                mat.postRotate(progresValue);
                Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
                imageView.setImageBitmap(bMapRotate);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

No need of creating new bitmap everytime. 无需每次都创建新的位图。 Use just matrix instead of 仅使用矩阵而不是

float pivX=imageView.getDrawable().getBounds().width()/2;
float pivY=imageView.getDrawable().getBounds().height()/2;
Matrix matrix = new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

if APK Level >=11 如果APK等级> = 11

imageView.setRotation(angle);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = 0;

        @Override
        public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
            mat.postRotate(progresValue, centerX, centerY);
            imageView.setImageMatrix(mat);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 位图bMap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);

Matrix mat = new Matrix(); 矩阵垫= new Matrix();

create your Bitmap and matrix. 创建您的位图和矩阵。 You should be avoid to create bitmap every time. 您应该避免每次都创建位图。 Instead of that one you use matrix. 而不是那个,您使用矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM