Pages

Monday, September 19, 2016

Android Example -Animate a Bitmap in X and Y direction


activity_main.xml
<RelativeLayout 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"
    tools:context=".MainActivity" >



    <ImageView android:id="@+id/animatedImage"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_centerInParent="true"
        android:src="@drawable/rasenshuriken"/>
</RelativeLayout>

MainActivity.java
package in.kantapp.example20;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    animate var;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        /*setContentView(R.layout.activity_main);
        ImageView imageView= (ImageView) findViewById(R.id.animatedImage);

        Animation animation= AnimationUtils.loadAnimation(this,R.anim.roatate);
        imageView.startAnimation(animation);*/

        var = new animate(this);
        setContentView(var);
    }


}

anim/roatate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator" >

    <rotate android:duration="500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:startOffset="0"
        android:toDegrees="360"/>
</set>

animete.java
package in.kantapp.example20;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class animate extends View{

    Bitmap bm;
    int x, y;
    public animate(Context context) {
        super(context);

        bm=BitmapFactory.decodeResource(getResources(), R.drawable.rasengan);

        x = 0; y = 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Rect myrect = new Rect(0, 0, canvas.getWidth(),canvas.getHeight());

        Paint pa = new Paint();
        pa.setColor(Color.WHITE);
        pa.setStyle(Paint.Style.FILL);


        canvas.drawRect(myrect, pa);


        if (x < canvas.getWidth()) {
            x += 1;
        }
        else {
            x = 0;
        }
        if (y < canvas.getHeight()) {
            y += 15;
        }
        else {
            y = 0;
        }
        canvas.drawBitmap(bm, x, y, new Paint());
        invalidate();//calls this method again and again
    }

}



No comments:

Post a Comment