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 ...
随机推荐
- FFT与NTT
讲解:http://www.cnblogs.com/poorpool/p/8760748.html 递归版FFT #include <iostream> #include <cstd ...
- EasyUI-Calendar
EasyUI-Calendar 日历篇 Calendar也是页面中经常用到的元素,easyui也为们提供了日历的组件,效果如下图所示: 使用方法如下所示: <div class="ea ...
- java网络编程学习笔记(三):ServerSocket详解
1.ServerSocket的构造方法 ServerSocket(); ServerSocket(int port); ServerSocket(int port,int backlog); Serv ...
- [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Linux signal 那些事儿(2)【转】
转自:http://blog.chinaunix.net/uid-24774106-id-4064447.html 上一篇博文,基本算是给glibc的signal函数翻了个身.现在glibc的sign ...
- python commands 模块
commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...
- 通过ansible一键部署集群ntp时间同步
环境准备 [root@server ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@server ~]# uname -r ...
- 在本地(自己电脑上)部署了tomcat服务器,真机测试遇到的问题
开始的时候自己就是给app搭建了一个小的框架,只有一个界面发送了网络请求,部署的tomcat,数据成功请求,得到了数据. 后来随着联网请求的增多,突然发现联网请求一直失败.自己dubug了最开始的第一 ...
- WEB接口测试之Jmeter接口测试自动化 (一)(初次接触)
软件测试自动化从不同的测试阶段分类,可从下层到上层依次分为单元测试-->接口测试-->界面自动化测试. 单元测试一般有开发人员自行完成,而界面自动化测试合适的测试条件又很难达到,测试人员在 ...
- Loj #6287 诗歌
link: https://loj.ac/problem/6287 一开始差点写FFT了23333,并且FFT还能算这样的三元组的数量而且还不用要求这是一个排列.... 但这太大材小用了(而且很可能被 ...