2014-02-05 3 views
0

MyTouchEventViewПолучить строковое значение с видом на активность

public class MyTouchEventView extends View { 

    private Paint paint = new Paint(); 
    private Path path = new Path(); 
    private Paint circlePaint = new Paint(); 
    private Path circlePath = new Path(); 

    public Button btnReset; 
    public LayoutParams params; 
    public String strpath = ""; 

    public MyTouchEventView(Context context) { 
     super(context); 

     paint.setAntiAlias(true); 
     paint.setColor(Color.BLACK); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(2f); 

     circlePaint.setAntiAlias(true); 
     circlePaint.setColor(Color.BLUE); 
     circlePaint.setStyle(Paint.Style.STROKE); 
     circlePaint.setStrokeJoin(Paint.Join.MITER); 
     circlePaint.setStrokeWidth(2f); 

     btnReset = new Button(context); 
     btnReset.setText("Clear"); 

     params = new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     btnReset.setLayoutParams(params); 

     btnReset.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       // resets the screen 
       path.reset(); 

       // Calls the onDraw() method 
       postInvalidate(); 
      } 
     }); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     canvas.drawPath(path, paint); 
     canvas.drawPath(circlePath, circlePaint); 
     // Toast.makeText(getContext(), strpath, Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     // Gives you x and y coordinates on the Event. 
     float pointX = event.getX(); 
     float pointY = event.getY(); 

     // Checks for the event that occurs 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(pointX, pointY); 
      strpath = "(0-" + Float.toString(pointX) + "-" 
        + Float.toString(pointY) + ")"; 
      return true; 
     case MotionEvent.ACTION_MOVE: 
      path.lineTo(pointX, pointY); 
      circlePath.reset(); 
      strpath = "(2-" + Float.toString(pointX) + "-" 
        + Float.toString(pointY) + ")"; 
      // (circle's center x-coordinate, y-coordinate, radius of the 
      // circle, direction to wind the shape) 
      circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW); 
      // circlePath.addRect(pointX - 25, pointY - 25, pointX + 25, pointY 
      // + 25, Path.Direction.CW); 
      /* 
      * RectF rect = new RectF(pointX - 25, pointY - 25, pointX + 25, 
      * pointY + 25); circlePath.addRoundRect(rect, 0, 0, 
      * Path.Direction.CW); 
      */ 
      break; 

     case MotionEvent.ACTION_UP: 
      circlePath.reset(); 
      strpath = "(1-" + Float.toString(pointX) + "-" 
        + Float.toString(pointY) + ")"; 
      break; 
     default: 
      return false; 
     } 

     // Schedules a repaint. 
     // Force a view to draw. 
     postInvalidate(); 
     return true; 
    } 
} 

и

public class DrawingBrush extends Activity { 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 

     Log.d("Focus debug", "Focus changed !"); 

     if (!hasFocus) { 
      Log.d("Focus debug", "Lost focus !"); 

      Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
      sendBroadcast(closeDialog); 
     } 
    } 

    Button btnSave, btnNewPage; 
    ProgressDialog pd; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     DisplayMetrics displaymetrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
     int height = displaymetrics.heightPixels; 

     height = height/2; 
     MyTouchEventView tv = new MyTouchEventView(this); 
     // tv.setBackgroundResource(drawable.shapebg); 
     LinearLayout lnrmain = (LinearLayout) findViewById(R.id.lnrMain); 

     LinearLayout lnrMid = (LinearLayout) findViewById(R.id.lnrSurface); 

     LinearLayout lnrfoot = new LinearLayout(DrawingBrush.this); 
     lnrfoot.setOrientation(LinearLayout.HORIZONTAL); 
     LayoutParams LLParams = new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     lnrfoot.setLayoutParams(LLParams); 
     lnrmain.addView(lnrfoot); 
     lnrMid.addView(tv); 
     btnSave = (Button) findViewById(R.id.btnSave); 
     btnNewPage = (Button) findViewById(R.id.btnNewPage); 

     btnSave.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       new Connection().execute(); 
      } 
     }); 
     // tv.btnReset.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
     // addContentView(tv.btnReset, tv.params); 

    } 

    private class Connection extends AsyncTask<Object, Object, Object> { 
     String ftpres, fna, updt; 

     @Override 
     protected void onPreExecute() { 
      pd = ProgressDialog.show(DrawingBrush.this, "", 
        "Uploading Image file", true, false); 
     } 

     @Override 
     protected Object doInBackground(Object... arg0) { 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Object unused) { 
      pd.dismiss(); 

     } 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     finish(); 
    } 

    @Override 
    public void onAttachedToWindow() { 
     // Disable home button 
     this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); 
     super.onAttachedToWindow(); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     // Disable all keys 
     return false; 
    } 

} 

Mytouchview добавляется в DrawingBrush активности моя деятельность,

Теперь я должен принять strpath от Mytouchview к DrawingBrush Мероприятия.

Может ли кто-нибудь сказать мне, как сохранить Drawpath в DB.

ответ

2

strPath в Mytouchview уже государственная собственность, вы должны быть непосредственно в состоянии использовать его от активности т.е. tv.strPath

Смежные вопросы