android-Matrix放大缩小图片

package zhang.matrix;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class maxtrix extends Activity {
	private ImageView mImageView;
	private Button mButton1;
	private Button mButton2;
	private RelativeLayout layout1;
	private Bitmap bmp;
	private int id=0;
	private int displayWidth;
	private int displayHeight;
	private float scaleWidth=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);
        //取得屏幕分辨率大小
        DisplayMetrics dm=  new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        displayWidth=dm.widthPixels;
        displayHeight=dm.heightPixels-80;
        bmp=BitmapFactory.decodeResource(getResources(), R.drawable.q2);
        mImageView=(ImageView)findViewById(R.id.imageView1);
        layout1= (RelativeLayout)findViewById(R.id.layout1);
        mButton1= (Button)findViewById(R.id.button1);
        mButton2=(Button)findViewById(R.id.button2);
        
        mButton1.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				small();
			}
        });
        mButton2.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				big();
			}
        });
    }
    public void small(){
    	int bmpWidth=bmp.getWidth();
    	int bmpHeight=bmp.getHeight();
    	double scale =0.8;
    	scaleWidth=(float)(scaleWidth*scale);
    	scaleHeight=(float)(scaleHeight*scale);
    	Matrix matrix = new Matrix();
    	matrix.postScale(scaleWidth, scaleHeight);
    	Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
    	if(id==0){//第一次就删除原来的Imageview
    		layout1.removeView(mImageView);
      	}else{//不是第一次删除上一次产生的ImageView
      		
      		layout1.removeView((ImageView)findViewById(id));
       	}
    	id++;
    	ImageView imageView = new ImageView(maxtrix.this);
    	imageView.setId(id);
    	imageView.setImageBitmap(resizeBmp);
    	layout1.addView(imageView);
    	setContentView(layout1);
    	//图片最大的时候。放大按钮disable,缩小的时候设为enable
    	mButton2.setEnabled(true);
    }
    
    public void big(){
    	int bmpWidth=bmp.getWidth();
    	int bmpHeight=bmp.getHeight();
    	double scale =1.25;
    	scaleWidth=(float)(scaleWidth*scale);
    	scaleHeight=(float)(scaleHeight*scale);
    	Matrix matrix = new Matrix();
    	matrix.postScale(scaleWidth, scaleHeight);
    	Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
    	if(id==0){//第一次就删除原来的Imageview
    		layout1.removeView(mImageView);
      	}else{//不是第一次删除上一次产生的ImageView
      		layout1.removeView((ImageView)findViewById(id));
       	}
    	id++;
    	ImageView imageView = new ImageView(maxtrix.this);
    	imageView.setId(id);
    	imageView.setImageBitmap(resizeBmp);
    	layout1.addView(imageView);
    	setContentView(layout1);
    	//如果在放大图片就超出屏幕,就把Button disable
  if(scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*bmpHeight>displayHeight)
    	mButton2.setEnabled(false);
    	
    	
    }
}



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/layout1"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content" ></ImageView>
<Button android:layout_alignParentBottom="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:id="@+id/button1"></Button>
<Button android:layout_toRightOf="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:id="@+id/button2" android:layout_alignTop="@+id/button1" android:layout_alignBottom="@+id/button1"></Button>
</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值