514
技術社區[雲棲]
Android兩種不同的方法去實現圖像的放大與縮小(很有幫助)
其實不算兩種不同的方法,隻是一個方法用的是硬編碼,而另一個用的是MVC設計模式,用的都是同一個類Matrix。第一種:硬編碼方式
MainActivity.java
package com.android.yhb;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
private ImageView mImageView;
private Button mButton01;
private Button mButton02;
private AbsoluteLayout layout1;
private Bitmap bmp;
private int id = 0;
private int displayWidth;
private int displayHeight;
private float scaleWidth = 1;
private float scaleHeight = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 取得屏幕分辨率的大小 */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
/* 加載資源 */
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.me);
mImageView = (ImageView) findViewById(R.id.imageView);
layout1 = (AbsoluteLayout) findViewById(R.id.layout);
mButton01 = (Button) findViewById(R.id.button_small);
mButton02 = (Button) findViewById(R.id.button_big);
/* 計算出來的高度要減去Button的高度 */
displayHeight = dm.heightPixels-mButton01.getHeight();
Log.e("Tag", " " + displayHeight);//這塊通過Log輸出發現並沒有獲得Button的高度,不知道怎麼回事!
/* 縮小監聽 */
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
small();
}
});
/* 放大監聽 */
mButton02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
big();
}
});
}
/* small method */
private void small() {
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
/* 設置圖片縮小比例 */
double scale = 0.8;
/* 計算出縮小後的長寬 */
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
/* 產生Resize後的Bitmap對象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
matrix, true);
if (id == 0) {
/* 如果是第一次單擊縮小按鈕,就刪除原來默認的ImageView */
layout1.removeView(mImageView);
} else {
/* 不然就刪除上次放大或縮小產生的ImageView */
layout1.removeView((ImageView) findViewById(id));
}
/* 產生新的ImageView,放入Resize後的Bitmap對象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
/* 將mButton02設置成可點擊的 */
mButton02.setEnabled(true);
}
/* big method */
private void big() {
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
/* 放大變量 */
double scale = 1.1;
/* 放大以後的寬高,一定要強製轉換為float型的 */
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
/* 產生resize後的Bitmap對象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
matrix, true);
if (id == 0) {
/* 如果是第一次按就刪除原來設置的ImageView */
layout1.removeView(mImageView);
} else {
/* 如果不是第一次按,就刪除上次放大or縮小的ImageView */
layout1.removeView((ImageView) findViewById(id));
}
/* 產生新的ImageView,放入Resize後的Bitmap對象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
/* 如果再放大會超過屏幕大小,就把Button disable */
if (scaleWidth * scale * bmpWidth > displayWidth
|| scaleHeight * scale * bmpHeight > displayHeight) {
Log.e("Tag", " " + scaleHeight * scale * bmpHeight);
mButton02.setEnabled(false);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:andro
>
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/big"
android:layout_x="61px"
android:layout_y="380px"
>
</Button>
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/small"
android:layout_x="175px"
android:layout_y="380px"
>
</Button>
<ImageView
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/me"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>
</AbsoluteLayout>
關於布局這塊,那麼細致的去確定位置坐標,真是有點麻煩,我是用一款專門的設計軟件droiddraw去做的,咋此可以下載:
droiddraw-r1b14.rar
實現的效果如圖1:

此圖為放大到最大時的截圖,放大按鈕被置為disabled。
注意:由於在代碼70-73行有強製類型轉換,所以每次放大後的長寬高都有誤差,所以不是每次放大都是精準的,同理,縮小也一樣。
第二種:在XML裏設計(MVC)
自定義View.java
package com.android.yhb;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
public class GameView extends View implements Runnable {
private int BitmapWidth = 0;
private int BitmapHeight = 0;
Bitmap mBitmap = null;
float Scale = 1.0f;
Matrix mMatrix = new Matrix();
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mBitmap = ((BitmapDrawable) getResources().getDrawable(
R.drawable.myicon)).getBitmap();
BitmapWidth = mBitmap.getWidth();
BitmapHeight = mBitmap.getHeight();
new Thread(this).start();
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
postInvalidate();
}
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
mMatrix.reset();
mMatrix.postScale(Scale, Scale);
Bitmap mBitmap2 = Bitmap.createBitmap(mBitmap, 0, 0, BitmapWidth,
BitmapHeight, mMatrix, true);
GameView.drawImage(canvas, mBitmap2, (320 - BitmapWidth) / 2, 10);
mBitmap2 = null;
}
public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y) {
canvas.drawBitmap(bitmap, x, y, null);
}
}
MainActivity.Java
package com.android.yhb;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
public class MainActivity extends Activity {
GameView myGameView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*setContentView(R.layout.main);*/
myGameView = new GameView(this);
setContentView(myGameView);
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_DPAD_DOWN) {
if (myGameView.Scale > 0.1) {
Log.e("Tag", "=====>KEYCODE_DPAD_UP");
myGameView.Scale -= 0.1;
}
} else if (keycode == KeyEvent.KEYCODE_DPAD_UP) {
if (myGameView.Scale < 2.5) {
myGameView.Scale += 0.1;
}
}
return true;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--<com.android.yhb.GameView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
--><TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
這裏注意應該把按鍵監聽語句放在MainActivity.java代碼裏麵,不應該放在自定義View裏麵進行監聽,這樣會出錯。
總結:
-
/* 加載資源 */
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.me); 或mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.myicon)).getBitmap(); - 第二個通過鍵盤去放大縮小,我試著通過添加按鈕區控製,但是總是出錯。
- 通過對代碼的比較,顯然易見,MVC的代碼更清楚,但是用硬編碼有硬編碼的好處,硬代碼編程運行時可以加快調用速度,而MVC會增加內存。
- 在Android中不允許ImageView在產生後,動態修改其長度和寬度,所以為了實現圖片放大縮小功能,使用的方式是當用戶在單擊放大或縮小的按鈕時,除了將圖片作放大或縮小的動作外,並將原來Layout中ImageView刪除,重新產生一個新的ImageView,指定圖片給它後,再放入Layout裏,用戶看來,就好像同一張圖片再放大或縮小。即方法一的代碼。
- 代碼有很多不足,希望大家自己去修改,也希望高人能在此給予指點。
最後更新:2017-04-02 16:47:34