上一次我们讲了一堆实现自定义控件的理论基础,列举了View类一些可以重写的方法,我们对这些方法的重写是我们继承View类来派生自定义控件的关键


我通过一个最简单的例子给大家展示了这一个过程,无论是多么复杂的自定义控件,思路总是这样子的,但是因为我们仅仅重写了onDraw方法使得大家觉得怪怪的,作为一个控件,我们居然还要为了他的实现为其增加麻烦的监听,这就不能叫做控件了。

下面再给大家介绍一个经常重写的方法法:publicboolean onTouchEvent (MotionEvent event)


通过这个方法,我们就把写在Activity的监听部分内置在控件内部了,这才能叫做一个完整的控件,其功能是建立一片区域,并其中包含一个可以根据手指触摸而改变位置的小球。

下面我们来看一下这个触摸事件方法:

publicboolean onTouchEvent (MotionEvent event)


Added in API level 1


Implement this method to handle touch screen motionevents.

If this method is used to detect click actions, it isrecommended that the actions be performed by implementing and calling performClick(). This willensure consistent system behavior, including:

obeying click sound preferences

dispatching OnClickListener calls

handling ACTION_CLICK whenaccessibility features are enabled


Parameters

event

The motion event.



Returns

True if the event was handled, false otherwise.

 

这样我们就可以把我们刚才在Activity的类中做的工作放到我们的自定义控件中来实现

只要去掉刚才的setter 和 getter 然后重写这个触摸事件的方法就可以了:



public boolean onTouchEvent(MotionEvent motionevent){
		
		CircleX  = motionevent.getX();
		CircleY  = motionevent.getY();
		this.invalidate();
		
		return true;



这样我们只需要再简单的在xml中调用,一切都愉快的解决了!

这个例子我会和第一个一并放在一起的,就和我之前写的适配器的教程一样,源码我会整理再一起再给大家。第二个程序我注释就不那么注意啦……不是我懒,最近比较忙(其实就是懒)



下面我贴一下代码:


Activity的代码:



package com.example.customcomponentsdemo.Activity;

import com.example.customcomponentsdemo.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class MoveBallActivity2 extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_moveball2);
	}

}



简介了好多有木有!

自定义View版本2的代码:


package com.example.customcomponentsdemo.component;

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

public class DrawView2 extends View{

	private Context context;
	
	private float CircleX = 100;
	private float CircleY = 100;
	private float CircleR = 10;
	
	public DrawView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	public void onDraw(Canvas canves){
		Paint paint = new Paint();
		
		paint.setColor(Color.BLUE);
		canves.drawCircle(CircleX, CircleY, CircleR, paint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent motionevent){
		
		CircleX  = motionevent.getX();
		CircleY  = motionevent.getY();
		this.invalidate();
		
		return true;
		
	}


}



这样就简洁了好多!


还有xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是MoveBall的Demo 版本2"
        android:textColor="@color/white" >
    </TextView>

    <com.example.customcomponentsdemo.component.DrawView2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" >
    </com.example.customcomponentsdemo.component.DrawView2>
    
    

</LinearLayout>




 这样我们的第二个教程也就先到这里了,这次的主题不是这个ontouch方法,而是要告诉大家,自定义控件的核心是重写这些方法,并添加所需要的逻辑,View的方法不多也不少,我就用这个例子给大家抛砖引玉一下,希望大家在自定义自己的控件并选择了继承View这条路时,要花时间去了解和理解这些方法的重写方法,这是十分重要的。 下次再给大家介绍一下如果自定义的View需要有自定义的属性我们该如何处理,下一讲也将会是这个系列完结篇了,因为自定义View之路还有很远,我也没有举一些很难的例子,我认为基础知识只有这些,学习了这些之后自定义控件的基础也就讲完了,剩下的是大家在基础之上发挥了!之后如果有比较好的例子我还会继续补充的。