Android 圖片縮放與旋轉(zhuǎn)的實現(xiàn)詳解
更新時間:2013年06月19日 09:07:58 作者:
本篇文章是對在Android中實現(xiàn)圖片縮放與旋轉(zhuǎn)的方法進行了詳細(xì)的分析介紹,需要的朋友參考下
本文使用Matrix實現(xiàn)Android實現(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實現(xiàn)圖片縮放與旋轉(zhuǎn)。
* @author Administrator
*
*/
public class MatixActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Android實現(xiàn)圖片縮放與旋轉(zhuǎn)。");
LinearLayout linLayout = new LinearLayout(this);
//加載需要操作的圖片,這里是一張圖片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);
//獲取這個圖片的寬和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
//定義預(yù)轉(zhuǎn)換成的圖片的寬度和高度
int newWidth = 200;
int newHeight = 200;
//計算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 創(chuàng)建操作圖片用的matrix對象
Matrix matrix = new Matrix();
// 縮放圖片動作
matrix.postScale(scaleWidth, scaleHeight);
//旋轉(zhuǎn)圖片 動作
matrix.postRotate(45);
// 創(chuàng)建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
//將上面創(chuàng)建的Bitmap轉(zhuǎn)換成Drawable對象,使得其可以使用在ImageView, ImageButton中
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//創(chuàng)建一個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)地實現(xiàn)圖片縮放,下例中可以通過鼠標(biāo)滑輪和方向鍵實現(xiàn)圖片動態(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實現(xiàn)圖片縮放與旋轉(zhuǎn)。
* @author Administrator
*
*/
public class MatixActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Android實現(xiàn)圖片縮放與旋轉(zhuǎn)。");
LinearLayout linLayout = new LinearLayout(this);
//加載需要操作的圖片,這里是一張圖片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);
//獲取這個圖片的寬和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
//定義預(yù)轉(zhuǎn)換成的圖片的寬度和高度
int newWidth = 200;
int newHeight = 200;
//計算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 創(chuàng)建操作圖片用的matrix對象
Matrix matrix = new Matrix();
// 縮放圖片動作
matrix.postScale(scaleWidth, scaleHeight);
//旋轉(zhuǎn)圖片 動作
matrix.postRotate(45);
// 創(chuàng)建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
//將上面創(chuàng)建的Bitmap轉(zhuǎn)換成Drawable對象,使得其可以使用在ImageView, ImageButton中
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//創(chuàng)建一個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)地實現(xiàn)圖片縮放,下例中可以通過鼠標(biāo)滑輪和方向鍵實現(xiàn)圖片動態(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實現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android中利用matrix 控制圖片的旋轉(zhuǎn)、縮放、移動
- Android Tween動畫之RotateAnimation實現(xiàn)圖片不停旋轉(zhuǎn)效果實例介紹
- Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android實現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android編程中調(diào)用Camera時預(yù)覽畫面有旋轉(zhuǎn)問題的解決方法
- Android開發(fā)之圖形圖像與動畫(二)Animation實現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
- Android編程實現(xiàn)RotateAnimation設(shè)置中心點旋轉(zhuǎn)動畫效果
- Android部分手機拍照后獲取的圖片被旋轉(zhuǎn)問題的解決方法
- android實現(xiàn)icon動態(tài)旋轉(zhuǎn)效果
相關(guān)文章
Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
這篇文章主要為大家詳細(xì)介紹了Android折疊式Toolbar的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android發(fā)送GET與POST請求的DEMO詳解
本篇文章是對Android發(fā)送GET與POST請求的DEMO進行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android中 webView調(diào)用JS出錯的解決辦法
這篇文章主要介紹了Android中 webView調(diào)用JS出錯的解決辦法,需要的朋友可以參考下2015-01-01
Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼
本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實現(xiàn)實例代碼,非常具有實用價值,需要的朋友可以參考下。2017-03-03
Android應(yīng)用中使用SharedPreferences類存儲數(shù)據(jù)的方法
這篇文章主要介紹了Android應(yīng)用中使用SharedPreferences類存儲數(shù)據(jù)的方法,SharedPreferences使用鍵值對應(yīng)的方式進行存儲,使用于少量的數(shù)據(jù)保存,需要的朋友可以參考下2016-04-04
Android實現(xiàn)自動點擊無障礙服務(wù)功能的實例代碼
這篇文章主要介紹了Android實現(xiàn)自動點擊無障礙服務(wù)功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Android 自定義 HorizontalScrollView 打造多圖片OOM 的橫向滑動效果(實例代碼)
這篇文章主要介紹了Android 自定義 HorizontalScrollView 打造多圖片OOM 的橫向滑動效果(實例代碼),需要的朋友可以參考下2017-10-10

