Android 自定义组件随着手指自动画圆
首先自定义一个View子类:
package com.example.androidtest0.myView; 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 DrawView extends View {
public float currentX = ;
public float currentY = ;
//定义、并创建画笔
Paint p = new Paint();
public DrawView(Context context) {
super(context);
} public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画笔的颜色
p.setColor(Color.RED);
//绘制一个小球
canvas.drawCircle(currentX, currentY, , p);
} /**
* 为该组件的触碰事件重写事件处理方法
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//修改currentX、currentY两个属性
currentX = event.getX();
currentY = event.getY();
//通知当前组件重绘自己
invalidate();
return true;
}
}
主界面XML:
custom_layout.xml
<pre name="code" class="java"><?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:id="@+id/root"
android:orientation="vertical" > </LinearLayout>
主activity文件如下:
package com.example.androidtest0; import com.example.androidtest0.myView.DrawView; import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout; public class CustomView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_layout);
}
}
效果:
除此之外:
还可以用XML的方式:也是首先建一个View的子类,和上面一样。然后主界面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:id="@+id/root"
android:orientation="vertical" > <com.example.androidtest0.myView.DrawView
android:layout_width="match_parent" android:layout_height="match_parent"
/>
</LinearLayout>
主activity文件如下:
package com.example.androidtest0; import com.example.androidtest0.myView.DrawView; import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout; public class CustomView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_layout);
}
}
Android 自定义组件随着手指自动画圆的更多相关文章
- Android自定义组件系列【7】——进阶实践(4)
上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...
- Android自定义组件之自动换行及宽度自适应View:WordWrapView
目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...
- Android自定义组件系列【6】——进阶实践(3)
上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...
- Android自定义组件系列【5】——进阶实践(2)
上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- Android自定义组件
[参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...
- Android自定义Activity酷炫的动画跳转效果
两个Activity跳转的时候,自定义翻页效果: Intent intent = new Intent(FirstActivity.this, SecondActivity.class); sta ...
- Android 自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...
- Android自定义组件系列【3】——自定义ViewGroup实现侧滑
有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
随机推荐
- hibernate篇章四-- Hibernate配置文件中hiberante.hbm2ddl.auto四个参数的配置
我们在搭建环境的时候,在配置文件中有一个属性标签为: <property name="hibernate.hbm2ddl.auto"> </propert ...
- uploadify上传控件使用
uploadify是JQuery的一个上传插件,实现的效果非常不错,并且带进度显示,我将给大家演示如何使用uploadify进行图片上传, 1.点我下载http://www.uploadify.com ...
- Highcharts在IE中不能一次性正常显示的一种解决办法
由于客户要求必须在IE浏览器下兼容图表,故选用了兼容性较好的Highcharts.另外说一句,博主尝试过ichartjs.ECharts.YUI,兼容性都没有Highcharts给力(所有的兼容性问题 ...
- 2.redis.3.2 下载,安装、配置、使用 - 2
上篇简单介绍了 下载,安装,测试,现在直接使用了,看结果 使用的redis服务便是,上篇临时搭建的简易服务,,注意,说的是简易,因为它只是一个单点的“玩具”: 临时在项目登录的时候模拟了一下,这里使用 ...
- 在office 2010中插入Mathtype出现ctrl+v不能复制的问题
加载项中去掉command for那个即可 详细网址:http://heblue.blog.163.com/blog/static/96325568201375102628405/
- Linux搭建SSH服务器
Linux 远程登录服务:ssh ·SSH是标准的网络协议,可用于大多数UNIX操作系统,能够实现字符界面的远程登录管理,它默认使用22号端口,采用密文的形式在网络中传输数据,相对于通过明文传输的Te ...
- weblogic9.2重置密码
1.删除DefaultAuthenticatorInit.ldift 2.执行命令:java -cp /home/weblogic/bea/weblogic92/server/lib/weblogic ...
- 数学符号π (Pi)、Σ(Capital Sigma)、μ (Mu) 、σ(sigma)、∏(capital pi), ∫(Integral Symbol)的来历
1.π (Pi; periphery/周长) March 14 marks Pi Day, the holiday commemorating the mathematical constant π ...
- Codevs 4189 字典(字典树Trie)
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里 ...
- cocos2d-x使用DragonBones动画
前言 在网上查找关于DragonBones在cocos2d-x的使用教程,找了大半天也没有找到一个有用的.在自己摸索了一段时间终于摸索了出来,在这里记下分享给大家. 下载DragonBones 我这里 ...