0

我想将文本移动到图像上以将文本固定在图像上所需的位置。我成功运行了这段代码,但它不是最佳的,因为它不是用户友好的。
文本移动有时与手指的拾取和放下不匹配。
如果有人有更好的代码请与我分享或让我知道我是否遗漏了什么。

//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);
                                    }
4

1 回答 1

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;
        }

    }
于 2012-06-08T09:35:40.250 回答