参考《疯狂android讲义》第2版 2.1节P48,对应CustomViewDemo.zip。

若在开发过程中,发现现有的view均不能满足需要,可以自定义一个view。

自定义一个view 的关键在于重写view类的几个核心方法,如onDraw, onTouchEvent等。

自定义view类:

DrawBall.java

package com.ljh.customviewdemo.ui;

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 DrawBall extends View { private Paint p = new Paint();
private float currentX = 40;
private float currentY = 50; public DrawBall(Context context) {
super(context);
} public DrawBall(Context context, AttributeSet set) {
super(context, set);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
p.setColor(Color.RED);
canvas.drawCircle(currentX, currentY, 15, p);
} @Override
public boolean onTouchEvent(MotionEvent event) { currentX = event.getX();
currentY = event.getY(); /*
* Invalidate the whole view. If the view is visible,
* onDraw(android.graphics.Canvas) will be called at some point in the
* future. This must be called from a UI thread. To call from a non-UI
* thread, call postInvalidate().
*/
invalidate();
return true;
} }

Activity类:MainAtivity.java

package com.ljh.customviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

布局文件:activity_main.xml

<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"
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=".MainActivity" > <!-- 注意:此处使用类的全路径,标明ui类型。 -->
<com.ljh.customviewdemo.ui.DrawBall
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </RelativeLayout>

注意,activity中包括多个view。一个activity对应一个布局文件,布局文件中包括多个view,这些view可以是android定义好的,或者自定义的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

自定义view组件 分类: H1_ANDROID 2013-10-26 21:55 741人阅读 评论(0) 收藏的更多相关文章

  1. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  2. 【solr专题之二】配置文件:solr.xml solrConfig.xml schema.xml 分类: H4_SOLR/LUCENCE 2014-07-23 21:30 1959人阅读 评论(0) 收藏

    1.关于默认搜索域 If you are using the Lucene query parser, queries that don't specify a field name will use ...

  3. ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 175人阅读 评论(0) 收藏

    一.功能说明: 可以解决类似 http://****/news 情形,Url路径支持正则匹配. 二.操作步骤: 1.增加URL重写模块: using System; using System.IO; ...

  4. ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 174人阅读 评论(0) 收藏

    一.功能说明: 可以解决类似 http://****/news 情形,Url路径支持正则匹配. 二.操作步骤: 1.增加URL重写模块: using System; using System.IO; ...

  5. Safecracker 分类: HDU 搜索 2015-06-25 21:12 12人阅读 评论(0) 收藏

    Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  7. 【solr基础教程之二】索引 分类: H4_SOLR/LUCENCE 2014-07-18 21:06 3331人阅读 评论(0) 收藏

    一.向Solr提交索引的方式 1.使用post.jar进行索引 (1)创建文档xml文件 <add> <doc> <field name="id"&g ...

  8. Bzoj 1036 树的统计 分类: ACM TYPE 2014-12-29 18:55 72人阅读 评论(0) 收藏

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...

  9. 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...

随机推荐

  1. 【Android 面试基础知识点整理】

    针对Android面试中常见的一些知识点整理,Max 仅仅是个搬运工.感谢本文中引用文章的各位作者,给大家分享了这么多优秀文章.对于当中的解析,是原作者个人见解,有错误和不准确的地方,也请大家积极指正 ...

  2. 多类别分类问题由 confusion matrix 到分类准确率(accuracy)的计算

    conf_mat = confusionmat(y_true, y_pred); % 首先根据数据集上的真实 label 值,和训练算法给出的预测 label 值, % 计算 confusion ma ...

  3. Flume的Collector

    Collector的作用是将多个Agent的数据汇总后,加载到Storage中.它的source和sink与agent类似. 数据源(source),如: collectorSource[(port) ...

  4. Day4晚笔记

    数据结构 并查集:捆绑两个点的信息,判断对错 倍增:LCA, 字符串 hash,模拟, 最小表示法 给定一个环状字符串,切开,使得字符串的字典序最小 图和树 割点,割边,强联通分量 点双联通分量 (把 ...

  5. MockServer jar包安装

    github地址: https://github.com/jamesdbloom/mockserver 1. org.apache.maven.plugin-tools:maven-plugin-an ...

  6. method initializationerror not found:JUnit4单元測试报错问题

           今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下:            网上说版本 ...

  7. Android Studio中怎样引用图片资源

    随着不断接触Android Studio,越来越发现和Eclipse有着巨大的差别. 不管是表面的目录结构,还是内在对各种资源的管理. 本篇就来聊聊Android Studio中怎样来显示图片. 以及 ...

  8. 字串乱序 PHP&JS

    <?php /** * 字串乱序 PHP&JS * * php 中把字串乱序后输出给客户机的 JAVASCRIPT , JAVASCRIPT 中恢复 * 在指定长度提取一个字符,并把这一 ...

  9. 1.2 Use Cases中 Messaging官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 下面是一些关于Apache kafka 流行的使用场景.这些领域的概述,可查看博客文 ...

  10. Android SocketService

    package com.freer.infusion.module.service; import android.app.ActivityManager; import android.app.Pe ...