将位图绘制到画布并显示在主屏幕/活动上?

时间:2013-11-19 19:08:35

标签: java android bitmap android-canvas

我正在尝试创建一个按下按钮一定次数的应用。达到此阈值时,将在主屏幕/活动上的随机点创建位图图像。我希望它出现在与主要活动上的按钮相同的屏幕上。

这是我的Imaga.java类代码:

package com.example.projectname;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Image {

private Bitmap bitmap;
private int x;
private int y;

public Image(Bitmap bitmap) {
    this.bitmap = bitmap;
    x = 0;
    y = 0;
}

public void draw(Canvas canvas, WindowManager wm, Bitmap bm) {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    x = 0 + (int) (Math.random() * displayMetrics.widthPixels);
    y = 0 + (int) (Math.random() * displayMetrics.heightPixels);

    canvas.drawBitmap(bm, x, y, null);
}

}

这是我的MainActivity.java:

package com.example.projectname;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView output;
int beans = 0;
int nextWorker = 5;

// Button clicker;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void increment(View view) {
    output = (TextView) findViewById(R.id.output);
    beans++;
    output.setText("Beans: " + beans);

    Canvas canvas = new Canvas();

    if(beans >= nextWorker){
        nextWorker = (int) ((int)(beans*1.1) + 8*(beans/4.0));
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.popup_picture);
        Image image = new Image(bm);

        image.draw(canvas, wm, bm);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/flag" >

<TextView
    android:id="@+id/output"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/clicker"
    android:text="Beans: 0"
    android:gravity="center"
    android:textSize="25dp" 
    android:textColor="#5B2F0B"
    android:typeface="serif"
     />

<ImageButton
    android:id="@+id/clicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@android:color/transparent"
    android:onClick="increment"
    android:src="@drawable/crown" />

</RelativeLayout>

当我按下按钮时,计数器会递增并显示正确的数字。我甚至确保在达到阈值时if(beans&gt; = nextWorker){}循环中的代码正在执行。但是,屏幕上没有任何内容。我认为问题可能是画布和主要活力之间的联系。 有小费吗?谢谢你的帮助。

0 个答案:

没有答案