0

我正在尝试将 1280x800 图像的图像缩放到 1024x600 以使其适合 Galaxy 7 英寸选项卡。我正在使用本教程中的函数 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。我不明白为什么会这样。

任何帮助将不胜感激

用新代码更新后:

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);
    }    
}
4

1 回答 1

0

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

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

看看这个答案。它显示了如何使用 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);
于 2013-01-01T12:37:51.463 回答