android开发实例05:新浪微博图片缩放实现
2011-12-15 22:58
781 查看
以前程序中用到了图片缩放,按照新浪微博效果写了一个(点击图片出现缩放按钮,过一会消失)。分享给大家,代码整理过,可以直接用!
![](http://img1.51cto.com/attachment/201112/225441357.png)
package com.Johnson.image.zoom;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private final int LOADING_IMAGE = 1;
public static String KEY_IMAGEURI = "ImageUri";
private ZoomControls zoom;
private ImageView mImageView;
private LinearLayout layoutImage;
private int displayWidth;
private int displayHeight;
/**图片资源*/
private Bitmap bmp;
/**宽的缩放比例*/
private float scaleWidth = 1;
/**高的缩放比例*/
private float scaleHeight = 1;
/**用来计数放大+1 缩小-1*/
private int zoomNumber=0;
/**点击屏幕显示缩放按钮,三秒消失*/
private int showTime=3000;
RelativeLayout rl;
Handler mHandler = new Handler();
private Runnable task = new Runnable() {
public void run() {
zoom.setVisibility(View.INVISIBLE);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//showDialog(LOADING_IMAGE);
//图片是从网络上获取的话,需要加入滚动条
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.image);
//removeDialog(LOADING_IMAGE);
initZoom();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case LOADING_IMAGE: {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}
});
dialog.setMessage("正在加载图片请稍后...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
public void initZoom() {
/* 取得屏幕分辨率大小 */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels;
mImageView = (ImageView) findViewById(R.id.myImageView);
mImageView.setImageBitmap(bmp);
layoutImage = (LinearLayout) findViewById(R.id.layoutImage);
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/**
* 在图片上和整个view上同时添加点击监听捕捉屏幕
* 点击事件,来显示放大缩小按钮
* */
zoom.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(task);
mHandler.postDelayed(task, showTime);
}
});
layoutImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoom.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(task);
mHandler.postDelayed(task, showTime);
}
});
zoom = (ZoomControls) findViewById(R.id.zoomcontrol);
zoom.setIsZoomInEnabled(true);
zoom.setIsZoomOutEnabled(true);
// 图片放大
zoom.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
big();
}
});
// 图片减小
zoom.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
small();
}
});
zoom.setVisibility(View.VISIBLE);
mHandler.postDelayed(task, showTime);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
/**
* 在图片上和整个view上同时添加点击监听捕捉屏幕
* 点击事件,来显示放大缩小按钮
* */
zoom.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(task);
mHandler.postDelayed(task, showTime);
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyDown(keyCode, event);
return true;
}
/* 图片缩小的method */
private void small() {
--zoomNumber;
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
Log.i("","bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
/* 设置图片缩小的比例 */
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);
mImageView.setImageBitmap(resizeBmp);
/* 限制缩小尺寸 */
if ((scaleWidth * scale * bmpWidth < bmpWidth / 4
|| scaleHeight * scale * bmpHeight > bmpWidth /4
|| scaleWidth * scale * bmpWidth > displayWidth / 5
|| scaleHeight * scale * bmpHeight > displayHeight / 5)&&(zoomNumber==-1) ){
zoom.setIsZoomOutEnabled(false);
} else {
zoom.setIsZoomOutEnabled(true);
}
zoom.setIsZoomInEnabled(true);
System.gc();
}
/* 图片放大的method */
private void big() {
++zoomNumber;
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
/* 设置图片放大的比例 */
double scale = 1.25;
/* 计算这次要放大的比例 */
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);
mImageView.setImageBitmap(resizeBmp);
/* 限制放大尺寸 */
if (scaleWidth * scale * bmpWidth > bmpWidth * 4
|| scaleHeight * scale * bmpHeight > bmpWidth * 4
|| scaleWidth * scale * bmpWidth > displayWidth * 5
|| scaleHeight * scale * bmpHeight > displayHeight * 5) {
zoom.setIsZoomInEnabled(false);
} else {
zoom.setIsZoomInEnabled(true);
}
zoom.setIsZoomOutEnabled(true);
System.gc();
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rl"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="19"
android:scrollbars="none"
android:fadingEdge="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<HorizontalScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:scrollbars="none"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/hs"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layoutImage"
android:layout_gravity="center"
android:gravity="center"
>
<ImageView
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/myImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="19"
android:paddingTop="5dip"
android:paddingBottom="5dip"
/>
</LinearLayout>
</HorizontalScrollView >
</ScrollView>
<ZoomControls android:id="@+id/zoomcontrol"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
</ZoomControls>
</RelativeLayout>
</FrameLayout>
本文出自 “Johnson” 博客,请务必保留此出处http://johnson008.blog.51cto.com/4000361/743048
![](http://img1.51cto.com/attachment/201112/225441357.png)
package com.Johnson.image.zoom;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private final int LOADING_IMAGE = 1;
public static String KEY_IMAGEURI = "ImageUri";
private ZoomControls zoom;
private ImageView mImageView;
private LinearLayout layoutImage;
private int displayWidth;
private int displayHeight;
/**图片资源*/
private Bitmap bmp;
/**宽的缩放比例*/
private float scaleWidth = 1;
/**高的缩放比例*/
private float scaleHeight = 1;
/**用来计数放大+1 缩小-1*/
private int zoomNumber=0;
/**点击屏幕显示缩放按钮,三秒消失*/
private int showTime=3000;
RelativeLayout rl;
Handler mHandler = new Handler();
private Runnable task = new Runnable() {
public void run() {
zoom.setVisibility(View.INVISIBLE);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//showDialog(LOADING_IMAGE);
//图片是从网络上获取的话,需要加入滚动条
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.image);
//removeDialog(LOADING_IMAGE);
initZoom();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case LOADING_IMAGE: {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}
});
dialog.setMessage("正在加载图片请稍后...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
public void initZoom() {
/* 取得屏幕分辨率大小 */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels;
mImageView = (ImageView) findViewById(R.id.myImageView);
mImageView.setImageBitmap(bmp);
layoutImage = (LinearLayout) findViewById(R.id.layoutImage);
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/**
* 在图片上和整个view上同时添加点击监听捕捉屏幕
* 点击事件,来显示放大缩小按钮
* */
zoom.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(task);
mHandler.postDelayed(task, showTime);
}
});
layoutImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoom.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(task);
mHandler.postDelayed(task, showTime);
}
});
zoom = (ZoomControls) findViewById(R.id.zoomcontrol);
zoom.setIsZoomInEnabled(true);
zoom.setIsZoomOutEnabled(true);
// 图片放大
zoom.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
big();
}
});
// 图片减小
zoom.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
small();
}
});
zoom.setVisibility(View.VISIBLE);
mHandler.postDelayed(task, showTime);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
/**
* 在图片上和整个view上同时添加点击监听捕捉屏幕
* 点击事件,来显示放大缩小按钮
* */
zoom.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(task);
mHandler.postDelayed(task, showTime);
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyDown(keyCode, event);
return true;
}
/* 图片缩小的method */
private void small() {
--zoomNumber;
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
Log.i("","bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
/* 设置图片缩小的比例 */
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);
mImageView.setImageBitmap(resizeBmp);
/* 限制缩小尺寸 */
if ((scaleWidth * scale * bmpWidth < bmpWidth / 4
|| scaleHeight * scale * bmpHeight > bmpWidth /4
|| scaleWidth * scale * bmpWidth > displayWidth / 5
|| scaleHeight * scale * bmpHeight > displayHeight / 5)&&(zoomNumber==-1) ){
zoom.setIsZoomOutEnabled(false);
} else {
zoom.setIsZoomOutEnabled(true);
}
zoom.setIsZoomInEnabled(true);
System.gc();
}
/* 图片放大的method */
private void big() {
++zoomNumber;
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
/* 设置图片放大的比例 */
double scale = 1.25;
/* 计算这次要放大的比例 */
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);
mImageView.setImageBitmap(resizeBmp);
/* 限制放大尺寸 */
if (scaleWidth * scale * bmpWidth > bmpWidth * 4
|| scaleHeight * scale * bmpHeight > bmpWidth * 4
|| scaleWidth * scale * bmpWidth > displayWidth * 5
|| scaleHeight * scale * bmpHeight > displayHeight * 5) {
zoom.setIsZoomInEnabled(false);
} else {
zoom.setIsZoomInEnabled(true);
}
zoom.setIsZoomOutEnabled(true);
System.gc();
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rl"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="19"
android:scrollbars="none"
android:fadingEdge="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<HorizontalScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:scrollbars="none"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/hs"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layoutImage"
android:layout_gravity="center"
android:gravity="center"
>
<ImageView
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/myImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="19"
android:paddingTop="5dip"
android:paddingBottom="5dip"
/>
</LinearLayout>
</HorizontalScrollView >
</ScrollView>
<ZoomControls android:id="@+id/zoomcontrol"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
</ZoomControls>
</RelativeLayout>
</FrameLayout>
本文出自 “Johnson” 博客,请务必保留此出处http://johnson008.blog.51cto.com/4000361/743048
相关文章推荐
- android开发-图片缩放,拖动功能实现。
- Android开发之ImageView通过matrix实现两点缩放和图片拖动
- Android开发实现高仿优酷的客户端图片左右滑动切换功能实例【附源码下载】
- [Android] PorterDuff使用实例----实现新浪微博图片下载效果
- [Android] PorterDuff使用实例----实现新浪微博图片下载效果
- [Android] PorterDuff使用实例----实现新浪微博图片下载效果
- Android实现多点触控,自由缩放图片的实例代码
- Android 自定义imageview实现图片缩放实例详解
- [Android] PorterDuff使用实例----实现新浪微博图片下载效果
- Android开发实现图片平移、缩放、倒影及旋转功能的方法
- Android游戏开发:实现手势操作切换图片的实例
- 【前端开发实例】JS CSS - 鼠标滚轮实现图片缩放 -(五)
- Android开发之--Android实现对图片的缩放、剪切、旋转、存储
- Android开发之imageView图片按比例缩放的实现方法
- 【Android开发小记--9】触摸事件---实现双指缩放图片
- 【Android 开发】:UI控件之 ImageView 实现图片旋转和缩放功能
- Android 开发实例:图片拖动的实现
- Android 蓝牙开发实例--蓝牙聊天程序的设计和实现
- android实现图片缩放 旋转的几种方法