0

I'm implementing the onTouchListener demo. In that I want to generate imageView dynamically and move it throughout the frameLayout.I done with imageView creation and also implement the onTouchListener but the imageView is not moving as I want..Folloing is my code..

package your.packagename;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity {
    Button b1;
    Button b2;
    FrameLayout f;
    ImageView imageview;
    int windowwidth;
    int windowheight;
    private android.widget.FrameLayout.LayoutParams layoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        f = (FrameLayout) findViewById(R.id.framelayout);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        windowwidth = size.x;
        windowheight = size.y;
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageview = new ImageView(getBaseContext());
                imageview.setId(1);
                registerForContextMenu(imageview);
                imageview.setBackgroundResource(R.drawable.ic_launcher);
                FrameLayout.LayoutParams ivparam = new FrameLayout.LayoutParams(100, 100);
                imageview.setLayoutParams(ivparam);
                f.addView(imageview);
                imageview.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        layoutParams = (FrameLayout.LayoutParams) imageview.getLayoutParams();
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                break;
                            case MotionEvent.ACTION_MOVE:
                                int x_cord = (int) event.getRawX();
                                int y_cord = (int) event.getRawY();
                                if (x_cord > windowwidth) {
                                    x_cord = windowwidth;
                                }
                                if (y_cord > windowheight) {
                                    y_cord = windowheight;
                                }
                                layoutParams.leftMargin = x_cord - 25;
                                layoutParams.topMargin = y_cord - 25;
                                imageview.setLayoutParams(layoutParams);
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
            }
        });
    }
}

Please tell me am I going wrong or should I use any another way.

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
Akshay
  • 6,029
  • 7
  • 40
  • 59
  • 1
    Creating CustomView and drawing using onDraw is better option than addView – Pankaj Feb 19 '14 at 05:18
  • @Pankaj will you please give me any link or example which will be helpful for my demo.. – Akshay Feb 19 '14 at 05:21
  • See if this help and do furthur research: http://stackoverflow.com/questions/2423327/android-view-ondraw-always-has-a-clean-canvas – Pankaj Feb 19 '14 at 05:22
  • Check out https://github.com/Almeros/android-gesture-detectors It is very good example for moving and rotating image views. Modify code according to your need :) – Jigar Pandya Feb 19 '14 at 05:32

0 Answers0