当前位置: 代码迷 >> Android >> Android 改变图片大小解决方案
  详细解决方案

Android 改变图片大小解决方案

热度:69   发布时间:2016-05-01 21:05:31.0
Android 改变图片大小
Android 如何实现 改变图片大小 功能

------解决方案--------------------
Java code
    /* 相关变量声明 */    private ImageView mImageView;    private Button mButton01;    private Button mButton02;    private FrameLayout layout1;    private LinearLayout layoutImage;    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);        /* 加载display.xml Layout */        setContentView(R.layout.display);                /* 取得屏幕分辨率大小 */        DisplayMetrics dm=new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(dm);        displayWidth=dm.widthPixels;        displayHeight=dm.heightPixels;                 /* 初始化相关变量 */        Bundle bundle = this.getIntent().getExtras();        Integer imageId = bundle.getInt("imageId");        Log.i(TAG, "onCreate, imageId = " + imageId);                               bmp=BitmapFactory.decodeResource(getResources(), imageId);         mImageView = (ImageView)findViewById(R.id.myImageView);        mImageView.setImageBitmap(bmp);        mImageView.setOnTouchListener(this);        mImageView.setLongClickable(true);                layout1 = (FrameLayout)findViewById(R.id.layout1);        layoutImage = (LinearLayout)findViewById(R.id.layoutImage);        mButton01 = (Button)findViewById(R.id.myButton1);        mButton02 = (Button)findViewById(R.id.myButton2);                 /* 缩小按钮onClickListener */        mButton01.setOnClickListener(new Button.OnClickListener() {            @Override            public void onClick(View v) {                small();             }        });                /* 放大按钮onClickListener */        mButton02.setOnClickListener(new Button.OnClickListener() {            @Override                   public void onClick(View v) {                big();            }         });    }    /* 图片缩小的method */    private void small()    {        int bmpWidth=bmp.getWidth();         int bmpHeight=bmp.getHeight();                Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);                /* 设置图片缩小的比例 */        double scale=0.8;        /* 计算出这次要缩小的比例 */         scaleWidth=(float) (scaleWidth*scale);         scaleHeight=(float) (scaleHeight*scale);         /* 产生reSize后的Bitmap对象 */        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,                 bmpHeight,matrix,true);                 if(id==0)      {            /* 如果是第一次按,就删除原来默认的ImageView */            layoutImage.removeView(mImageView);        } else {            /* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */            layoutImage.removeView((ImageView)findViewById(id));        }                 /* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */        id++;        ImageView imageView = new ImageView(this);        imageView.setId(id);        imageView.setImageBitmap(resizeBmp);        layoutImage.addView(imageView);        Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()                + ", imageView.getHeight() = " + imageView.getHeight());        setContentView(layout1);        /* 因为图片放到最大时放大按钮会disable,所以在缩小时把它重设为enable */         mButton02.setEnabled(true);        mButton02.setTextColor(Color.MAGENTA);    }        /* 图片放大的method */    private void big() {        int bmpWidth=bmp.getWidth();        int bmpHeight=bmp.getHeight();                Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);                /* 设置图片放大的比例 */        double scale=1.25;        /* 计算这次要放大的比例 */        scaleWidth=(float)(scaleWidth*scale);        scaleHeight=(float)(scaleHeight*scale);        /* 产生reSize后的Bitmap对象 */        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,                 bmpHeight,matrix,true);                if(id==0) {            /* 如果是第一次按,就删除原来设置的ImageView */            layoutImage.removeView(mImageView);        } else {            /* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */             layoutImage.removeView((ImageView)findViewById(id));        }                /* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */        id++;        ImageView imageView = new ImageView(this);        imageView.setId(id);        imageView.setImageBitmap(resizeBmp);        layoutImage.addView(imageView);        setContentView(layout1);        /* 如果再放大会超过屏幕大小,就把Button disable */        if( scaleWidth * scale * bmpWidth > bmpWidth * 3 ||            scaleHeight * scale * bmpHeight > bmpWidth * 3 ||            scaleWidth * scale * bmpWidth > displayWidth * 5 ||            scaleHeight * scale * bmpHeight > displayHeight * 5) {                mButton02.setEnabled(false);                mButton02.setTextColor(Color.GRAY);            } else {                mButton02.setEnabled(true);                mButton02.setTextColor(Color.MAGENTA);            }        }
  相关解决方案