Custom Persuasion To Depict Bitmap Along Path

Posted on

Refer to my one-time do “Animation of moving bitmap along path“, the bitmap together with path are hard-coded within custom view. It’s a modified version; the affair to last drawn (bitmap/path) are held separated together with referenced yesteryear custom persuasion inwards a list, such that it is slow to insert to a greater extent than things.


Please notice:
– I don’t affair it’s a adept approach to produce this, I but exhibit a interesting exercise.
– You should non produce the calculation (such equally motion the bitmap) within onDraw(), it’s suggested to produce it inwards or together with then other thread.

Create a novel class AnimationThing.java. It agree the bitmap together with path of private thing.

package com.blogspot.android_er.androidmovingbitmapalongpath;  import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PathMeasure;  world class AnimationThing {      Paint paint;     Path animPath;     PathMeasure pathMeasure;     float pathLength;     Bitmap bm;     int bm_offsetX, bm_offsetY;     float step;     float distance;     float[] pos;     float[] tan;     Matrix matrix;      world AnimationThing(Paint paint,                           Path animPath,                           Bitmap bm,                           float step) {         this.paint = paint;          this.animPath = animPath;         pathMeasure = novel PathMeasure(this.animPath, false);         pathLength = pathMeasure.getLength();          this.bm = bm;         bm_offsetX = bm.getWidth()/2;         bm_offsetY = bm.getHeight()/2;          this.step = step;         distance = 0;         pos = novel float[2];         tan = novel float[2];          matrix = novel Matrix();     } }  

Create our custom view, AnimationView .java.

package com.blogspot.android_er.androidmovingbitmapalongpath;  import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View;  import java.util.ArrayList; import java.util.List;  world class AnimationView extends View {      List<AnimationThing> animationThingsList;     world AnimationView(Context context) {         super(context);         initAnimationView();     }      world AnimationView(Context context, AttributeSet attrs) {         super(context, attrs);         initAnimationView();     }      world AnimationView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);         initAnimationView();     }      private void initAnimationView(){         animationThingsList = novel ArrayList<>();     }      world void insertThing(AnimationThing thing){         animationThingsList.add(thing);     }      @Override     protected void onDraw(Canvas canvas) {         for (AnimationThing affair : animationThingsList){              //!!! Only the path of the terminal affair volition last drawn on hide             canvas.drawPath(thing.animPath, thing.paint);              if(thing.distance < thing.pathLength){                 thing.pathMeasure.getPosTan(thing.distance, thing.pos, thing.tan);                  thing.matrix.reset();                 float degrees = (float)(Math.atan2(thing.tan[1], thing.tan[0])*180.0/Math.PI);                 thing.matrix.postRotate(degrees, thing.bm_offsetX, thing.bm_offsetY);                 thing.matrix.postTranslate(thing.pos[0]-thing.bm_offsetX, thing.pos[1]-thing.bm_offsetY);                  canvas.drawBitmap(thing.bm, thing.matrix, null);                  thing.distance += thing.step;             }else{                 thing.distance = 0;             }         }          invalidate();     } }  

Layout, activity_main.xml.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidmovingbitmapalongpath.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <com.blogspot.android_er.androidmovingbitmapalongpath.AnimationView         android:id="@+id/MyAnimationView"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:background="#F0F0F0"/> </LinearLayout>  

MainActivity.java

package com.blogspot.android_er.androidmovingbitmapalongpath;  import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.os.Bundle; import android.support.v7.app.AppCompatActivity;  world class MainActivity extends AppCompatActivity {      AnimationView myAnimationView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myAnimationView = (AnimationView)findViewById(R.id.MyAnimationView);          prepareThings();     }      private void prepareThings(){         Paint paint;         Path animPath;         float step;         Bitmap bm;          pigment = novel Paint();         paint.setColor(Color.BLUE);         paint.setStrokeWidth(1);         paint.setStyle(Paint.Style.STROKE);          bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);          animPath = novel Path();         animPath.moveTo(100, 100);         animPath.lineTo(200, 100);         animPath.lineTo(300, 50);         animPath.lineTo(400, 150);         animPath.lineTo(100, 300);         animPath.lineTo(600, 300);         animPath.lineTo(100, 100);         animPath.close();          footstep = 1;          AnimationThing affair = novel AnimationThing(paint, animPath, bm, step);         myAnimationView.insertThing(thing);          //The instant affair         bm = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_add);          animPath.reset();         animPath.addCircle(400, 400, 300, Path.Direction.CW);         footstep = 3;         affair = novel AnimationThing(paint, animPath, bm, step);         myAnimationView.insertThing(thing);     } }  

 are held separated together with referenced yesteryear custom persuasion inwards a listing Custom persuasion to describe bitmap along pathDownload the files .

Next:
– Custom persuasion to describe bitmap along path, calculate inwards background thread