Android 自定义控件开发入门(二)

简介:

上一次我们讲了一堆实现自定义控件的理论基础,列举了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之路还有很远,我也没有举一些很难的例子,我认为基础知识只有这些,学习了这些之后自定义控件的基础也就讲完了,剩下的是大家在基础之上发挥了!之后如果有比较好的例子我还会继续补充的。


源码我会在下次一并发给大家链接的,希望大家能学到一些东西~


另外我也是学生,如果有写的不好或者有错误的地方还请大家多多指教,谢谢!

目录
相关文章
|
8天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
10天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
11天前
|
监控 Java Android开发
安卓应用开发:打造高效用户界面的五大策略
【4月更文挑战第29天】 在安卓应用开发的世界中,构建一个既美观又高效的用户界面(UI)对于吸引和保留用户至关重要。本文将深入探讨五种策略,这些策略可以帮助开发者优化安卓应用的UI性能。我们将从布局优化讲起,逐步过渡到绘制优化、内存管理、异步处理以及最终的用户交互细节调整。通过这些实践技巧,你将能够为用户提供流畅而直观的体验,确保你的应用在竞争激烈的市场中脱颖而出。
|
1天前
|
Android开发
Android游戏引擎AndEngine入门资料
Android游戏引擎AndEngine入门资料
|
1天前
|
Java Android开发
android AsyncTask入门
android AsyncTask入门
|
2天前
|
Java API 开发工具
java与Android开发入门指南
java与Android开发入门指南
8 0
|
3天前
|
Android开发 Kotlin
Kotlin开发Android之基础问题记录
Kotlin开发Android之基础问题记录
15 1
|
3天前
|
Java Android开发
Android开发@IntDef完美替代Enum
Android开发@IntDef完美替代Enum
12 0
|
4天前
|
Android开发
Android 盒子开发过程中遇到的问题及解决方法
Android 盒子开发过程中遇到的问题及解决方法
8 2
|
5天前
|
机器学习/深度学习 算法 Android开发
安卓应用开发:打造高效通知管理系统
【5月更文挑战第6天】 在现代移动应用的海洋中,用户经常面临信息过载的挑战。一个精心设计的通知管理系统对于提升用户体验至关重要。本文将探讨在安卓平台上如何实现一个高效的通知管理系统,包括最佳实践、系统架构设计以及性能优化技巧。通过分析安卓通知渠道和优先级设置,我们的目标是帮助开发者构建出既能吸引用户注意,又不会引发干扰的智能通知系统。
16 2