自定义view组件
参考《疯狂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组件的更多相关文章
- Android Studio开发基础之自定义View组件
一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...
- 自定义view组件 分类: H1_ANDROID 2013-10-26 21:55 741人阅读 评论(0) 收藏
参考<疯狂android讲义>第2版 2.1节P48,对应CustomViewDemo.zip. 若在开发过程中,发现现有的view均不能满足需要,可以自定义一个view. 自定义一个vi ...
- Android中实现自定义View组件并使其能跟随鼠标移动
场景 实现效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建An ...
- 【Android 应用开发】自定义View 和 ViewGroup
一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...
- 安卓程序代写 网上程序代写[原]自定义View
一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...
- 自定义View 和 ViewGroup
一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...
- 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件
Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...
- Android之自定义View的实现
对于学习Android开发的小童鞋对于自定义View一定不会陌生,相信大家对它是又爱又恨,爱它可以跟随我们的心意设计出漂亮的效果:恨它想要完全流畅掌握,需要一定的功夫.对于初学者来说确实很不容易,网上 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
随机推荐
- eclipse 添加jar包的方式
参考资料地址:http://blog.csdn.net/mazhaojuan/article/details/21403717
- C# post请求 HttpWebRequest
//body是要传递的参数,格式"roleId=1&uid=2" //post的cotentType填写: //"application/x-www-form-u ...
- linux运维基础__争取十月前研究的差不多
转来的一编,考虑在十月前研究的差不多 linux运维人员基础 1.很多地方经常会用到的rsync工具 实施几台服务器的同步效果 我们公司就是使用这个工具完成服务器的游戏的服务端和客户端同步,有几个文章 ...
- iOS基本的发短信和打电话调用
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- SIM卡基础,各管脚意义,封装定义
1. SIM简介 SIM卡(Subscriber Identity Module).即用户识别模块,是一张符合GSM规范的"智慧卡".SIM卡可以插入任何一部符合GSM规范的移动电 ...
- The Flat Dictionary
The Flat Dictionary 原来的代码没处理dict为空的情况 1 def flatten(dictionary): 2 #[] is a list 3 #() is a tuple 4 ...
- 【转】使用adb命令对手机进行截屏(截图)保存到电脑,SDCard
原文网址:http://blog.csdn.net/huangyabin001/article/details/29198367 adb shell /system/bin/screencap -p ...
- 【剑指offer】面试题30:最小的 k 个数
题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 这个是O(nlogk)时间复杂度的思路:用一个容器来保存最先 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ
2.4.4 堆的算法 我们用长度为 N + 1的私有数组pq[]来表示一个大小为N的堆,我们不会使用pq[0],堆元素放在pq[1]至pq[N]中.在排序算法中,我们只能通过私有辅助函数less()和 ...
- How to run OFBiz as a Service on linux
Windows See this specific guide: How to Run OFBiz as Windows Service with Java Service Wrapper Linux ...