Android 旋轉圖片
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;public class RotateBitmapActivity extends Activity { Button btnRotate1, btnRotate2; ImageView ivBitmap; //逆時針轉45度 RotateAnimation rotateAnimation = new RotateAnimation(0, -45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setTitle("旋轉圖片"); setContentView(R.layout.rorate_bitmap); btnRotate1 = (Button) findViewById(R.id.btn_rorate); btnRotate2 = (Button) findViewById(R.id.btn_rorate_anim); ivBitmap = (ImageView) findViewById(R.id.iv_bitmap); btnRotate1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub rotateBitmap1(); } }); btnRotate2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub rotateBitmap2(); } }); } /** * 使用矩陣對象 旋轉 */ private void rotateBitmap1() { LinearLayout linLayout = new LinearLayout(this); BitmapDrawable bitmapDrawable = (BitmapDrawable) ivBitmap.getDrawable(); //獲取這個圖片的寬和高 int width = bitmapDrawable.getBitmap().getWidth(); int height = bitmapDrawable.getBitmap().getHeight(); //定義預轉換成的圖片的寬度和高度 int newWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth() / 2; int newHeight = getWindow().getWindowManager().getDefaultDisplay().getHeight() / 2; //計算縮放率,新尺寸除原始尺寸 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 創建操作圖片用的matrix對象 Matrix matrix = new Matrix(); // 縮放圖片動作 matrix.postScale(scaleWidth, scaleHeight); //旋轉圖片 動作 matrix.postRotate(90); // 創建新的圖片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapDrawable.getBitmap(), 0, 0, width, height, matrix, true); ivBitmap.setImageBitmap(resizedBitmap); } /** * 通過動畫方式旋轉,實際位置沒有改變,當再次運行該函數,圖像還是從原來的位置開始轉動 */ private void rotateBitmap2() { rotateAnimation.setDuration(1000); ivBitmap.startAnimation(rotateAnimation); rotateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub rotateAnimation.setFillAfter(true);//動畫后的效果固定下來,實際圖片位置沒有改變 //逆時針轉45度 坐標點算不好, // ivBitmap.layout(l, t, r, b); } }); } } </pre>
本文由用戶 cp5m 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!