安卓开发例4-4设计一个在屏幕上移动小球的程序

设计一个自定义组件继承于Android.view.View的图形绘制类TestView,在该视图组件中绘制一个小球:再设计一个实现监听触摸屏事件的方法onTouch(View v,MotionEvent event),该方法监听并获取触摸屏的坐标位置,并把坐标值传递给图形绘制 类TestView,由TestView在该位置重绘小球。

(一)设计图形绘制类TestView(自定义组件)

package com.example.test44;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

class TestView extends View {
    int x = 150, y = 50;
    public TestView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    void getXY(int _x, int _y) {
        x = _x;
        y = _y;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);/*设置背景颜色为白色*/
        Paint paint = new Paint();
        paint.setAntiAlias(true);/*去锯齿*/
        paint.setColor(Color.BLACK);/*设置paint的颜色*/
        canvas.drawCircle(x, y, 30, paint);/*画一个实心圆*/
        paint.setColor(Color.WHITE);/*画实心圆上的小白点*/
        canvas.drawCircle(x - 9, y - 9, 6, paint);



    }
}

(二)把自定义组件添加到布局文件activity_main.xml中。

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.test44.MainActivity">
    <com.example.test44.TestView
        android:id="@+id/testview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

(三)设计主控文件MainActivity.java。

package com.example.test44;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {
    TestView tView = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tView = (TestView) findViewById(R.id.testview1);
        tView.setOnTouchListener(new mOnTouch());
    }

    class mOnTouch implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x1, y1;
            x1 = (int) event.getX();
            y1 = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                tView.getXY(x1, y1);
                tView.invalidate();
                return true;
            }
        else if (event.getAction() == MotionEvent.ACTION_MOVE)
                {
                    tView.getXY(x1, y1);
                    tView.invalidate();

            return true;
            }
            return tView.onTouchEvent(event);
        }

    }
}

以下是演示图:

心得与体会:

在Android系统中通过OnTouchListener监听接口来处理屏幕事件,当在View的范围内进行触摸按下,抬起或滑动等动作时都会触发该事件。

在设计简单触摸屏事件程序时要实现Android.view.View.OnTouchListener接口,并重写该接口的监听方法onTouch(View v,MotionEvent event)。

MotionEvent.ACTION_DOWN:在屏幕上点击。

MotionEvent.ACTION_UP:抬起

MotionEvent.ACTION_MOVE:在屏幕上滑动。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值