java文件:
package EX04_23.txt;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);
}
}

});
}
}这是我的布局:<?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>
本来我需要的效果是;无论放大还是缩小图片,ImageView都应该在左上角,可是现在只要一开始放大或者缩小图片,ImageView就会跑到正中间,然后一直处于这个位置。
麻烦大神帮帮解决一下,谢谢!
初始状态:

解决方案 »

  1.   


    <?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"
       >
    <LinearLayout
     android:id = "@+id/myLayout0"  
      android:orientation="horizontal"
       android:gravity = "top|left"
       android:layout_gravity="top|left"
       android:layout_weight="1"
       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>
    <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>