制作Android上的一个随机移动可点击按钮 [英] Making a randomly moving clickable button on Android

查看:385
本文介绍了制作Android上的一个随机移动可点击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找创建随机移动的点击在Android的按钮(一个儿童游戏)。
我跟着这个网站上的code创建屏幕周围的一些随机移动圈
http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/

I'm looking to create randomly moving clickable buttons in Android (for a childrens game). I followed the code on this website to create some randomly moving circles around the screen http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/

与动画words查看更改为此:

with the Animated words view changed to this:

public class AnimatedWordsView extends ImageView {

private Context myContext;
int [] xCoOrd = {-1, -2, -3, -4, -5, -6, -7, -8};
int [] yCoOrd = {-1, -2, -3, -4, -5, -6, -7, -8};
int [] xVeloc = {4, 8, 12, 16, 20, 20, 20, 20};
int [] yVeloc = {2, 4, 6, 8, 10, 12, 14, 16};
private Handler handler;
private final int FRAME_RATE = 30;

public AnimatedWordsView(Context context, AttributeSet attributes){

    super(context, attributes);
    myContext = context;
    handler = new Handler();
}

private Runnable run = new Runnable() {
    @Override
    public void run() {
        invalidate();
    }
};

protected void onDraw(Canvas canvas){

    BitmapDrawable [] word = {(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green),
            (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow),
            (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue),
            (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow)};

    for(int count = 0; count <=7; count++ ) {
        if (xCoOrd[count] < 0 && yCoOrd[count] < 0) {
            xCoOrd[count] = this.getWidth() / 2;
            yCoOrd[count] = this.getHeight() / 2;
        } else {
            xCoOrd[count] += xVeloc[count];
            yCoOrd[count] += yVeloc[count];
        }
        if ((xCoOrd[count] > this.getWidth() - word[count].getBitmap().getWidth()) || (xCoOrd[count] < 0)) {
            xVeloc[count] = xVeloc[count] * -1;
        }
        if ((yCoOrd[count] > this.getHeight() - word[count].getBitmap().getHeight()) || (yCoOrd[count] < 0)) {
            yVeloc[count] = yVeloc[count] * -1;
        }
        canvas.drawBitmap(word[count].getBitmap(),xCoOrd[count],yCoOrd[count],null);
    }
    handler.postDelayed(run, FRAME_RATE);
}

我正在寻找的是什么,做类似于这样做的东西,但让我文本添加到圈子,让他们可点击

What i'm looking for is something that does something similar to what this does, but allows me to add text to the circles and make them clickable

有没有办法做到这一点?

Is there any way to do this?

感谢

推荐答案

您可以使用的drawText方法画布绘制文本:

You can use drawText method for Canvas to draw text:

public void drawText (CharSequence text, int start, int end, float x, float y, Paint paint)

事情是这样的:

Paint paint = new Paint(); 
canvas.drawPaint(paint); 
paint.setColor(Color.MY_COLOR); 
paint.setTextSize(24); 
canvas.drawText("My Text", x, y, paint); 

要点击视图,你只需要一个onClickListener添加到它:

To click the view you just need to add a onClickListener to it:

        myAnimatedWordsView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

           }
        });

这篇关于制作Android上的一个随机移动可点击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆