我的代码中的位图图像(微笑)在屏幕上随机移动。我想在用户点击随机移动的位图图像时显示一条消息。我尝试使用Ontouch()方法但找不到解决方案。
公共类MainActivity扩展了Activity {
Myclass ourView;
Bitmap smile;
TextView dis;
int x=0,y=0,a=0,b=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
ourView=new Myclass(this);
setContentView(ourView);
}
public class Myclass extends View implements OnTouchListener{
int changingX=0,changingY=0;
Random crazy = new Random();
Paint ourblue=new Paint();
Paint Text=new Paint();
Paint text=new Paint();
public Myclass(Context context) {
super(context);
// TODO Auto-generated constructor stub
smile=BitmapFactory.decodeResource(getResources(), R.drawable.smiling);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
ourblue.setARGB(220, 221, 243, 249);
canvas.drawPaint(ourblue);
a=crazy.nextInt(350);
b=crazy.nextInt(550);
canvas.drawBitmap(smile,a,b , null);
if(x>(a-20)&&x<(a+20)&&y>(b-20)&&y<(b+20))
{
text.setARGB(220, 0, 0, 0);
text.setTextAlign(Align.CENTER);
text.setTextSize(50);
canvas.drawText("Awesome", 100, 100, text);
}
for(int i=0;i<1000000;i++)
{
}
invalidate();
}
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
x=(int) event.getX();
y=(int) event.getY();
return true;
}
}
}
答案 0 :(得分:0)
boolean displayText
onTouch
方法中,始终检查触摸是否发生在位图的矩形内。如果是,则设置displayText = true
,否则将其设置为false
onDraw
方法中,检查displayText
是否为真,如果是,请绘制文字。如果没有,请不要画它。