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

时间:2015-05-21 05:31:32

标签: android android-intent android-activity

我正在创建一个Android应用程序,将图像旋转360度。当我跟踪Seek条时,图像应该旋转。

你能帮我解决一下这个问题吗?

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);
}

我不知道如何计算旋转值并在向前追踪时应用于搜索条,在这种情况下图像应该逐渐旋转的搜索条,当向后轨道时它应该逐渐反向旋转。

3 个答案:

答案 0 :(得分:0)

你可以试试这个。我使用了drawable图像并从中获取位图。

 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) {

            }
        });

答案 1 :(得分:0)

  

无需每次都创建新的位图。仅使用矩阵而不是

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);
  

如果APK级别> = 11

imageView.setRotation(angle);

答案 2 :(得分:0)

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) {

        }
    });

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

Matrix mat = new Matrix();

创建您的位图和矩阵。您应该避免每次都创建位图。而不是那个你使用矩阵。