Feb 19, 2015

ObjectAnimator to animate coordinates along a path

In Android 5.0, android:minSdkVersion="21", ObjectAnimator has a new constructors that enable you to animate coordinates along a path using two or more properties at once. Here is a example:


package com.example.androidobjectanimator;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

 LinearLayout mainLayout;
 TextView textTitle;
 Button buttonMove;
 ImageView image;

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

  mainLayout = (LinearLayout) findViewById(R.id.mainlayout);
  mainLayout.setOnClickListener(MyOnClickListener);
  textTitle = (TextView) findViewById(R.id.title);
  textTitle.setOnClickListener(MyOnClickListener);
  buttonMove = (Button) findViewById(R.id.buttonflip);
  buttonMove.setOnClickListener(MyOnClickListener);
  image = (ImageView) findViewById(R.id.image);
  image.setOnClickListener(MyOnClickListener);
 }

 OnClickListener MyOnClickListener = 
  new OnClickListener() {

  @Override
  public void onClick(View v) {
   moveit(v);
  }

 };

 private void moveit(final View view) {

  float x = view.getX();
  float y = view.getY();
  Path path = new Path();

  path.moveTo(x + 0, y + 0);
  path.lineTo(x + 100, y + 150);
  path.lineTo(x + 400, y + 150);
  path.lineTo(x + 0, y + 0);
  ObjectAnimator objectAnimator = 
   ObjectAnimator.ofFloat(view, View.X,
    View.Y, path);
  objectAnimator.setDuration(3000);
  objectAnimator.start();
 }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainlayout"
    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"
    tools:context="com.example.androidflipview.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="28dp"
        android:textStyle="bold" />
    
    <Button
        android:id="@+id/buttonflip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Move" />
    
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
</LinearLayout>

2 comments:

  1. this method is only working for API21+.
    can u please resolve issue for below version.
    atleast for API 11+.
    waiting for your healthy response.

    ReplyDelete
  2. Not working on API 21+, Only working on API 21

    ReplyDelete

Infolinks In Text Ads