简体   繁体   中英

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

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 (aka 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;
        }
    }
}

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!

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.

在此处输入图片说明 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM