在Android上触摸事件的Imageview上绘制一个矩形?

时间:2014-07-21 09:43:41

标签: android imageview touch draw

我想在触摸点处在此图像上绘制一个小矩形(应该在触摸位置移动)。只应显示最新的矩形。这是我的代码:

public class MainActivity extends Activity  {

    File[] file;
    static int n;
    Paint myPaint;
    Canvas c;

    private static int RESULT_LOAD_IMAGE = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        if(n != 2)
        {
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
                n = 2;
        }

         final TextView xCoord = (TextView) findViewById(R.id.textView1);
         final TextView yCoord = (TextView) findViewById(R.id.textView2);

         final View touchView = findViewById(R.id.parent);          

         touchView.setOnTouchListener(new View.OnTouchListener() 
         {              
                @Override
                public boolean onTouch(View v, MotionEvent event) 
                {
                    // TODO Auto-generated method stub
                     final int action = event.getAction();
                        switch (action & MotionEvent.ACTION_MASK) {

                            case MotionEvent.ACTION_DOWN: {
                                xCoord.setText(String.valueOf((int) event.getX()));
                                yCoord.setText(String.valueOf((int) event.getY()));

                                break;
                            }

                            case MotionEvent.ACTION_MOVE:{
                                xCoord.setText(String.valueOf((int) event.getX()));
                                yCoord.setText(String.valueOf((int) event.getY()));

                                break;
                            }
                        }
                        return true;
                }

        });


    }//oncreate

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.imgView);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }


        }

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/parent"
    tools:context="com.example.screenshot.MainActivity$PlaceholderFragment" >

   <LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="wrap_content"   
    android:orientation="horizontal">



    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    </ImageView>




</LinearLayout>

0 个答案:

没有答案