利用Matrix class 旋轉圖像

1 篇文章 / 0 new
author
利用Matrix class 旋轉圖像
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <SeekBar android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_margin="10px"
            android:id="@+id/rotatebar" android:max="360" android:progress="0" />
    </LinearLayout>
    <ImageView android:id="@+id/imageview"
        android:layout_gravity="center" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:scaleType="center" />
</LinearLayout>
src
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
 
public class AndroidImage extends Activity {
    private ImageView myImageView;
    private float scale, rotateDegree;
    private Bitmap bitmap;
    int bmpWidth, bmpHeight;
    SeekBar rotateBar;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myImageView = (ImageView) findViewById(R.id.imageview);
        rotateBar = (SeekBar) findViewById(R.id.rotatebar);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        bmpWidth = bitmap.getWidth();
        bmpHeight = bitmap.getHeight();
        scale = 1;
        rotateDegree = 0;
        loadImage();
        rotateBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {    }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {    }
                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    rotateDegree = (float) progress;
                    loadImage();
                }
            }
        );
    }
 
    private void loadImage() {
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        matrix.setRotate(rotateDegree);
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth,
                bmpHeight, matrix, true);
        myImageView.setImageBitmap(scaledBitmap);
    }
}

from http://androidbiancheng.blogspot.com/2010/07/matrixbitmap_05.html
Free Web Hosting