【问题标题】:merge two images in android. One taken from a picture another from drawable resources. The position is not being proper.在android中合并两个图像。一个取自一张图片,另一个取自可绘制资源。位置不合适。
【发布时间】:2015-12-29 20:40:48
【问题描述】:

我有一个 Imageview 假设 image1。现在有一个名为 image2 的 imageview。图像1>图像2。我想将 image2 拖到 image1 上并使用画布保存新图像。图像已合并,但 image2 未在正确位置合并。任何帮助,将不胜感激。 这是我的合并代码

    int maxWidth = (bitmap1.getWidth() > resizedbitmap2.getWidth() ? bitmap1.getWidth() : resizedbitmap2.getWidth());
        int maxHeight = (bitmap1.getHeight() > resizedbitmap2.getHeight() ? bitmap1.getHeight() : resizedbitmap2.getHeight());
        Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bitmap1, 0f, 0f, null);
        canvas.drawBitmap(resizedbitmap2, x, y, null);

这就是我拖动图像2的方式

    public boolean onTouch(View arg0, MotionEvent arg1) {
   switch (arg1.getAction())
   {
       case MotionEvent.ACTION_DOWN:

             //  attemptClaimDrag();

           moving=true;
           break;
       case MotionEvent.ACTION_MOVE:
           if(moving){
               x=arg1.getRawX()-ima2.getWidth()/2;
               y=arg1.getRawY()-ima2.getHeight()*3/2;
               ima2.setX(x);
               ima2.setY(y);
           }
           break;
       case MotionEvent.ACTION_UP:
           moving=false;
           break;

   }

    return true;
}

定义了这两个

    float x,y=0.0f

【问题讨论】:

    标签: android merge


    【解决方案1】:

    试试这个:

    首先,使用RelativeLayout 重叠两张图片。正确显示。

    其次,将这个RelativeLayout转换成Bitmap

    在 layout.xml 中

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image1"/>
    
        <!-- Change the position of the image. Now, its in center -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image2"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>
    

    在activity.java中,

    // Set enable drawing cache first.
    layout.setDrawingCacheEnabled(true);
    
    
    // Get Bitmap from RelativeLayout
    Bitmap bitmap = layout.getDrawingCache();
    

    【讨论】:

    • 那么,您的意思是我应该将 image1 的 imageview 保留在相对布局中吗?
    • 我不想这样硬编码。用户将在 image1 上拖动 image2 到任何他想要的位置。但是当合并的图像被保存时。我希望它显示在它被丢弃的确切位置。
    猜你喜欢
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多