缩放和旋转imageview

时间:2012-11-11 22:19:44

标签: java android

我准备了一些指南针活动。 我有我的风.png,我希望在活动开始时缩放它,并在方向改变后旋转。

问题在于,当我缩放图像时(仅限onCreate),在第一次(也是第一次)使用rotate方法后,我的视图再次调整大小,我不知道为什么。

让我发布我的旋转和缩放方法。 rose是我的ImageView实例(ImageView rose)

请查看并发布任何建议。我应该尝试不同的方式来扩展我的ImageView吗?

感谢您的帮助!

旋转(看起来工作正常)

public void rotateRose(float angle){

            angle = 360 - angle;
            Matrix matrix=new Matrix();
            rose.setScaleType(ScaleType.MATRIX);
            pivX = rose.getDrawable().getBounds().width()/2;
            pivY = rose.getDrawable().getBounds().height()/2;
            matrix.preRotate((float) angle, pivX, pivY);
            rose.setImageMatrix(matrix);
}

比例(找到source link

private void scaleImage(ImageView view, int boundBoxInDp)
    {
        // Get the ImageView and its bitmap
        Drawable drawing = view.getDrawable();
        Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

        // Get current dimensions
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // Determine how much to scale: the dimension requiring less scaling is
        // closer to the its side. This way the image always stays inside your
        // bounding box AND either x/y axis touches it.
        float xScale = ((float) boundBoxInDp) / width;
        float yScale = ((float) boundBoxInDp) / height;
        float scale = (xScale <= yScale) ? xScale : yScale;

        // Create a matrix for the scaling and add the scaling data
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);

        // Create a new bitmap and convert it to a format understood by the ImageView
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        BitmapDrawable result = new BitmapDrawable(scaledBitmap);
        width = scaledBitmap.getWidth();
        height = scaledBitmap.getHeight();

        // Apply the scaled bitmap
        view.setImageDrawable(result);

        // Now change ImageView's dimensions to match the scaled image
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);

        //pivX = width/2;
       // pivY = height/2;
    }

1 个答案:

答案 0 :(得分:1)

好的,我知道了!

替换

view.setImageDrawable(result);
使用

在Scale方法中

view.setImageBitmap(scaledBitmap);

如果您正在寻找图像视图的缩放和旋转方法:以上这些工作正常(通过这个小改动)