Android示例中的图像拉直

时间:2015-04-16 05:54:28

标签: android image-rotation

我想实现矫直,以便用户可以旋转图像。有人可以提供代码片段或图像矫直的示例吗?我试图找到一个例子,但失败了。

提前致谢

1 个答案:

答案 0 :(得分:3)

enter image description here

以下是一个示例实现:

<强> HomeActivity.java

public class HomeActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    private ImageView mImageView;
    private Matrix mMatrix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // load bitmap from resource
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.sample2);

        // calculate suitable width / height
        int WIDTH = getWindowManager().getDefaultDisplay().getWidth();
        int HEIGHT = (int)((image.getHeight() / (image.getWidth() * 1.0f)) * WIDTH);

        // scale image to be visible on screen
        image = Bitmap.createScaledBitmap(image, WIDTH, HEIGHT, false);

        // configure image view accordingly
        mImageView = (ImageView) findViewById(R.id.imageView);
        mImageView.setLayoutParams(new RelativeLayout.LayoutParams(WIDTH, HEIGHT));
        mImageView.setImageBitmap(image);

        SeekBar seekbar = (SeekBar) findViewById(R.id.rotate_bar);
        seekbar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        float angle = (progress - 45);
        float width = mImageView.getDrawable().getIntrinsicWidth();
        float height = mImageView.getDrawable().getIntrinsicHeight();

        if (width > height) {
            width = mImageView.getDrawable().getIntrinsicHeight();
            height = mImageView.getDrawable().getIntrinsicWidth();
        }

        float a = (float) Math.atan(height/width);

        // the length from the center to the corner of the green
        float len1 = (width / 2) / (float) Math.cos(a - Math.abs(Math.toRadians(angle)));
        // the length from the center to the corner of the black
        float len2 = (float) Math.sqrt(Math.pow(width/2,2) + Math.pow(height/2,2));
        // compute the scaling factor
        float scale = len2 / len1;

        Matrix matrix = mImageView.getImageMatrix();
        if (mMatrix == null) {
            mMatrix = new Matrix(matrix);
        }
        matrix = new Matrix(mMatrix);

        float newX = (mImageView.getWidth() / 2) * (1 - scale);
        float newY = (mImageView.getHeight() / 2) * (1 - scale);
        matrix.postScale(scale, scale);
        matrix.postTranslate(newX, newY);
        matrix.postRotate(angle, mImageView.getWidth() / 2, mImageView.getHeight() / 2);
        mImageView.setImageMatrix(matrix);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }
}

<强> activity_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".HomeActivity">

    <ImageView
        android:id="@+id/imageView"
        android:scaleType="matrix"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

    <SeekBar
        android:id="@+id/rotate_bar"
        android:max="90"
        android:progress="45"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

注意:您需要drawable名为sample2才能使上述代码生效。