0

我正在尝试创建一个简单的放大/缩小视图。

所以我扩展LinearLayout并添加两个ImaegView作为inout按钮,这是代码:

public class ZoomView extends LinearLayout{

    private ImageView mZoomIn;
    private ImageView mZoomOut;


    public ZoomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setupView();
    }

    private void setupView() {
        mZoomIn = new ImageView(getContext());
        mZoomOut = new ImageView(getContext());

        mZoomIn.setBackgroundDrawable(getResources().getDrawable(R.drawable.map_zoomin_btn));
        mZoomIn.setOnClickListener(this);

        mZoomOut.setBackgroundDrawable(getResources().getDrawable(R.drawable.map_zoomout_btn));
        mZoomOut.setOnClickListener(this);


        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setOrientation(LinearLayout.HORIZONTAL);
        } else {
            setOrientation(LinearLayout.VERTICAL);
        }
        addView(mZoomIn);
        addView(mZoomOut);
    }
}

如代码所示,我想ZoomView根据屏幕改变它的方向。

但是我只有两张图片:

现在这就是我在portrait视图中得到的(这就是我想要的):

在此处输入图像描述

但是,这是为了landscape查看: 在此处输入图像描述

虽然我想landscape查看这个:

在此处输入图像描述

然后我想知道我是否可以在没有新位图的情况下实现我的要求?也许旋转图像视图或其他东西?我试过了:

mZoomIn.setRotate(-90);

或者

        Matrix matrix = new Matrix();
        matrix.postRotate(-90);
        mZoomIn.setScaleType(ImageView.ScaleType.MATRIX);   //required
        mZoomIn.setImageMatrix(matrix);

但这不起作用。有什么建议吗?

4

1 回答 1

0

试试这个:

mZoomIn 的值:

Matrix matrix=new Matrix();
mZoomIn.setScaleType(ScaleType.MATRIX);
matrix.postRotate( -90f, mZoomIn.getDrawable().getBounds().width()/2,
mZoomIn.getDrawable().getBounds().height()/2);
imageView.setImageMatrix(matrix);

和 mZoomOut 一样:

 matrix.postRotate( 90f, mZoomOut.getDrawable().getBounds().width()/2,
 mZoomOut.getDrawable().getBounds().height()/2);

不要忘记使用fin90f或者只是做 atype casting
的正确实现matrix.postRotate如下:

matrix.postRotate((float) angle, pivX, pivY);

希望这对你有用。

于 2013-11-12T09:19:47.750 回答