0

Galaxy 7インチのタブに収まるように、1280x800の画像を1024x600に拡大しようとしています。このチュートリアルの関数 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画像はターゲット密度に自動的にスケーリングされます。そのため、予想される 1280 x 800 ではなく 640 x 400 の画像が得られます。

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 に答える