将叠加图像放置在画布上

时间:2016-07-12 09:45:18

标签: android imageview android-canvas android-bitmap

你好我正在开发一个Android应用程序,它从图库中获取一个图像并在其上添加水印。它具有以下特点:

  • 我在manifest.xml
  • 中手动将屏幕锁定为纵向模式
  • 用户可以将水印拖动到屏幕上的任何所需位置
  • 水印图像正在添加到用户的图像上
  • 用户可以使用所需的水印保存图像

水印xml

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

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:id="@+id/root">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/pic_holder">


        </FrameLayout>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/watermark"
            />

    </RelativeLayout>

</RelativeLayout>

ImageView已添加为

    image_watermark=(ImageView)v.findViewById(R.id.watermark);
    mRrootLayout = (ViewGroup) v.findViewById(R.id.root);

    mRrootLayout.setDrawingCacheEnabled(true);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(300, 300);

    image_watermark.setLayoutParams(layoutParams);
    image_watermark.setImageResource(R.drawable.cat8);
    image_watermark.setOnTouchListener(this);

    //the R.drawable.cat8 is a sample image i'm using
    //The drawable width and height would be 600*600 pixels all the time as I am using this same size for all imageview water mark drawables

可以将imageview水印拖动为

 @Override
public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            xloc=X;
            yloc=Y;
            //to check the location
            Toast.makeText(getContext(), "Location==="+X+"==="+Y, Toast.LENGTH_SHORT).show();
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            view.setLayoutParams(layoutParams);
            break;
    }
    mRrootLayout.invalidate();
    return true;
}

这种方法运行正常(从我这边起,对我来说效果很好)

水印在图像上添加为

//get the image from the user
//may be of any width and height any resolution
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

 //add the water mark to the camera photo bitmap here
 Bitmap mutableBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, true);

 image_watermark.buildDrawingCache();
 Bitmap bmap = image_watermark.getDrawingCache();

 Paint pai=new Paint();
 pai.setAntiAlias(true);

 //get the location of image view water mark on screen
 int[] posXY = new int[2];
 image_watermark.getLocationOnScreen(posXY)
 xloc = posXY[0];
 yloc = posXY[1];

 //create the image view water mark on the image got by user
 // problem is here
 //the water mark is placed not accurately onto the target location where the user drops it
 //it happens differently on multiple devices       
 Bitmap final_image=Bitmap.createScaledBitmap(bmap,
                        image_watermark.getMeasuredWidth()*5,
                        image_watermark.getMeasuredHeight()*5,false);

 Canvas canvas = new Canvas(mutableBitmap);
 canvas.drawBitmap(final_image,((image_watermark.getWidth()*2)+xloc),(yloc), pai);

 //save the mutable image as so far

问题

问题是水印图像未位于用户设定的目标上

实施例: 这是用户加载的图像上图像视图的初始位置: enter image description here

这是用户保存应用水印的图像后的结果图像: enter image description here

0 个答案:

没有答案