在缩放之前和在Android中缩放之后的图像

时间:2013-01-01 12:17:56

标签: android android-layout

我正在尝试将1280x800图像的图像缩放到1024x600以适合Galaxy 7英寸标签。 我正在使用此tutorial.

中的函数draw()

在我的XML文件中,我声明了一个imageView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" 
        />

</RelativeLayout>

在我的java文件中:

public class MainActivity extends Activity {

    TextView text;
    Button mybutton;
    Bitmap bitmap2;
    ImageView iview;
    int maxWidth ;
    int maxHeight ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        Display display = getWindowManager().getDefaultDisplay();
        Point size= new Point();
        (display).getSize(size);
        maxWidth = size.x;
        maxHeight = size.y;

        Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main);
        mybutton=(Button) findViewById(R.id.Send);

        iview=(ImageView)findViewById(R.id.imageView1);

        resizeImage1(bitmap);// set the image
    }

     public void resizeImage1(final Bitmap image)
        {
         float scaleFactor;
         Bitmap resizedImage = null;
            float screenAspect = (float)maxWidth / (float)maxHeight;
            if (screenAspect <= 2)
            {
                scaleFactor = (float)(maxWidth )/ (float)(image.getWidth());

                Toast.makeText(this, "i am here", Toast.LENGTH_LONG).show();
            }
            else
            {
                final int PLAYFIELD_HEIGHT = 10000;
                scaleFactor = (float)maxHeight / (float)PLAYFIELD_HEIGHT;
            }

            int newWidth = (int)(image.getWidth() * scaleFactor);
            int newHeight = (int)(image.getHeight() * scaleFactor);
            int destX = (image.getWidth() - newWidth) / 2;
            int destY = (image.getHeight() - newHeight) / 2;           

            resizedImage = Bitmap.createScaledBitmap( image, newWidth, newHeight, true);
            iview.setMaxHeight(newHeight);
            iview.setMaxWidth(newWidth);

            iview.setImageBitmap(resizedImage);

            iview.setVisibility(1);
        }    
}

现在在调整尺寸之前,当我尝试获取图像的高度和宽度时,它给出了640x400,但实际上图像大小为1280x800。 调整大小后,它会给出模糊图像,因为图像的大小从640x400到1024x600.i无法理解为什么会发生这种情况。

任何帮助将不胜感激

使用新代码更新后:

public class MainActivity extends Activity {

    ImageView iview;
    int maxWidth ;
    int maxHeight ;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        Display display = getWindowManager().getDefaultDisplay();
        Point size= new Point();
        (display).getSize(size);
        maxWidth = size.x;
        maxHeight = size.y;
        File photos= new File("C:/Users/drive3/workspace/images");


        Bitmap bitmap = decodeFile(photos);
        bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
    }

    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);              
            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=40;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale++;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        }
        return super.onOptionsItemSelected(item);
    }    
}

1 个答案:

答案 0 :(得分:0)

如果我没弄错的话,BitmapFactory会自动将图像缩放到目标密度。这就是为什么你得到的图像是640 x 400而不是预期的1280 x 800。

您最好使用BitmapFactory,即指示它直接创建具有所需大小的图像。这将更有效率,您的应用程序将不太可能遇到内存不足问题。

看看this answer。它显示了如何使用BitmapFactory选项。

<强>更新

要防止按BitmapFactory进行默认缩放,您可以像这样更改原始代码。而不是:

Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main);

使用:

BitmapFactory.Options o = new BitmapFactory.Options();
o.inScaled= 0;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.main, o);