布局文件:

<LinearLayout 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=".CustomViewActivity"
android:id="@+id/root"
android:orientation="vertical">
</LinearLayout>

自定义类:

这个类必须写在一个独立的java文件中

public class DrawView extends View
{
public float currentX = 40;
public float currentY = 50;
// 定义、并创建画笔
Paint p = new Paint();
public DrawView(Context context)
{
super(context);
}
public DrawView(Context context , AttributeSet set)
{
super(context,set);
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
// 设置画笔的颜色
p.setColor(Color.RED);
// 绘制一个小圆(作为小球)
canvas.drawCircle(currentX, currentY, 15, p);
}
// 为该组件的触碰事件重写事件处理方法
@Override
public boolean onTouchEvent(MotionEvent event)
{
// 修改currentX、currentY两个属性
currentX = event.getX();
currentY = event.getY();
// 通知当前组件重绘自己
invalidate();
// 返回true表明该处理方法已经处理该事件
return true;
}
}

将组件添加到容器中去

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); LinearLayout root=(LinearLayout) findViewById(R.id.root);
final myDrawView draw=new myDrawView(this);
draw.setMinimumWidth(300);
draw.setMinimumHeight(500);
root.addView(draw);
}
}

也可以在布局文件里加载自定义的组件。把布局文件修改如下:

Android studio在写布局文件的时候,代码补全可以侦测到自定义组件的名字,确实很智能。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="myDrawView"
android:id="@+id/root"
android:orientation="vertical"> <allegro.mydrawview.myDrawView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

这样在java代码中不必要额外写代码加载自定义的组件了。下面的代码已经注释了加载组件的代码。

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /*LinearLayout root=(LinearLayout) findViewById(R.id.root);
final myDrawView draw=new myDrawView(this);
draw.setMinimumWidth(300);
draw.setMinimumHeight(500);
root.addView(draw);*/
}
}

这个程序的效果是,随手指移动的小球。

Android学习:自定义组件,DrawView的更多相关文章

  1. Android学习----自定义Adapter实现ListView

    前言: 对于ListView而言,自定义的Adapter对于显示复杂的界面有很大的灵活性 .使用自定义的Adapter需要继承BaseAdapter,然后重写getCount(),getView(), ...

  2. Android学习自定义Dialog

    Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...

  3. Android学习笔记 Toast屏幕提示组件的使用方法

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  4. 【转】Android学习基础自定义Checkbox组件

    原文网址:http://forum.maiziedu.com/thread-515-1-1.html heckbox组件是一种可同时选中多项的基础控件,即复选框,在android学习中,Checkbo ...

  5. android学习日记20--连接组件之Intent和IntentFilter

    上次刚了解完Android的四大组件,现在学习组件间通信的Intent和IntentFilter 一.Intent 1.简述 Intent(意图)在应用程序运行时连接两个不同组件,是一种运行时的绑定机 ...

  6. Android之自定义View学习(一)

    Android之自定义View学习(一) Canvas常用方法: 图片来源 /** * Created by SiberiaDante on 2017/6/3. */ public class Bas ...

  7. Android学习系列(3)--App自动更新之自定义进度视图和内部存储

    友好的视觉感知和稳定的不出错表现,来自于我们追求美感和考虑的全面性,博客园从技术的角度,一直我都很欣赏.这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 这 ...

  8. 【转】 Pro Android学习笔记(六五):安全和权限(2):权限和自定义权限

    目录(?)[-] 进程边界 声明和使用权限 AndroidManifestxml的许可设置 自定义权限 运行安全通过两个层面进行保护.进程层面:不同应用运行在不同的进程,每个应用有独自的user ID ...

  9. Android自定义组件系列【5】——进阶实践(2)

    上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...

随机推荐

  1. tablespace(表空间) / segment(断) / extent(盘区)/ block(块)/datafile(文件)之间的关系

    tablespace(表空间) / segment(断) / extent(盘区)/ block(块)之间的关系   tablespace : 一个数据库划分为一个或多个表逻辑单位,即表空间,每个表空 ...

  2. GMA Round 1 逃亡

    传送门 逃亡 你在森林中,遇到了一只老虎.此时此刻,老虎在(0,0)的位置,你在(2,1)的位置. 你开始沿着一条林间小路逃亡,移动向量是$(\frac{\sqrt{6}}{2},\frac{\sqr ...

  3. springboot邮件发送与接收读取

    发送邮件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  4. Spring Mvc配置多视图 - tiles, velocity, freeMarker, jsp

    <!-- Velocity --> <bean id="velocityViewResolver" class="org.springframework ...

  5. centos7 快速安装 mariadb(mysql)

    从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install mariadb mariadb-server systemct ...

  6. PHP性能分析——xhprof(window 安装xhporf)

    1 下载xhprof的php扩展 因为官方的xhprof不支持php7,所以采用tideways版本的xhprof 下载地址:windows版tideways_xhprof 将windows版的dll ...

  7. [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript

    Sometimes we need to create modules at runtime, for example depending on a condition. We could even ...

  8. memcache源码编译安装

    问题描述: memcached未授权漏洞.运行账户root,对未授权,官方也没有账户认证,也想不明白,为啥不搞个账户认证memcached..... 问题解决: 未授权,网上大部分通过防火墙实现对未知 ...

  9. Ubuntu apt-get彻底卸载软件包【转】

    原文地址:https://blog.csdn.net/get_set/article/details/51276609 最近对ubuntu卸载参数的详细程度了解不够:转载已了解查用. 如果你关注搜索到 ...

  10. TensorFlow精选Github开源项目

    转载于:http://www.matools.com/blog/1801988 TensorFlow源码 https://github.com/tensorflow/tensorflow 基于Tens ...