簡體   English   中英

如何在Java Android中繪制位圖並將其移動?

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

我花了一個月的時間嘗試一起完成所有這些工作。

  • 在屏幕上繪制位圖
  • 創建時以恆定速率向下移動位圖
  • 到達底部時停止Bimap
  • 允許在位圖移動時單擊它(也就是不翻譯動畫)

多謝您協助預先將所有內容整合在一起。 這是我的嘗試:

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

您應該在onDraw()中使用invalidate()方法,使其重繪位圖的新位置。 並使其停止,直到我到達底部時,在if語句中使用setbounds方法為您的位圖停止,該語句告訴位圖,當screec的距離為0時使其停止。 您應該將onclicklistener設置為bitmap.to.make.clickable。 希望這對您有幫助!

回答問題的第一部分

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類

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

我在屏幕底部有一個按鈕。 單擊時(按鈕變為不可見),圖像從頂部移到布局的高度。 當達到布局高度時,它停止。

在此處輸入圖片說明 我不知道如何為圖像移動添加點擊偵聽器。

回答了問題的第一部分。 根據需要修改以上內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM