如何使用seekbar调整图像大小?

时间:2014-07-25 09:56:02

标签: android bitmap image-resizing android-seekbar

我想使用搜索栏调整图像大小。 Seekbar正在工作,但它没有给出任何好结果。使用以下代码,图像会调整大小,但只是在您第一次更改搜索条的进度时。请帮忙

Seekbar听众

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progresValue,
                boolean fromUser) {
            // TODO Auto-generated method stub
            progresValue = progress;
            resizeImage(image, progresValue);
        }
    });

调整图像大小的功能

public void resizeImage(Bitmap bitmap, int progress) {
    Bitmap bitmapSource = bitmap;
    float bHeight = bitmapSource.getHeight();
    float bWidth = bitmapSource.getWidth();
    float factorH = progress / (float) bHeight;
    float factorW = progress / (float) bWidth;
    float factorToUse = (factorH > factorW) ? factorW : factorH;
    Bitmap bm = Bitmap.createScaledBitmap(bitmapSource,
            (int)(bWidth * factorToUse), (int)(bHeight * factorToUse),
            false);
    qImage.setImageBitmap(bm);
}

1 个答案:

答案 0 :(得分:0)

 final SeekBar sk1 = (SeekBar) findViewById(R.id.seekBar1);
                sk1.setMax(500);
                 sk1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

                                public void onStopTrackingTouch(SeekBar seekBar) {      
                                    // TODO Auto-generated method stub      
                                }       

                                public void onStartTrackingTouch(SeekBar seekBar) {     
                                    // TODO Auto-generated method stub  

                                }       

                                public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
                                    // TODO Auto-generated method stub      



                     img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(progress,progress);
                      img.setLayoutParams(params);

                      img.setMinimumWidth(progress);
                      img.setMinimumHeight(progress);






                                }       
                            });