Android 圖片縮放與旋轉(zhuǎn)的實(shí)現(xiàn)詳解
更新時(shí)間:2013年06月19日 09:07:58 作者:
本篇文章是對(duì)在Android中實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
本文使用Matrix實(shí)現(xiàn)Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。示例代碼如下:
package com.android.matrix;
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.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
/**
* Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。
* @author Administrator
*
*/
public class MatixActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。");
LinearLayout linLayout = new LinearLayout(this);
//加載需要操作的圖片,這里是一張圖片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);
//獲取這個(gè)圖片的寬和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
//定義預(yù)轉(zhuǎn)換成的圖片的寬度和高度
int newWidth = 200;
int newHeight = 200;
//計(jì)算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 創(chuàng)建操作圖片用的matrix對(duì)象
Matrix matrix = new Matrix();
// 縮放圖片動(dòng)作
matrix.postScale(scaleWidth, scaleHeight);
//旋轉(zhuǎn)圖片 動(dòng)作
matrix.postRotate(45);
// 創(chuàng)建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
//將上面創(chuàng)建的Bitmap轉(zhuǎn)換成Drawable對(duì)象,使得其可以使用在ImageView, ImageButton中
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//創(chuàng)建一個(gè)ImageView
ImageView imageView = new ImageView(this);
// 設(shè)置ImageView的圖片為上面轉(zhuǎn)換的圖片
imageView.setImageDrawable(bmd);
//將圖片居中顯示
imageView.setScaleType(ScaleType.CENTER);
//將ImageView添加到布局模板中
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// 設(shè)置為本activity的模板
setContentView(linLayout);
}
}
上例是靜態(tài)地實(shí)現(xiàn)圖片縮放,下例中可以通過(guò)鼠標(biāo)滑輪和方向鍵實(shí)現(xiàn)圖片動(dòng)態(tài)的放大與縮小。
程序結(jié)構(gòu)如下圖:

Zoom.java文件中代碼:
package com.android.zooming;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
public class Zoom extends View {
private Drawable image;
private int zoomControler=20;
public Zoom(Context context){
super(context);
image=context.getResources().getDrawable(R.drawable.x);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//控制圖像的寬度和高度
image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
image.draw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)//放大
zoomControler+=10;
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) //縮小
zoomControler-=10;
if(zoomControler<10)
zoomControler=10;
invalidate();
return true;
}
}
復(fù)制代碼 代碼如下:
package com.android.matrix;
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.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
/**
* Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。
* @author Administrator
*
*/
public class MatixActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。");
LinearLayout linLayout = new LinearLayout(this);
//加載需要操作的圖片,這里是一張圖片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);
//獲取這個(gè)圖片的寬和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
//定義預(yù)轉(zhuǎn)換成的圖片的寬度和高度
int newWidth = 200;
int newHeight = 200;
//計(jì)算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 創(chuàng)建操作圖片用的matrix對(duì)象
Matrix matrix = new Matrix();
// 縮放圖片動(dòng)作
matrix.postScale(scaleWidth, scaleHeight);
//旋轉(zhuǎn)圖片 動(dòng)作
matrix.postRotate(45);
// 創(chuàng)建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
//將上面創(chuàng)建的Bitmap轉(zhuǎn)換成Drawable對(duì)象,使得其可以使用在ImageView, ImageButton中
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//創(chuàng)建一個(gè)ImageView
ImageView imageView = new ImageView(this);
// 設(shè)置ImageView的圖片為上面轉(zhuǎn)換的圖片
imageView.setImageDrawable(bmd);
//將圖片居中顯示
imageView.setScaleType(ScaleType.CENTER);
//將ImageView添加到布局模板中
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// 設(shè)置為本activity的模板
setContentView(linLayout);
}
}
上例是靜態(tài)地實(shí)現(xiàn)圖片縮放,下例中可以通過(guò)鼠標(biāo)滑輪和方向鍵實(shí)現(xiàn)圖片動(dòng)態(tài)的放大與縮小。
程序結(jié)構(gòu)如下圖:

Zoom.java文件中代碼:
復(fù)制代碼 代碼如下:
package com.android.zooming;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
public class Zoom extends View {
private Drawable image;
private int zoomControler=20;
public Zoom(Context context){
super(context);
image=context.getResources().getDrawable(R.drawable.x);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//控制圖像的寬度和高度
image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
image.draw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)//放大
zoomControler+=10;
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) //縮小
zoomControler-=10;
if(zoomControler<10)
zoomControler=10;
invalidate();
return true;
}
}
您可能感興趣的文章:
- Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android中利用matrix 控制圖片的旋轉(zhuǎn)、縮放、移動(dòng)
- Android Tween動(dòng)畫(huà)之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
- Android開(kāi)發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android編程中調(diào)用Camera時(shí)預(yù)覽畫(huà)面有旋轉(zhuǎn)問(wèn)題的解決方法
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(二)Animation實(shí)現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
- Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫(huà)效果
- Android部分手機(jī)拍照后獲取的圖片被旋轉(zhuǎn)問(wèn)題的解決方法
- android實(shí)現(xiàn)icon動(dòng)態(tài)旋轉(zhuǎn)效果
相關(guān)文章
Android?Java?try?catch?失效問(wèn)題及解決
這篇文章主要介紹了Android?Java?try?catch?失效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Android 自定義SeekBar 實(shí)現(xiàn)分段顯示不同背景顏色的示例代碼
這篇文章主要介紹了Android 自定義SeekBar 實(shí)現(xiàn)分段顯示不同背景顏色,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序 附源碼
這篇文章主要幫助大家簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序,附源碼下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)音樂(lè)播放器歌詞顯示效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)音樂(lè)播放器歌詞顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android 獲取未安裝的APK圖標(biāo)、版本號(hào)、包名等信息方法
下面小編就為大家分享一篇Android 獲取未安裝的APK圖標(biāo)、版本號(hào)、包名等信息方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2018-01-01AndroidStudio圖片壓縮工具ImgCompressPlugin使用實(shí)例
這篇文章主要為大家介紹了AndroidStudio圖片壓縮工具ImgCompressPlugin使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08