或是讀取進來的圖片尺寸可能都有各式各樣的,這樣很容易破壞我們原來的版型,怎麼辦呢?
今天就介紹怎麼縮放圖片,讓圖片變大、變小、變成我們想要的。
為了讓讀者比較容易了解及讀取方便,所以我會故意把三個按鈕的處理程序都拆開來寫
,而不是寫在一起,目的是為了易於讀取,不然用程序 + switch case寫在一起就好了
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_Center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/button_center" />
<Button
android:id="@+id/btn_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_toRightOf="@+id/btn_Center"
android:text="@string/button_right" />
<Button
android:id="@+id/btn_Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="30dp"
android:layout_toLeftOf="@+id/btn_Center"
android:text="@string/button_left" />
</RelativeLayout>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">圖片縮放</string>
<string name="button_left">放大</string>
<string name="button_right">縮小</string>
<string name="button_center">變為480*480</string>
</resources>
DemoZoomActivity.java
package jim.demo.zoom;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class DemoZoomActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//圖片
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//放大
Button btnLeft = (Button)findViewById(R.id.btn_Left);
//指定大小
Button btnCenter = (Button)findViewById(R.id.btn_Center);
//縮小
Button btnRight = (Button)findViewById(R.id.btn_Right);
//放大按鈕事件
View.OnClickListener listener_left = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//imageView轉Bitmap
imageView.buildDrawingCache();
Bitmap bmp=imageView.getDrawingCache();
//轉換為圖片指定大小
//獲得圖片的寬高
int width = bmp.getWidth();
int height = bmp.getHeight();
//放大為1.2倍
float scaleWidth = (float) 1.2;
float scaleHeight = (float) 1.2;
// 取得想要缩放的matrix參數
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的圖片
Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
//重新載入 imageView
imageView.setImageBitmap(newbm);
}
};
btnLeft.setOnClickListener(listener_left);
//指定大小按鈕事件
View.OnClickListener listener_center = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//imageView轉Bitmap
imageView.buildDrawingCache();
Bitmap bmp=imageView.getDrawingCache();
//轉換為圖片指定大小
//獲得圖片的寬高
int width = bmp.getWidth();
int height = bmp.getHeight();
// 設置想要的大小
int newWidth = 480;
int newHeight = 480;
// 計算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix參數
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的圖片
Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
//重新載入 imageView
imageView.setImageBitmap(newbm);
}
};
btnCenter.setOnClickListener(listener_center);
//縮小按鈕事件
View.OnClickListener listener_right = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//imageView轉Bitmap
imageView.buildDrawingCache();
Bitmap bmp=imageView.getDrawingCache();
//轉換為圖片指定大小
//獲得圖片的寬高
int width = bmp.getWidth();
int height = bmp.getHeight();
//縮小為0.8倍
float scaleWidth = (float) 0.8;
float scaleHeight = (float) 0.8;
// 取得想要缩放的matrix參數
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的圖片
Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
//重新載入 imageView
imageView.setImageBitmap(newbm);
}
};
btnRight.setOnClickListener(listener_right);
}
}
執行畫面
後記:
如果試著一直放大縮小你會發現一種現象:"失真了",因為把小圖放的太大本來
就會失真,所以最常用的是在讀取圖片後改變圖片比例到我們指定的大小,要不失真也
行,請準備一張大圖,這樣就比較不會失真了,不然系統內的圖片就如同啟始畫面一樣
小小一張而已,放大就會失真,這是圖片一定會有的狀況,謝謝收看。




你好你的教學對我們剛學APP的幫助很大
回覆刪除請問 Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
這行的意思是我重新定義一張新圖用matrix.postScale(scaleWidth, scaleHeight);
這陣列的大小去實現是嗎?
那如果我想要調整照片的明暗度的話該用drawable 還是 bitmap
Function該怎麼用比較恰當ㄋ
請問一下,我照您的方法做了,但這個問題一職出現,都無法成功,可以請您告訴我該如何解決嗎?
回覆刪除會出現這個:ImageView1 cannot be resolved or is not a field
這樣的資訊太少了,可以詳細說明是做了什麼動作後出現的?有畫面可以附上最好,可以寄到我的信箱:joejoejoe3232@gmail.com,資訊太少我也很難知道是哪一個地方出的問題,盡量愈詳細愈好
刪除我在寫這文章時的環境是用Android2.2版的,而且有執行過,所以正常照抄應該都是可以執行的。
謝謝您,我有寄信給您了,希望您可以幫我看看
回覆刪除已回信,妳再試看看。
刪除有一個問題是, 每一次變更圖像大小時就拿上一次的圖來變更, 所以失真的問題會不斷增加上去
回覆刪除有解決辦法嗎? 如重新拿原圖一次來變更大小
您好,在網路上參考到您的教學網站,我想請問ImageView的縮小具有圖像"壓縮"的效果嗎?
回覆刪除