how to draw a Bitmap and move it in java Android? -


i have spent month trying of together.

  • draw bitmap screen
  • on create move bitmap down @ constant rate
  • stop bimap when reaches bottom
  • allow bitmap clicked on while moves (a.k.a. not translation animation)

thanks helping put in advance. here attempt:

package com.example.animating;  import android.annotation.suppresslint; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view;   public class firstview extends view {      private int screenw;     private int screenh;      private float drawscalew;     private float drawscaleh;      int moverate = 10, dot1y, dot1x;      private context mycontext;      private bitmap dot;     private boolean dotsinking = true, gameover = false;      public firstview(context context) {         super(context);         mycontext = context;     }      @override     protected void onsizechanged(int w, int h, int oldw, int oldh) {         super.onsizechanged(w, h, oldw, oldh);         dot = bitmapfactory.decoderesource(mycontext.getresources(), r.drawable.dot);         screenw = w;         screenh = h;         drawscalew = (float) screenw / 800;         drawscaleh = (float) screenh / 600;         dot1y = (int) (475*drawscaleh);         dot1x = (int) (55*drawscalew);     }      public void run() {         if (!gameover) {             animatedot();         }     }      @suppresslint("drawallocation")     @override     protected void ondraw(canvas canvas) {         paint redpaint = new paint();         redpaint.setcolor(color.red);         canvas.drawbitmap(dot, dot1x, dot1y, null);     }      private void animatedot(){         if (dotsinking) {             dot1y -= moverate;         }     } } 

to answer first part of question

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" >  <linearlayout     android:id="@+id/textview1"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_above="@+id/button1"      />  <button     android:id="@+id/button1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_centerhorizontal="true"     android:text="button" />  </relativelayout> 

mainactivity class

public class mainactivity extends activity {      firstview dv;      @override      protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);     dv = new firstview(this);     setcontentview(r.layout.activity_main);     final button b= (button) findviewbyid(r.id.button1);     b.setonclicklistener(new onclicklistener()     {         public void onclick(view v) {             // todo auto-generated method stub             linearlayout rl= (linearlayout ) findviewbyid(r.id.textview1);             rl.addview(dv);             b.setvisibility(view.invisible);          }     });          } public class firstview extends view {  private int screenw; private int screenh;  private float drawscalew; private float drawscaleh;  int moverate = 10, dot1y, dot1x;  private context mycontext;  private bitmap dot; private boolean dotsinking = true, gameover = false;   public firstview(context context) { super(context); mycontext = context; }  @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); dot = bitmapfactory.decoderesource(mycontext.getresources(), r.drawable.ic_launcher); screenw = w; screenh = h; drawscalew = (float) screenw / 800; drawscaleh = (float) screenh / 600; dot1y = 0+dot.getheight(); dot1x = w/2-(dot.getwidth()/2); }  public void run() { if (!gameover) {     animatedot();       invalidate(); } } @override protected void ondraw(canvas canvas) { paint redpaint = new paint(); redpaint.setcolor(color.red); canvas.drawbitmap(dot, dot1x, dot1y, null); run(); } private void animatedot(){ if (dotsinking) {     if(dot1y<screenh-dot.getheight())     dot1y += moverate; }  } }    

i have button @ bottom of screen. when clicked (button becomes invisible) image moves top height of layout. when reaches layout height stops.

enter image description here don't know how add click listener image moving.

answered first part of question. modify above according needs.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -