James
James

Reputation: 14101

Move an image dynamically in android

This is the oncreate from my application.

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

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

     getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    viewControl = controlInflater.inflate(R.layout.control, null);
    image =(ImageView) viewControl.findViewById(R.id.img);
    LayoutParams layoutParamsControl 
        = new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

}

control.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
        >
<ImageView
    android:id="@+id/img"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher"
    />

 <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button"
/>

</LinearLayout>

How can I move this image on a button click. Actually i want to transalate this image from the current pixel position to another.

I tried this on the button click.

 b =(Button)findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { 
                System.out.println("Button clicked");
                TranslateAnimation anim = new TranslateAnimation(0,0,200,200);              
                anim.setDuration(2000);
                image.setAnimation(anim); 
            }
        });

but now the image will disappear for two seconds. What am I doing wrong. please any one help. Actually i want to move the image on accelerometer change.

Upvotes: 1

Views: 4596

Answers (1)

user370305
user370305

Reputation: 109237

If you are using Animation then your Image is back to the start position after it completes the animation.

If you want to achieve like this then just increase Animation duration,

Or If you want to scale image one place to another without animation,

Remove animation and just Use on your button's click method,

 ImageView.scrollBy(scrollByX, scrollByY);

For more info look at Android: Scrolling an Imageview.

Upvotes: 1

Related Questions