在android中使用seekBar旋转图像

时间:2015-08-26 15:21:54

标签: android

我有以下代码来旋转图像,但是当我拖动搜索栏时,它会在旧图像上创建一个新的位图(它应该旋转旧图像而不是创建新图像),有人可以帮我整理这个东西吗。? 我访问了我的旧问题,但没有人为我解决这个问题

公共类MainActivity扩展了Activity实现OnSeekBarChangeListener {

ImageView iv_image;
SeekBar seekbar;
Bitmap source;
float angle = 0;
Button buttonClick;
Bitmap rotatedImage;
static Matrix matrix;
Matrix m1 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    buttonClick = (Button) findViewById(R.id.bt);
    buttonClick.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            angle += 90;

            iv_image.buildDrawingCache();
            source = iv_image.getDrawingCache();
            rotatedImage = rotateImage(source, angle);
            iv_image.setImageBitmap(rotatedImage);
        }
    });
}

void init() {
    iv_image = (ImageView) findViewById(R.id.iv_image);
    seekbar = (SeekBar) findViewById(R.id.seekbar);
    seekbar.setOnSeekBarChangeListener(this);
   // seekbar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this);

}

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

    angle = progress;
    iv_image.buildDrawingCache();
    source = iv_image.getDrawingCache();
    rotatedImage = rotateImage(source, angle);
    iv_image.setImageMatrix(null);
    iv_image.setImageBitmap(rotatedImage);
}

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

}

public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

public static Bitmap rotateImage(Bitmap sourceImage, float angle) {
    matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(),
            sourceImage.getHeight(), matrix, true);

}

2 个答案:

答案 0 :(得分:2)

请改为尝试:

public static void rotateImage(float angle) {
    matrix = new Matrix();
    matrix.postRotate(angle);
    imageView.setImageMatrix(matrix);
    imageView.invalidate();
}

这是你的界面实现:

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    angle = progress;
    rotateImage(angle);
}

这样你就可以在ImageView中旋转Bitmap,效率更高。

您甚至可以通过使其成为私有成员并在轮换后调用 imageView.invalidate()来重用Matrix对象,以优化您的代码。

答案 1 :(得分:0)

public static void rotateImage(float angle) {
    matrix = new Matrix();
    matrix.postRotate(angle);
    imageView.setScaleType(ImageView.ScaleType.MATRIX);
    imageView.setImageMatrix(matrix);
    imageView.invalidate();
}