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 = 40;
public float currentY = 50;
//定义、并创建画笔
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, 15, 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);
//获取布局文件里LinearLayout容器
LinearLayout root = (LinearLayout)findViewById(R.id.root);
//创建DrawView组件
final DrawView drawView = new DrawView(this);
//设置自己定义组件的最小宽度、高度
drawView.setMinimumWidth(10);
drawView.setMinimumHeight(10);
root.addView(drawView);
}
}
效果:
除此之外:
还能够用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中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...
- Android自己定义组件系列【4】——自己定义ViewGroup实现双側滑动
在上一篇文章<Android自己定义组件系列[3]--自己定义ViewGroup实现側滑>中实现了仿Facebook和人人网的側滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布 ...
- Android自己定义组件系列【6】——进阶实践(3)
上一篇<Android自己定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计 ...
- Android自己定义组件系列【5】——进阶实践(2)
上一篇<Android自己定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这 ...
- Android自己定义组件系列【3】——自己定义ViewGroup实现側滑
有关自己定义ViewGroup的文章已经非常多了,我为什么写这篇文章,对于刚開始学习的人或者对自己定义组件比較生疏的朋友尽管能够拿来主义的用了,可是要一步一步的实现和了解当中的过程和原理才干真真脱离别 ...
- Android自己定义组件系列【5】——高级实践(1)
在接下来的几篇文章将任老师的博文<您可以下拉PinnedHeaderExpandableListView实现>骤来具体实现.来学习一下大神的代码并记录一下. 原文出处:http://blo ...
- Android自己定义组件系列【8】——面膜文字动画
我们掩盖文字动画Flash中非经货共同体共同,由于Android应用程序开发人员做你想要做这个动画在应用程序中去?本文中,我们看的是如何自己的定义ImageView来实现让一张文字图片实现文字的遮罩闪 ...
- Android自己定义组件之日历控件-精美日历实现(内容、样式可扩展)
需求 我们知道.Android系统本身有自带的日历控件,网络上也有非常多开源的日历控件资源.可是这些日历控件往往样式较单一.API较多.不易于在实际项目中扩展并实现出符合详细样式风格的,内容可定制的效 ...
- Android自己定义组件系列【1】——自己定义View及ViewGroup
View类是ViewGroup的父类,ViewGroup具有View的全部特性.ViewGroup主要用来充当View的容器.将当中的View作为自己孩子,并对其进行管理.当然孩子也能够是ViewGr ...
随机推荐
- tomcat 日志输出
1.找到tomcat安装目录:cd tomcat/logs 2.tail -f catalina.out 3.ctrl+c 退出
- jquery.slider jquery滑块插件
原文发布时间为:2011-03-08 -- 来源于本人的百度文章 [由搬家工具导入] http://jqueryui.com/demos/slider jquery滑块插件
- DB迁移:从SQL Server 2005到MySQL
一.工具选择 依工作需要进行老产品升级,其中一项重要工作就是将SQL Server数据库改为MySQL数据库,故而在对<各种主流 SQLServer 迁移到 MySQL 工具对比>文章学习 ...
- 用正则表达式模仿Mustache插件的功能
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Python Challenge 第十四关
14关页面上是两张图,一张是一个卷面包,一张类似条形码的东西.没任何提示,就看源代码,果然,有一行注释: <!-- remember: 100*100 = (100+99+99+98) + (. ...
- Python Challenge 第十一关
第十一关,一张模糊的图,题目为 odd even,源代码中也没任何提示,看来又是图像处理. 这张模糊的图看起来没什么头绪,但是题目给了个奇数和偶数,就先试试坐标吧,根据原图来生成一个新图.我第一次尝试 ...
- codevs——1220 数字三角形(棋盘DP)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或 ...
- pandas常用操作
删除某列: concatdfs.drop('Unnamed: 0',axis=1) 打印所有列名: .columns
- mac 下mysql常用命令
是那种单独安装的mysql 启动: /usr/local/mysql/bin/mysql -u root -p
- ssh配置含义解释
ssh包括客户端配置(ssh_config)和服务端配置(sshd_config) 一.客户端配置ssh_config,客户端软件有SecureCRT.putty.Xshell等 “#”表示注释,虽然 ...