通过搜索栏位图缩放 [英] Scale bitmap through Seekbar

查看:265
本文介绍了通过搜索栏位图缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用搜索栏缩放图像。图像缩放到一个特定的大小,在任何你把搜索栏和缩放您更改搜索栏的图像保持同一尺寸变更进展情况的一次,下一次。我想用增加或搜索栏进步降低动态扩展它。

搜索栏code段

  seekBar.setOnSeekBarChangeListener(新OnSeekBarChangeListener(){        @覆盖
        公共无效onStopTrackingTouch(搜索栏为arg0){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onStartTrackingTouch(搜索栏为arg0){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onProgressChanged(搜索栏搜索栏,INT progresValue,
                布尔FROMUSER){
            // TODO自动生成方法存根
            Log.i(测试,进度值=+ Integer.toString(progresValue));
            Log.i(测试,图像宽度=+ Integer.toString(宽));
            Log.i(测试,图像高度=+ Integer.toString(高度));            scaleImage(图像,宽度,高度);
        }
    });

功能规模图像

 公共无效scaleImage(位图位图,诠释W,INT高){    //获取当前尺寸和所需的边框
    INT边界= dpToPx(150);
    Log.i(测试,原始宽度=+ Integer.toString(W));
    Log.i(测试,原来的高度=+ Integer.toString(H));
    Log.i(测试,边界=+ Integer.toString(边界));    //确定多少扩展:尺寸要求较少的比例为
    //靠近其侧面。通过这种方式,图像始终保持你的内
    //边框,要么X / Y轴接触它。
    浮动xScale等=((浮动)边界)/ W;
    浮动yScale =((浮动)边界)/小时;
    浮规模=(xScale等< = yScale)? xScale等:yScale;
    Log.i(测试,xScale等=+ Float.toString(xScale等));
    Log.i(测试,yScale =+ Float.toString(yScale));
    Log.i(测试,规模=+ Float.toString(规模));    //创建缩放的矩阵,并添加缩放数据
    字模=新的Matrix();
    matrix.postScale(秤,秤);    //创建一个新的位图,并将其转换成由理解的格式
    // ImageView的
    位图scaledBitmap = Bitmap.createBitmap(位图,0,0,W,H,矩阵
            真正);
    sWidth = scaledBitmap.getWidth(); //再利用
    sHeight = scaledBitmap.getHeight(); //再利用
    @燮pressWarnings(德precation)
    BitmapDrawable结果=新BitmapDrawable(scaledBitmap);
    Log.i(测试,缩放宽度=+ Integer.toString(sWidth));
    Log.i(测试,缩放高度=+ Integer.toString(sHeight));
    qrImage.setImageDrawable(结果);}

功能,使边界框

 私人诠释dpToPx(INT DP){
    浮密度= getActivity()。getApplicationContext()。getResources()
            。.getDisplayMetrics()密度;
    返回Math.round((浮点)DP *密度);
}


解决方案

低于code使用搜索栏调整您的图像使用

 公共类MyActivity延伸活动{私有静态最终诠释WIDTH_SCALE_RATIO = 10;
私有静态最终诠释HEIGHT_SCALE_RATIO = 10;
私人诠释previousProcess = 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    最后ImageView的IMG =(ImageView的)findViewById(R.id.img);
    ((搜索栏)findViewById(R.id.seekBar))
            .setOnSeekBarChangeListener(新OnSeekBarChangeListener(){
                @覆盖
                公共无效onStopTrackingTouch(搜索栏为arg0){
                }                @覆盖
                公共无效onStartTrackingTouch(搜索栏为arg0){
                }                @覆盖
                公共无效onProgressChanged(搜索栏搜索栏,
                        INT progresValue,布尔FROMUSER){
                    INT差异= progresValue - previousProcess;
                    scaleImage(IMG,差异);
                    previousProcess = progresValue;
                }
            });
}公共无效scaleImage(ImageView的IMG,int标){
    位图的位图=((BitmapDrawable)img.getDrawable())getBitmap();
    浮宽度= bitmap.getWidth();
    浮高度= bitmap.getHeight();
    宽+ = *规模WIDTH_SCALE_RATIO;
    身高+ = *规模HEIGHT_SCALE_RATIO;
    位= Bitmap.createScaledBitmap(位图,(INT)宽度(INT)的高度,
            真正);
    img.setImageBitmap(位图);
}
}

I am using seekbar to scale the image. The image is scaled to one specific size, where ever you take the seekbar and its scaled for the once, next time you change the progress of seekbar the image remains in same changed size. I want to scale it dynamically with the increase or decrease of seekbar progress.

Seekbar code snippet

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
            Log.i("Test", "Progress value = " + Integer.toString(progresValue));
            Log.i("Test", "Image width = " + Integer.toString(width));
            Log.i("Test", "Image height = " + Integer.toString(height));

            scaleImage(image, width, height);
        }
    });

Function to scale image

public void scaleImage(Bitmap bitmap, int w, int h) {

    // Get current dimensions AND the desired bounding box
    int bounding = dpToPx(150);
    Log.i("Test", "original width = " + Integer.toString(w));
    Log.i("Test", "original height = " + Integer.toString(h));
    Log.i("Test", "bounding = " + Integer.toString(bounding));

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) bounding) / w;
    float yScale = ((float) bounding) / h;
    float scale = (xScale <= yScale) ? xScale : yScale;
    Log.i("Test", "xScale = " + Float.toString(xScale));
    Log.i("Test", "yScale = " + Float.toString(yScale));
    Log.i("Test", "scale = " + Float.toString(scale));

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
            true);
    sWidth = scaledBitmap.getWidth(); // re-use
    sHeight = scaledBitmap.getHeight(); // re-use
    @SuppressWarnings("deprecation")
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    Log.i("Test", "scaled width = " + Integer.toString(sWidth));
    Log.i("Test", "scaled height = " + Integer.toString(sHeight));
    qrImage.setImageDrawable(result);

}

Function to make a bounding box

private int dpToPx(int dp) {
    float density = getActivity().getApplicationContext().getResources()
            .getDisplayMetrics().density;
    return Math.round((float) dp * density);
}

解决方案

use below code for resize your image using seekbar

public class MyActivity extends Activity {

private static final int WIDTH_SCALE_RATIO = 10;
private static final int HEIGHT_SCALE_RATIO = 10;
private int previousProcess = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ImageView img = (ImageView) findViewById(R.id.img);
    ((SeekBar) findViewById(R.id.seekBar))
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progresValue, boolean fromUser) {
                    int diff = progresValue - previousProcess;
                    scaleImage(img, diff);
                    previousProcess = progresValue;
                }
            });
}

public void scaleImage(ImageView img, int scale) {
    Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();
    float width = bitmap.getWidth();
    float height = bitmap.getHeight();
    width += scale * WIDTH_SCALE_RATIO;
    height += scale * HEIGHT_SCALE_RATIO;
    bitmap = Bitmap.createScaledBitmap(bitmap, (int) width, (int) height,
            true);
    img.setImageBitmap(bitmap);
}
}

这篇关于通过搜索栏位图缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆