Android:如何从中心旋转图像?

时间:2014-09-04 23:13:44

标签: java android bitmap rotation

我正在尝试将此位图在中心旋转“R0”度。但它不会在中心旋转。它从左上角旋转。

这是我的代码:

public class MainActivity extends Activity{

    Player ourView;

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ourView = new Player(this);

        setContentView(ourView);
    }

    protected void onPause() {
        super.onPause();
        ourView.pause();
    }

    protected void onResume() {
        super.onResume();
        ourView.resume();
    }
}

这是另一个类:

public class Player extends SurfaceView implements Runnable {

    Canvas canvas = new Canvas();
    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = true;

    public Player(Context context) {
        super(context);
        ourHolder = getHolder();
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void pause() {
        isRunning = false;
        while(true){
        try{
            ourThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        break;
    }
    ourThread = null;
}

    public void resume() {
        isRunning = true;
    }

    public void run() {
        while(isRunning) {
            if(!ourHolder.getSurface().isValid())
                continue;

                canvas = ourHolder.lockCanvas();
                canvas.drawRGB(30, 30, 200);

                MainPlayer player = new MainPlayer();
                player.execute();

            ourHolder.unlockCanvasAndPost(canvas);
        }           
    }
}

这是位图旋转的类:

public class MainPlayer{

     Canvas canvas = new Canvas();
     Context context;
     float x=160;
     float y=455;
     float k=160;
     float l=455;
     float R0 = 150;

    public void execute(){
        draw();
    }

    public void draw(){

        Matrix matrix = new Matrix();
        Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        matrix.setRotate(R0);   

        Bitmap b = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);  
        canvas.drawBitmap(b, x, y, null);

    }
}

非常感谢。

0 个答案:

没有答案