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

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;
}
}
復制代碼 代碼如下:
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)圖片縮放與旋轉。
* @author Administrator
*
*/
public class MatixActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Android實現(xiàn)圖片縮放與旋轉。");
LinearLayout linLayout = new LinearLayout(this);
//加載需要操作的圖片,這里是一張圖片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);
//獲取這個圖片的寬和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
//定義預轉換成的圖片的寬度和高度
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);
//旋轉圖片 動作
matrix.postRotate(45);
// 創(chuàng)建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
//將上面創(chuàng)建的Bitmap轉換成Drawable對象,使得其可以使用在ImageView, ImageButton中
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//創(chuàng)建一個ImageView
ImageView imageView = new ImageView(this);
// 設置ImageView的圖片為上面轉換的圖片
imageView.setImageDrawable(bmd);
//將圖片居中顯示
imageView.setScaleType(ScaleType.CENTER);
//將ImageView添加到布局模板中
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// 設置為本activity的模板
setContentView(linLayout);
}
}
上例是靜態(tài)地實現(xiàn)圖片縮放,下例中可以通過鼠標滑輪和方向鍵實現(xiàn)圖片動態(tài)的放大與縮小。
程序結構如下圖:

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;
}
}
您可能感興趣的文章:
- Android實現(xiàn)屏幕旋轉方法總結
- Android中利用matrix 控制圖片的旋轉、縮放、移動
- Android Tween動畫之RotateAnimation實現(xiàn)圖片不停旋轉效果實例介紹
- Android開發(fā) 旋轉屏幕導致Activity重建解決方法
- Android實現(xiàn)圖片反轉、翻轉、旋轉、放大和縮小
- Android編程中調用Camera時預覽畫面有旋轉問題的解決方法
- Android開發(fā)之圖形圖像與動畫(二)Animation實現(xiàn)圖像的漸變/縮放/位移/旋轉
- Android編程實現(xiàn)RotateAnimation設置中心點旋轉動畫效果
- Android部分手機拍照后獲取的圖片被旋轉問題的解決方法
- android實現(xiàn)icon動態(tài)旋轉效果
相關文章
Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
這篇文章主要為大家詳細介紹了Android折疊式Toolbar的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android發(fā)送GET與POST請求的DEMO詳解
本篇文章是對Android發(fā)送GET與POST請求的DEMO進行了詳細的分析介紹,需要的朋友參考下2013-06-06
Android應用中使用SharedPreferences類存儲數(shù)據(jù)的方法
這篇文章主要介紹了Android應用中使用SharedPreferences類存儲數(shù)據(jù)的方法,SharedPreferences使用鍵值對應的方式進行存儲,使用于少量的數(shù)據(jù)保存,需要的朋友可以參考下2016-04-04
Android實現(xiàn)自動點擊無障礙服務功能的實例代碼
這篇文章主要介紹了Android實現(xiàn)自動點擊無障礙服務功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Android 自定義 HorizontalScrollView 打造多圖片OOM 的橫向滑動效果(實例代碼)
這篇文章主要介紹了Android 自定義 HorizontalScrollView 打造多圖片OOM 的橫向滑動效果(實例代碼),需要的朋友可以參考下2017-10-10

