简体   繁体   中英

Android ImageView and OnTouchListener finger move

i'm very new to Android SDK, so excuse my silly question. I made a simple app with an ImageView set as background android:background="@drawable/image" and i added two zoom buttons, so i can zoom in and out the picture that is on the whole screen.So my question is should i use OnTouchListener or is there a better way to just move the picture view up,down, left or right with my finger, i just need these basic events, nothing else. I'm added part of my code, if anyone advises me, where would be better to implement it.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load_map);

        zoom = (ZoomControls) findViewById(R.id.zoomControls1);
        img = (ImageView) findViewById(R.id.imageView1);

        startX = img.getScaleX();
        startY = img.getScaleY();

        zoom.setOnZoomInClickListener(new OnClickListener() {

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

                float x = img.getScaleX();
                float y = img.getScaleY();

                img.setScaleX((float) (x+1));
                img.setScaleY((float) (y+1));                   
            }
        });

        zoom.setOnZoomOutClickListener(new OnClickListener() {

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

                float x = img.getScaleX();
                float y = img.getScaleY();
                if((x>startX) & (y>startY)) {
                    img.setScaleX((float) (x-1));
                    img.setScaleY((float) (y-1));
                }
            }
        });
}

Is it best to use OnTouchListener ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM