如何将图像组合成单个画布Android

时间:2014-02-19 05:32:23

标签: android image android-canvas

我在画布中有这个多个图像,如何在画布中获取每个图像的锚点,并能够在画布上拖动,调整大小和移动它。就像在其他图像编辑器Android应用程序中一样。请帮我。谢谢

这是代码:

public class MainActivity extends Activity implements OnClickListener {

        static final int PICKED_ONE = 0;
        static final int PICKED_TWO = 1;

        boolean onePicked = false;
        boolean twoPicked = false;

        Button choosePicture1, choosePicture2;
        ImageView compositeImageView;

        Bitmap bmp1, bmp2;
        Bitmap returnBmp;
        Bitmap drawingBitmap;
        Canvas canvas;
        Paint paint;

        protected static final String TAG = MainActivity.class.getName();
        Bitmap mBackImage, mTopImage, mBackground, mInnerImage, mNewSaving;
        Canvas mComboImage;
        FileOutputStream mFileOutputStream;
        BitmapDrawable mBitmapDrawable;
        private String mCurrent = null;
        private static String mTempDir;
        Button save;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            save = (Button)findViewById(R.id.save);

            mTempDir = Environment.getExternalStorageDirectory() + "/" + "Pixiedoo" + "/";
            mCurrent = "Aura.png";
            prepareDirectory();

            compositeImageView = (ImageView) this
                    .findViewById(R.id.CompositeImageView);

            choosePicture1 = (Button) this.findViewById(R.id.ChoosePictureButton1);
            choosePicture2 = (Button) this.findViewById(R.id.ChoosePictureButton2);

            choosePicture1.setOnClickListener(this);
            choosePicture2.setOnClickListener(this);

            save.setOnClickListener(new View.OnClickListener() {
                @SuppressWarnings("deprecation")
                public void onClick(View v) {
                  Log.v(TAG, "Save Tab Clicked");
                  try {
                    mBitmapDrawable = new BitmapDrawable(drawingBitmap);
                    mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();
                    String FtoSave = mTempDir + mCurrent;
                    File mFile = new File(FtoSave);
                    mFileOutputStream = new FileOutputStream(mFile);
                    mNewSaving.compress(CompressFormat.PNG, 95, mFileOutputStream);
                    mFileOutputStream.flush();
                    mFileOutputStream.close();
                  } catch (FileNotFoundException e) {
                    Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
                  } catch (IOException e) {
                    Log.v(TAG, "IOExceptionError " + e.toString());
                  }
                }
              });
        }
        private boolean prepareDirectory() {
            try {
              if (makeDirectory()) {
                return true;
              } else {
                return false;
              }
            } catch (Exception e) {
              e.printStackTrace();
              //Toast.makeText(this, getString(R.string.sdcard_error), 1000).show();
              return false;
            }
          }

        private boolean makeDirectory() {
            File mTempFile = new File(mTempDir);
            if (!mTempFile.exists()) {
              mTempFile.mkdirs();
            }

            if (mTempFile.isDirectory()) {
              File[] mFiles = mTempFile.listFiles();
              for (File mEveryFile : mFiles) {
                if (!mEveryFile.delete()) {
                  //System.out.println(getString(R.string.failed_to_delete) + mEveryFile);
                }
              }
            }
            return (mTempFile.isDirectory());
          }
        public void onClick(View v) {

            int which = -1;

            if (v == choosePicture1) {
                which = PICKED_ONE;
            } else if (v == choosePicture2) {
                which = PICKED_TWO;
            }

            Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(choosePictureIntent, which);
        }

        protected void onActivityResult(int requestCode, int resultCode,
                Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);

            if (resultCode == RESULT_OK) {
                Uri imageFileUri = intent.getData();

                if (requestCode == PICKED_ONE) {
                    bmp1 = loadBitmap(imageFileUri);
                    onePicked = true;
                } else if (requestCode == PICKED_TWO) {
                    bmp2 = loadBitmap(imageFileUri);
                    twoPicked = true;
                }

                if (onePicked && twoPicked) {
                    drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(),
                            bmp1.getHeight(), bmp1.getConfig());
                    canvas = new Canvas(drawingBitmap);
                    paint = new Paint();
                    canvas.drawBitmap(bmp1, 90, 0, paint);
                //  paint.setXfermode(new PorterDuffXfermode(
                        //  android.graphics.PorterDuff.Mode.MULTIPLY));
                    canvas.drawBitmap(bmp2, 30, 40, paint);


                     compositeImageView.setImageBitmap(drawingBitmap);


                }
            }
        }

        private Bitmap loadBitmap(Uri imageFileUri) {
            Display currentDisplay = getWindowManager().getDefaultDisplay();

            float dw = currentDisplay.getWidth();
            float dh = currentDisplay.getHeight();

            returnBmp = Bitmap.createBitmap((int) dw, (int) dh,
                    Bitmap.Config.ARGB_8888);


            try {
                // Load up the image's dimensions not the image itself
                BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                bmpFactoryOptions.inJustDecodeBounds = true;
                returnBmp = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(imageFileUri), null, bmpFactoryOptions);

                int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / dh);
                int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / dw);

                Log.v("HEIGHTRATIO", "" + heightRatio);
                Log.v("WIDTHRATIO", "" + widthRatio);

                // If both of the ratios are greater than 1, one of the sides of the
                // image is greater than the screen
                if (heightRatio > 1 && widthRatio > 1) {
                    if (heightRatio > widthRatio) {
                        // Height ratio is larger, scale according to it
                        bmpFactoryOptions.inSampleSize = heightRatio;
                    } else {
                        // Width ratio is larger, scale according to it
                        bmpFactoryOptions.inSampleSize = widthRatio;
                    }
                }

                // Decode it for real
                bmpFactoryOptions.inJustDecodeBounds = false;
                returnBmp = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(imageFileUri), null, bmpFactoryOptions);
            } catch (FileNotFoundException e) {
                Log.v("ERROR", e.toString());
            }

            return returnBmp;
        }
    }

0 个答案:

没有答案
相关问题