如何将图像放在另一个图像视图(已经是设置图像)上并将其保存为合并图像?

时间:2016-02-19 12:04:30

标签: android image bitmap

如何将图像放在另一个图像视图上(已经是设置图像)并将其保存为合并图像?

MoveBitmap类下面移动位图但没有任何返回我只想返回一个位图

  

我想实现   :在图像上放置一个图像(已经在imageview中设置)然后想要制作可移动的内部图像(第二个图像)并将它们保存为组合图像.....我很困惑如何实现这一点....任何人都可以告诉我 ?

移动位图的类

    package "name";


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MoveBitmap extends Activity {

    public class TouchView extends View {

        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private float x, y;
        boolean touching = false;

        Bitmap bm = BitmapFactory.decodeResource(
                getResources(),
                R.drawable.n);
        int bm_x = 0;
        int bm_y = 0;
        int bm_w = bm.getWidth();
        int bm_h = bm.getHeight();
        int bm_offsetx;
        int bm_offsety;
        boolean dm_touched = false;


        public TouchView(Context context) {

            super(context);
            // TODO Auto-generated constructor stub

        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            paint.setColor(Color.WHITE);

            if(touching){
                canvas.drawRect(x, y, x+bm_w, y+bm_h, paint);
            }

            canvas.drawBitmap(bm, bm_x, bm_y, paint);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
                    MeasureSpec.getSize(heightMeasureSpec));
        }

        @Override

        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            int action = event.getAction();
            switch(action){
                case MotionEvent.ACTION_MOVE:
                    x = event.getX();
                    y = event.getY();
                    touching = true;

                    if(dm_touched){
                        bm_x = (int)x - bm_offsetx;
                        bm_y = (int)y - bm_offsety;
                    }

                    break;
                case MotionEvent.ACTION_DOWN:
                    x = event.getX();
                    y = event.getY();
                    touching = true;

                    //check if bm touched
                    if((x > bm_x)
                            && (x < bm_x+bm_w)
                            && (y > bm_y)
                            && (y < bm_y+bm_h)){
                        bm_offsetx = (int)x - bm_x;
                        bm_offsety = (int)y - bm_y;
                        dm_touched = true;
                    }

                    break;
                case MotionEvent.ACTION_UP:
                default:
                    dm_touched = false;
                    touching = false;
            }

            invalidate();
            return true;
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TouchView(this));

    }
}

2 个答案:

答案 0 :(得分:1)

您可以在此方法中使用drawing cache from view

public static Bitmap loadBitmapFromView(View v) {
    Bitmap bitmap;
    v.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
}

但是你需要有两个ImageView的布局(例如,Frame Layout)。首先是背景(初始图像),第二个是可移动图像。然后以您需要的方式放置视图,然后使用FrameLayout作为视图使用上面的函数制作屏幕截图。

答案 1 :(得分:1)

public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
            view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}