动态缩放图片(java文件中布局控件(出现ImageView左上角位置改变的问题))

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值