如何设置两个图像的分辨率大小相同?

时间:2014-04-17 11:22:23

标签: android

大家好,我正在制作一个非常小的拼贴画,我可以在拼贴中保存图像,但问题是图像的分辨率不同,我想设置相同的分辨率

我使用的代码是:

xml中的

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

    <LinearLayout android:id="@+id/linear2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
     >

    <ImageView
        android:id="@+id/imageView21"
        android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView22"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    </LinearLayout>

    <RelativeLayout android:id="@+id/linear2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="5"
     >

    <Button
        android:id="@+id/savelayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Save" />

    </RelativeLayout>

</LinearLayout>

在Activity类

public class LayoutDisplay2 extends Activity {

    Button save;
    LinearLayout ll;
    ImageView iv1, iv2;
    private static int RESULT_LOAD_IMAGE1 = 1;
    private static int RESULT_LOAD_IMAGE2 = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout2);
        ll = (LinearLayout) findViewById(R.id.linear2);
        save = (Button) findViewById(R.id.savelayout2);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                ll.setDrawingCacheEnabled(true);
                Bitmap bitmap = ll.getDrawingCache();
                // bitmap = Bitmap.createBitmap(480, 800,
                // Bitmap.Config.ARGB_8888);
                String root = Environment.getExternalStorageDirectory()
                        .toString();
                File newDir = new File(root + "/saved_picture");
                newDir.mkdirs();
                Random gen = new Random();
                int n = 10000;
                n = gen.nextInt(n);
                String fotoname = n + ".jpg";
                File file = new File(newDir, fotoname);
                String s = file.getAbsolutePath();
                Log.i("Path of saved image.", s);
                System.err.print("Path of saved image." + s);

                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                    out.flush();
                    Toast.makeText(getApplicationContext(), "Photo Saved",
                            Toast.LENGTH_SHORT).show();
                    out.close();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Photo Saved",
                            Toast.LENGTH_SHORT).show();
                    Log.e("Exception", e.toString());
                }
            }

        });
        iv1 = (ImageView) findViewById(R.id.imageView21);
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE1);
            }
        });
        iv2 = (ImageView) findViewById(R.id.imageView22);
        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent in = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(in, RESULT_LOAD_IMAGE2);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            try {

                iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Toast.makeText(getApplicationContext(), "in second",
                    Toast.LENGTH_SHORT).show();
            Log.i("Second", "in second");
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            try {
                iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}

0 个答案:

没有答案