将可绘制图像添加到画布背景

时间:2014-03-20 15:23:20

标签: android canvas background-image

我知道可以使用

绘制画布的背景
mPaint = new Paint();
mPaint.setColor(Color.RED);

我只是想知道如何设置它的永久背景。我尝试过使用xml文件但没有任何反应。有任何想法吗?

这是该项目的源代码,我一直在关注如何做到这一点,因为我对位图非常不熟悉。

画布类

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.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class GameBoard extends View{

    private int mFlagX = -1;
    private int mFlagY = -1;
    private Bitmap mBitmap = null;
    private Bitmap nBitmap = null;
    private Paint mPaint = null;
    private boolean isFlagHidden = false;
    private int mBoundX = -1;
    private int mBoundY = -1;

    //play with these values to make the app more or less challenging
    public final int CLOSER = 50;
    public final int CLOSE = 100;

    public GameBoard(Context context, AttributeSet aSet) { 
        super(context, aSet); 
        //load our bitmap
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star);
        //create a paint brush
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
    }

    @Override
    public void onDraw(Canvas canvas) {
        //initialize
        if ((mFlagX < 1) || (mFlagY < 1)) {
            mFlagX = (int) (getWidth() / 2) - mBitmap.getWidth() / 2;
            mFlagY = (int) (getHeight() / 2) - mBitmap.getHeight() / 2;
            mBoundX = (int)getWidth() - mBitmap.getWidth();
            mBoundY = (int)getHeight() - mBitmap.getHeight();
        }
        //draw background
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
        //draw the flag
        if (!isFlagHidden) {
            canvas.drawBitmap(mBitmap, mFlagX, mFlagY, null);
        }
    }

    public void hideTheFlag(){
        //randomize flag location 
        mFlagX = (int) Math.ceil(Math.random() * mBoundX);
        mFlagY = (int) Math.ceil(Math.random() * mBoundY);
        isFlagHidden = true;
        //force redraw
        invalidate();   
    }

    public void giveUp(){
        isFlagHidden = false;
        //force redraw
        invalidate();
    }   

    public Indicators takeAGuess(float x, float y) {

        //this is our "warm" area
        Rect prettyClose = new Rect(mFlagX - CLOSE, mFlagY - CLOSE, mFlagX+mBitmap.getWidth() + CLOSE, mFlagY+mBitmap.getHeight() + CLOSE);
        //normalize
        if (prettyClose.left < 0) prettyClose.left = 0;
        if (prettyClose.top < 0) prettyClose.top = 0;
        if (prettyClose.right > mBoundX) prettyClose.right = mBoundX;
        if (prettyClose.bottom > mBoundY) prettyClose.bottom = mBoundY;

        //this is our "hot" area
        Rect reallyClose = new Rect(mFlagX - CLOSER, mFlagY - CLOSER, mFlagX+mBitmap.getWidth() + CLOSER, mFlagY+mBitmap.getHeight() + CLOSER);
        //normalize
        if (reallyClose.left < 0) reallyClose.left = 0;
        if (reallyClose.top < 0) reallyClose.top = 0;
        if (reallyClose.right > mBoundX) reallyClose.right = mBoundX;
        if (reallyClose.bottom > mBoundY) reallyClose.bottom = mBoundY;

        //this is the area that contains our flag
        Rect bullsEye = new Rect(mFlagX, mFlagY, mFlagX+mBitmap.getWidth(), mFlagY+mBitmap.getHeight());

        //check to see where on the board the user pressed
        if (bullsEye.contains((int) x, (int)y)) {
            //found it
            isFlagHidden = false;
            invalidate();
            return Indicators.BULLSEYE;
        } else if (reallyClose.contains((int) x, (int)y)) {
            //hot
            return Indicators.HOT;
        } else if (prettyClose.contains((int)x, (int)y)) {
            //warm
            return Indicators.WARM;
        } else {
            //not even close
            return Indicators.COLD;
        }
    }
}

游戏类

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class FindTheStar extends Activity implements OnTouchListener, OnClickListener{

    private GameBoard mGameBoard = null;
    private boolean isFlagHidden = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_star);
        mGameBoard = (GameBoard) findViewById(R.id.Hide_canvas);
        mGameBoard.setOnTouchListener(this);
        Button b = (Button) findViewById(R.id.the_button);
        b.setOnClickListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.Hide_canvas) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (isFlagHidden) {
                    TextView tv = (TextView)findViewById (R.id.the_label);
                    switch (mGameBoard.takeAGuess(event.getX(), event.getY())) {
                    case BULLSEYE:
                        Button b = (Button) findViewById(R.id.the_button);
                        isFlagHidden = false;
                        b.setText("Go Hide!");
                        tv.setText("You found me!");
                        tv.setTextColor(Color.GREEN);
                        break;
                    case HOT:
                        tv.setText("You're hot!");
                        tv.setTextColor(Color.RED);
                        break;
                    case WARM:
                        tv.setText("Getting warm...");
                        tv.setTextColor(Color.YELLOW);
                        break;
                    case COLD:
                        tv.setText("You're cold.");
                        tv.setTextColor(Color.BLUE);
                        break;
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.the_button) {
            TextView tv = (TextView)findViewById (R.id.the_label);
            tv.setText("");
            Button b = (Button) findViewById(R.id.the_button);
            isFlagHidden = !isFlagHidden;
            if (isFlagHidden) {
                b.setText("Can't find me?");
                mGameBoard.hideTheFlag();
            } else {
                b.setText("Go Hide!");
                mGameBoard.giveUp();
            }
        }
    }
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView  
        android:id="@+id/the_label"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center" 
        android:textSize="20sp"
        android:layout_marginBottom="10dip"
        android:text="Lets Play Hide and Seek!"/>
    <Button
        android:id="@+id/the_button"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center" 
        android:layout_marginBottom="10dip"
        android:text="Go Hide!"/>

    <app.autismapp.GameBoard

        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:id="@+id/Hide_canvas"/>



</LinearLayout>

1 个答案:

答案 0 :(得分:1)

是的,您可以使用xml layout..i设置永久背景。通过创建两个类来完成此操作。

这是我在MainACtivity中的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     final BrushView view=new BrushView(this);
    setContentView(R.layout.mylayout);//removed this one if the paint doesnt work
    view.setBackgroundResource(R.drawable.background);//to set background
    setContentView(view);// to display the background

和我的第二堂课

public class PaintView extends View {

private Paint paint = new Paint();

public LayoutParams params;



public PaintView(Context context) {
    super(context);
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);

我希望它能给你一个想法