Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

在 Android 中實現圖片縮放與旋轉

編輯:Android 程式設計入門

圖片處理在程式開發中經常用到。手機上的圖片瀏覽器能將圖片放大顯示,也能顯示為縮略圖,還可以將圖片橫豎切換觀看,這些都需要對圖片進行縮放和旋轉。下面來看看 Android 中處理圖片縮放與旋轉的步驟。

Matrix 是 Android 中用於操作圖像的類,其中的 postScale 和 postRotate 分別實現圖片縮放和旋轉,具體步驟如下:

一、圖片縮放

(1) 先加載要操作的圖片

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gilr);

(2) 取得加載圖片的高度與寬度

int height = bitmap.getHeight();
int width = bitmap.getWidth();

(3) 定義要縮放成的最終圖片高度與寬度

int nHeight = 200;
int nWidth = 150;

(4) 計算縮放比例

float scaleWidth = ((float) nWidth)/width;
float scaleHeight = ((float) nHeight)/height;

(5) 創建 Matrix 物件

Matrix matrix = new Matrix();

(6) 使用 Matrix 物件與縮放比例實現縮放

matrix.postScale(scaleWidth, scaleHeight);

(7) 最後生成縮放後的圖片

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

二、圖片旋轉

(1) Matrix matrix = new Matrix();

(2) matrix.postRotate(60);

(3) Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

三、完整範例

以下是對圖片進行縮放與旋轉的完整例子。在 .xml 文件中:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="150dp"
android:scaleType="fitCenter"
android:src="@drawable/gilr"/>

<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="圖像寬度:240圖像高度:160"/>
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:max="240"
android:progress="120"
android:layout_marginLeft="10dp"></SeekBar>
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="0度"/>
<SeekBar
android:id="@+id/seekbar2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:max="360"></SeekBar>

</LinearLayout>

在 .java 文件中:

public class MainActivity extends Activity implements OnSeekBarChangeListener{ private int minWidth = 80; //圖片最小寬度 private ImageView imageView; private TextView textView1; private TextView textView2; private SeekBar seekBar1; private SeekBar seekBar2; private Matrix matrix = new Matrix(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageview); textView1 = (TextView) findViewById(R.id.textview1); textView2 = (TextView) findViewById(R.id.textview2); seekBar1 = (SeekBar) findViewById(R.id.seekbar1); seekBar2 = (SeekBar) findViewById(R.id.seekbar2); seekBar1.setOnSeekBarChangeListener(this); seekBar2.setOnSeekBarChangeListener(this); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); seekBar1.setMax(dm.widthPixels - minWidth); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO 自動生成的方法存根 if(seekBar.getId() == R.id.seekbar1){ int newWidth = progress + mainWidth; int newHeight = (int)(newWidth * 3 / 4); //圖片高寬比列一定要以.xml文件中的ImageVie中高寬比列相同,否則實效 imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight)); //這裡是通過布局大縮放圖片大小 textView1.setText("圖像寬度:" + newWidth + "圖像高度" + newHeight); }else if (seekBar.getId() == R.id.seekbar2) { Bitmap bitmap = ((BitmapDrawable)(getResources().getDrawable(R.drawable.gilr))).getBitmap(); matrix.setRotate(progress);//設置翻轉的角度 bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); imageView.setImageBitmap(bitmap); textView2.setText(progress + "度"); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO 自動生成的方法存根 } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO 自動生成的方法存根 } }

注意:

Android.util 包下的 DisplayMetrics 類提供了一種關於顯示的通用資訊,如顯示大小、解析度和字體。

為了獲取 DisplayMetrics 成員,首先初始化一個物件:

DisplayMetrics display = new DisplayMetrics();

Log.d("DisplayMetrics ", display.toString()); //直接在這裡輸出,資訊都是 0。

getWindowManager().getDefaultDisplay().getMetrics(display); //將當前窗口的一些資訊放在 DisplayMetrics 類中。

Log.d("DisplayMetrics", display.toString()); //然後就可以透過 displayMetrics 類來獲取當前窗口的一些資訊。

注:構造函數 DisplayMetrics 不需要傳遞任何參數;getDefaultDisplay() 方法將取得的寬高維度存放於 DisplayMetrics 物件中,而取得的寬高維度是以像素為單位(Pixel),「像素」所指的是「絕對像素」而非「相對像素」。透過 DisplayMetrics 的 toString() 方法可以獲取到 DisplayMetrics 的大部分 fields 資訊。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved