1

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

问题是,当我缩放图像(仅 onCreate)时,在第一次(也是第一次)使用旋转方法之后,我的视图又调整了一次,我不知道为什么。

让我向您发布我的旋转和缩放方法。玫瑰是我的 ImageView 实例(ImageView 玫瑰)

请查看并发布任何建议。我应该尝试不同的方式来缩放我的 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);
}

规模(找到源链接

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

1 回答 1

1

好的,我知道了!

代替

view.setImageDrawable(result);

在 Scale 方法中

view.setImageBitmap(scaledBitmap);

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

于 2012-11-11T23:41:26.267 回答