触摸文本移动到图像上

时间:2012-06-08 09:17:28

标签: android

我想在图像上移动文本以将文本固定在图像上的所需位置。 我成功地运行了这个代码但是它不是最佳的,因为它不是用户友好的。
文本运动有时甚至与手指的选择都不匹配。
如果有人有更好的代码请与我分享或让我知道如果我错过了什么。

//Listeners for the Canvas that is being awarded
                 popImgae.setOnTouchListener( new OnTouchListener(){

                        public boolean onTouch(View v, MotionEvent e) {
                            someGlobalXvariable = e.getX();
                            someGlobalYvariable = e.getY();
                            setTextPosition();

                            //saveImage(imgRecord.get(1),leftPos,topPos,popText.getTextSize(),popText.getText().toString());
                            return true;
                        }
                    });
                                     public void setTextPosition(){
                                         try {
                                        redrawImage(imgRecord.get(1),Integer.parseInt(imgRecord.get(8)),imgRecord.get(6),Integer.parseInt(imgRecord.get(9)));
                                        } catch (Exception e) {
                                            // TODO: handle exception
                                            System.out.println("##########Error in setTextPositio========="+e.getMessage());
                                        }

                                     }

                                    ///Redrawing the image & touchin Move of the Canvas with text
                                    public void redrawImage(String path,float sizeValue,String textValue,int colorValue) {

                                        //Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.fashion_pic);
                                        BitmapFactory.Options options = new BitmapFactory.Options();
                                        try {
                                             options.inMutable = true;
                                        } catch (Exception e) {
                                            // TODO: handle exception
                                            System.out.println("#############Error is======"+e.getMessage());
                                        }

                                        Bitmap bm = BitmapFactory.decodeFile(path,options);
                                        //bm = imageManipulation.convertToMutable(bm);

                                        proxy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Config.ARGB_8888);
                                        Canvas c = new Canvas(proxy);

                                        //Here, we draw the background image.
                                        c.drawBitmap(bm, new Matrix(), null);

                                        Paint paint = new Paint();
                                        paint.setColor(colorValue); // Text Color
                                        paint.setStrokeWidth(30); // Text Size
                                        paint.setTextSize(sizeValue);

                                        System.out.println("Values passing=========="+someGlobalXvariable+",   "+someGlobalYvariable+",   "
                                              +sizeValue+",   "+textValue);

                                        //Here, we draw the text where the user last touched.
                                        c.drawText(textValue, someGlobalXvariable, someGlobalYvariable, paint);

                                        popImgae.setImageBitmap(proxy);
                                    }

1 个答案:

答案 0 :(得分:0)

class MyView  extends View{

        public MyView(Context context) {
            super(context);
            //get your drawable image 
            setBackgroundDrawable(drawable);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawText("text", x, y, new Paint());
        }

        float x,y;
        @Override
        public boolean onTouchEvent(MotionEvent event) {

            x= event.getX();
            y= event.getX();
            invalidate();
            return true;
        }

    }