从View到Activity获取字符串值

时间:2014-02-05 06:56:26

标签: android

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活动

现在我必须从strpathMytouchview活动DrawingBrush

有人可以告诉我如何将Drawpath保存到DB

1 个答案:

答案 0 :(得分:2)

Mytouchview中的

strPath已经是一个公共属性,您应该可以直接在活动中使用它,即tv.strPath