Android 自定义View控件,实现跟随手指触摸移动的小球

Android UI组件是通过继承View类,然后绘制内容,比如ImageView,TextView等组件都是继承View类。

当Android系统提供的组件功能不能满足需求时,可以通过继承View类重写一个或多个方法,派生自定义的组件,View类常用重写方法:

1.构造器:View子类最基本的重写方法

protected voidonDraw(Canvas canvas)

public booleanonTouchEvent(MotionEvent event)

public boolean onTrackballEvent(MotionEvent event)
public boolean onKeyDown(int keyCode, KeyEvent event)
public boolean onKeyUp(int keyCode, KeyEvent event)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)//检测View组件及子组件大小
protected void onSizeChanged(int w, int h, int oldw, int oldh)
protected boolean onSetAlpha(int alpha)
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
public void onWindowFocusChanged(boolean hasWindowFocus)
protected void onFinishInflate()//回调方法,当应用从xml文件加载构建界面之后此方法会被回调
定义DrawView 实现跟随手指移动的小球 DrawView.java
package shortcut.song.com.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2017/1/6 0006.
 */

public class DrawView extends View {

    public float currentX = 80;
    public float currentY = 60;


    public DrawView(Context context)
    {
        super(context);
    }

    public DrawView(Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint p = new Paint();
        p.setColor(Color.RED);
        canvas.drawCircle(currentX, currentY, 10, p);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.currentX = event.getX(); //触摸座标X
        this.currentY = event.getY(); //触摸座标Y
        invalidate();//重绘组件
        return true;//返回true:事件已处理,此处必须返回true,否则小球移动不了。
    }
}
 
//Layout文件activity_draw_view.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:id="@+id/activity_draw_view"
    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="shortcut.song.com.myapplication.DrawViewActivity">
    <!-- 自定义的DrawView组件 -->
    <shortcut.song.com.myapplication.DrawView  
        android:id="@+id/drawview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
	android:background="#0ff0ff"/>
</RelativeLayout>

 
DrawViewActivity.java
package shortcut.song.com.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class DrawViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //通过XML布局实现
        //setContentView(R.layout.activity_draw_view);
        //final DrawView mDrawView = (DrawView)findViewById(R.id.drawview);

        //通过代码编程实现
        LinearLayout main = new LinearLayout(this);
        main.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
        main.setOrientation(LinearLayout.HORIZONTAL);
        main.setGravity(Gravity.CENTER);

        DrawView mDrawView = new DrawView(this);
        mDrawView.setBackgroundColor(Color.BLUE);
        main.addView(mDrawView);

        setContentView(main);//设置Aictivity 显示View

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongYuLong的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值