main.xml
RotateImage.java
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class RotateImage extends Activity { private ImageView img; private Button btn; private Bitmap bmp; private int r = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img=(ImageView)findViewById(R.id.ImageView01); btn = (Button)findViewById(R.id.btn); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub r += 90; if(r >= 360) r=0; setRotation(r); } }); } private void setRotation(int rotate){ // Getting width & height of the given image. int w = bmp.getWidth(); int h = bmp.getHeight(); // Setting post rotate to 90 Matrix mtx = new Matrix(); mtx.postRotate(rotate); // Rotating Bitmap Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true); BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); img.setImageDrawable(bmd); } }
0 comments:
Post a Comment