1

I have spent a good month trying to do all of this together.

  • Draw Bitmap to screen
  • On create move Bitmap down at a constant rate
  • Stop the Bimap when it reaches the bottom
  • Allow the Bitmap to be clicked on while it moves (a.k.a. not translation animation)

Thanks for you for helping put this all together in advance. Here is my 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;
        }
    }
}
4

2 回答 2

1

you should use the invalidate() method inside your onDraw() to make it redraw your bitmap's new position. and to make it stop when i reaches the bottom use the setbounds method for your bitmap inside an if sentence telling the bitmap that when the distance with the screec is 0 make it stop. you should set an onclicklistener to your bitmap.to.make it.clickable . hope this helps you!

于 2013-05-14T19:27:21.017 回答
1

To answer first part of your 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 a button at the bottom of the screen. When clicked (button becomes invisible) the image moves from the top to the height of the layout. When it reaches the layout height it stops.

enter image description here I don't know how to add a click listener for the image moving.

Answered first part of the question. Modify the above according to your needs.

于 2013-05-14T20:44:47.323 回答