调整位图图像? [英] Resize Bitmap image?

查看:162
本文介绍了调整位图图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变屏幕方向时,改变图像大小。我试着用下面的code,但它不能正常工作。

  @覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
    如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(这一点,肖像,Toast.LENGTH_SHORT).show();
    }否则如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            MYIMAGE = Bitmap.createBitmap(BitmapFactory
                    由Matchi.com提供回到codeResource(this.getResources(),R.drawable.pic));
            显示D =((窗口管理器)getSystemService(WINDOW_SERVICE))
                    .getDefaultDisplay();            INT ScreenHeight = d.getHeight();
            INT屏幕宽度= d.getWidth();            位图ScaledImage = Bitmap.createScaledBitmap(MYIMAGE,屏幕宽度,ScreenHeight,
                             真正);            ImageView的=(ImageView的)findViewById(R.id.imageView2);
            imageview.setImageBitmap(ScaledImage);
    }
}


解决方案

下面是code旋转或android系统重新尺寸图像

 公共类bitmaptest延伸活动{
    @覆盖
    公共无效的onCreate(捆绑冰柱){
        super.onCreate(冰柱);
        的LinearLayout linLayout =新的LinearLayout(本);        //加载origial位图(500×500像素)
        位图bitmapOrg = BitmapFactory.de codeResource(getResources()
               R.drawable.android);        INT宽度= bitmapOrg.width();
        INT高度= bitmapOrg.height();
        INT newWidth = 200;
        INT newHeight = 200;        //计算比例 - 在这种情况下= 0.4f
        浮动scaleWidth =((浮点)newWidth)/宽度;
        浮动scaleHeight =((浮点)newHeight)/身高;        //用于操作createa矩阵
        字模=新的Matrix();
        //调整位图
        matrix.postScale(scaleWidth,scaleHeight);
        //旋转位图
        matrix.postRotate(45);        //重新创建新的位图
        位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,
                          宽度,高度,矩阵,真);        //使从位图可绘制允许设置位图
        //为ImageView的,ImageButton的或什么都
        BitmapDrawable BMD =新BitmapDrawable(resizedBitmap);        ImageView的ImageView的=新ImageView的(本);        //设置绘制对象上的ImageView
        imageView.setImageDrawable(BMD);        //中心图像
        imageView.setScaleType(ScaleType.CENTER);        //添加的ImageView的布局
        linLayout.addView(ImageView的,
                新LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT
                )
        );        //设置的LinearLayout作为内容查看
        的setContentView(linLayout);
    }
}

I want to change the image size when changing screen orientation. I tried to use the following code, but it does not work.

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            myImage = Bitmap.createBitmap(BitmapFactory
                    .decodeResource(this.getResources(), R.drawable.pic));
            Display d = ((WindowManager)getSystemService(WINDOW_SERVICE))
                    .getDefaultDisplay();

            int ScreenHeight = d.getHeight();
            int ScreenWidth = d.getWidth();

            Bitmap ScaledImage = Bitmap.createScaledBitmap(myImage , ScreenWidth, ScreenHeight, 
                             true);

            imageview = (ImageView)findViewById(R.id.imageView2);
            imageview.setImageBitmap(ScaledImage);
    }
}

解决方案

Below is the code to rotate or re size your image in android

public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}

这篇关于调整位图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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