Java:
package EX04_23.txt;
import android.R.integer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class EX04_23 extends Activity {
private ImageView mImageView;
private Button mButton1;
private Button mButton2;
private Bitmap bmp;
private LinearLayout layout;
private LinearLayout layout1;
private int displayWidth;
private int displayHeight;
private float scaleWidh = 1;
private float scaleHeight = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initApp();
}
private void initApp() {
// TODO Auto-generated method stub
mImageView = (ImageView)findViewById(R.id.myImage);
mButton1 = (Button)findViewById(R.id.myButton1);
mButton2 = (Button)findViewById(R.id.myButton2);
layout = (LinearLayout)findViewById(R.id.myLayout);
layout1 = (LinearLayout)findViewById(R.id.myLayout1);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayHeight = dm.heightPixels - 80;
displayWidth = dm.widthPixels;
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.sun);
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
small();
}
private void small() {
// TODO Auto-generated method stub
int width = bmp.getWidth();
int height = bmp.getHeight();
double scale = 0.8;
scaleHeight = (float)(scaleHeight*scale);
scaleWidh = (float)(scaleWidh*scale);
Matrix mt = new Matrix();
mt.postScale(scaleWidh, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0,
width, height, mt, true);
layout.removeAllViews();
ImageView imageView = new ImageView(EX04_23.this);
imageView.setImageBitmap(resizeBmp);
layout.addView(imageView);
layout.addView(layout1);
setContentView(layout);
mButton2.setEnabled(true);
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
big();
}
private void big() {
// TODO Auto-generated method stub
int height = bmp.getHeight();
int width = bmp.getWidth();
double scale = 1.2;
scaleWidh = (float)(scaleWidh*scale);
scaleHeight = (float)(scaleHeight*scale);
Matrix mt = new Matrix();
mt.postScale(scaleWidh, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, width, height, mt, true);
layout.removeAllViews();
ImageView bigView = new ImageView(EX04_23.this);
bigView.setImageBitmap(resizeBmp);
layout.addView(bigView);
layout.addView(layout1);
setContentView(layout);
if (displayHeight<height*scaleHeight*1.2 | displayWidth<width*scaleWidh*1.2) {
mButton2.setEnabled(false);
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id = "@+id/myImage"
android:layout_width="150dip"
android:layout_height="150dip"
android:background = "@drawable/sun"
/>
<LinearLayout
android:id = "@+id/myLayout1"
android:orientation="horizontal"
android:gravity = "bottom"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<Button
android:id = "@+id/myButton1"
android:layout_marginLeft = "40dip"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text = "缩小"
/>
<Button
android:id = "@+id/myButton2"
android:layout_marginLeft = "80dip"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text = "放大"
/>
</LinearLayout>
</LinearLayout>