Custom Stance To Depict Bitmap Along Path, Calculate Inward Background Thread

Posted on

Last transportation demonstrate a instance of “Custom sentiment to depict bitmap along path“, amongst calculation run within onDraw(). It’s modified version to pre-calculate inwards dorsum thread.


(remark: inwards final post, canvas.drawPath() is called within onDraw(). It appear run real slow, removed inwards this example.)

Modify 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;  populace course of teaching AnimationView extends View {      List<AnimationThing> animationThingsList;     populace AnimationView(Context context) {         super(context);         initAnimationView();     }      populace AnimationView(Context context, AttributeSet attrs) {         super(context, attrs);         initAnimationView();     }      populace AnimationView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);         initAnimationView();     }      somebody void initAnimationView(){         animationThingsList = novel ArrayList<>();     }      populace void insertThing(AnimationThing thing){         animationThingsList.add(thing);     }      //prepare calculation inwards background thread     populace void preDraw(){         for (AnimationThing affair : animationThingsList){             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);                  thing.distance += thing.step;             }else{                 thing.distance = 0;             }         }     }      @Override     protected void onDraw(Canvas canvas) {         for (AnimationThing affair : animationThingsList){             canvas.drawBitmap(thing.bm, thing.matrix, null);         }          invalidate();     }      /*     @Override     protected void onDraw(Canvas canvas) {         for (AnimationThing affair : animationThingsList){              //This code run slow!!!             //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();     }     */   }  

MainActivity.java

package com.blogspot.android_er.androidmovingbitmapalongpath;  import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Path; import android.os.Bundle; import android.support.v7.app.AppCompatActivity;  populace course of teaching MainActivity extends AppCompatActivity {      AnimationView myAnimationView;     AniThread myAniThread;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myAnimationView = (AnimationView)findViewById(R.id.MyAnimationView);          prepareThings();         myAniThread = novel AniThread(myAnimationView);         myAniThread.start();     }      somebody void prepareThings() {         Path animPath;         float step;         Bitmap bm;          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(animPath, bm, step);         myAnimationView.insertThing(thing);          //The minute 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(animPath, bm, step);         myAnimationView.insertThing(thing);     }      class AniThread extends Thread{          AnimationView targetView;          populace AniThread(AnimationView target) {             super();             targetView = target;         }          @Override         populace void run() {             piece (true){                 targetView.preDraw();                 seek {                     Thread.sleep(10);                 } grab (InterruptedException e) {                     e.printStackTrace();                 }             }          }     }  }  

The other files, AnimationThing.java and activity_main.xml, mention last post.

  populace course of teaching AnimationView extends View  Custom sentiment to depict bitmap along path, calculate inwards background threadDownload the files .