如何在另一张图片上添加图片

时间:2014-04-24 12:44:46

标签: android

应用程序已经正在拍摄并保存图片。拍摄完图像后,我必须在该图片上添加一个箭头,并且箭头必须放在用户按下的位置。

我没有提供代码,因为我还没有开始使用此功能,因为我不知道从哪里开始。任何帮助将非常感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

int touchX; // Get user touch 
int touchY; // Get user touch 

Bitmap bitmap = null;
    try {
        Bitmap bmpPhoto = BitmapFactory.decodeResource(res, R.drawable.photo); //photo

        Bitmap bmpArrow = BitmapFactory.decodeResource(res, R.drawable.arrow); //arrow


        bitmap = Bitmap.createBitmap(bmpPhoto.getWidth(), bmPhoto.getHeight(), Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        Resources res = getResources();

        Drawable drawable1 = new BitmapDrawable(bmpPhoto);
        Drawable drawable2 = new BitmapDrawable(bmpArrow);

        drawable1.setBounds(0, 0, bmpPhoto.getWidth(), bmpPhoto.getheight());
        drawable2.setBounds(touchX, touchY, touchX + bmpArrow.getWidth(), touchY + bmpArrow.getHeight());
        drawable1.draw(c);
        drawable2.draw(c);


    } catch (Exception e) {
    }
    return bitmap;

答案 1 :(得分:0)

我正在研究完全相同的功能!

这是我的代码:

    public class ImageEditActivity extends Activity {

        public String reportDate;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.image_edit_activity);

            Intent intent = getIntent();
            reportDate = intent.getStringExtra("reportDate");


            int w=getWindowManager().getDefaultDisplay().getWidth()-25;
            int h=getWindowManager().getDefaultDisplay().getHeight()-25;

            BallView ballView=new BallView(this,w,h, reportDate);
            setContentView(ballView);
        }
    }


    class BallView extends SurfaceView implements SurfaceHolder.Callback {
        private Bitmap bitmap ;
        private int x=20,y=20;int width,height;
        String reportDate1;

        public BallView(Context context,int w,int h, String reportDate) {
            super(context);

            reportDate1 = reportDate;
            width=w;
            height=h;
            getHolder().addCallback(this);
            setFocusable(true);
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/VibrationMeasurement/" + reportDate1 + ".machine" + ".jpg";  

            Bitmap bitmap2 = BitmapFactory.decodeFile(path);

            Bitmap scaled = Bitmap.createScaledBitmap(bitmap2, width, height, true);


            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.crosshair);

           // canvas.drawColor(Color.BLUE);//To make background 
            canvas.drawBitmap(scaled,0,0,null);
            canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null);

     //       canvas.drawBitmap(;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            x=(int)event.getX();
            y=(int)event.getY();

            if(x<25)
                x=25;
            if(x> width)   
                x=width;
            if(y <25)
                y=25;
            if(y > height)
                y=height;



            updateCrosshair();

            return true;
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
        }

        private void updateCrosshair() {
            Canvas canvas = null;
            try {
                canvas = getHolder().lockCanvas(null);
                synchronized (getHolder()) {
                    this.onDraw(canvas);
                }
            }
            finally {
                if (canvas != null) {
                    getHolder().unlockCanvasAndPost(canvas);
                }
            }


        }
    }

我不确定如何继续使用如何将其与layout.xml文件合并。