图片查看程序,基本缩放,滚动查看功能都是行的。

\" data-mce-src=

核心代码:

package snowfox.android;
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.util.Log;

import android.view.KeyEvent;

import android.widget.ImageView;
public class MainActivity extends Activity {

/** Called when the activity is first created. */

private ImageView v1;

private int widthOrg, heightOrg, w;

float scale = 1;

float scaleWidth, scaleHeight;

Bitmap OrgMap, newMap;

BitmapDrawable bmd;

Matrix matrix = new Matrix();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

v1 = (ImageView)findViewById(R.id.image01);

OrgMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);

widthOrg = OrgMap.getWidth();

heightOrg = OrgMap.getHeight();

//确定图片初始缩放比例,一般以适应屏幕宽度为准

w = 320;

scale = (float)w/widthOrg;



matrix = new Matrix();

matrix.postScale(scale, scale);

newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);



bmd = new BitmapDrawable(newMap);

v1.setImageDrawable(bmd);

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

{

//放大图片

if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)

{

//设置放大尺寸,若大于2倍,则停止放大,另外,小于1200是为了防止因图片太大造成内存泄露

if(scale < 2.0 && w < 1200)

{

scale += 0.1;

matrix.reset(); //重置矩阵

matrix.postScale(scale, scale); //设置矩阵属性

//重新绘图

newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);



//转化为drawable图像,使其可以显示在imageview中

bmd = new BitmapDrawable(newMap);

v1.setImageDrawable(bmd);

w = newMap.getWidth();

int h = newMap.getHeight();

Log.i("ta==========", w + " " +h);

}

return true;

}


//缩小图片

if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)

{

//设置缩放尺寸,若小于0.1倍,则停止缩小

if(scale > 0.1)

{

scale -= 0.05;



matrix.reset(); //重置矩阵

matrix.postScale(scale, scale); //设置矩阵属性

//重新绘图

newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);



//转化为drawable图像,使其可以显示在imageview中

bmd = new BitmapDrawable(newMap);

v1.setImageDrawable(bmd);



w = newMap.getWidth();

int h = newMap.getHeight();

Log.i("ta==========", w + " " +h);

}

return true;

}

else

return super.onKeyDown(keyCode, event); //在其他情况下才调用super是为了屏蔽音量按键原来的功能,同时保证其他按键原功能可用

}
}

 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent" android:layout_height="fill_parent"
android:fillViewport
="true">
<HorizontalScrollView
android:layout_width = "fill_parent"
android:layout_height
= "fill_parent"

android:fillViewport
= "true">

<LinearLayout
android:gravity ="center"

android:orientation
="horizontal"
android:layout_width
="fill_parent" android:layout_height="fill_parent">
<ImageView

android:id = "@+id/image01"

android:layout_width
="wrap_content"
android:layout_height
="wrap_content"

android:scaleType
="fitCenter"/>

</LinearLayout>
</HorizontalScrollView>

</ScrollView>

转自:http://www.eoeandroid.com/code/2012/0221/833.html