2012-06-08 5 views
0

이미지 위로 텍스트를 이동하여 원하는 위치의 텍스트를 이미지 위에 고정하려고합니다. 이 코드를 성공적으로 실행하지만 사용자에게 적합하지 않기 때문에 최적이 아닙니다.
가끔씩 텍스트 이동이 손가락의 선택 및 놓기와 일치하지 않습니다.
누군가 더 나은 코드를 가지고 있다면 나와 함께 공유하거나 뭔가를 놓치고 있는지 알려주세요. 활동 클래스의touch 텍스트 이동 이미지 위로

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

답변

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

    } 
+0

.. 된 setContentView (새 MYVIEW (이)); – Sandy09