View类是ViewGroup的父类,ViewGroup具有View的全部特性。ViewGroup主要用来充当View的容器。将当中的View作为自己孩子,并对其进行管理。当然孩子也能够是ViewGroup类型。

View类一般用于画图操作,重写它的onDraw方法,但它不能够包括其它组件,没有addView(View view)方法。

ViewGroup是一个组件容器,它能够包括不论什么组件,但必须重写onLayout(boolean changed,int l,int t,int r,int b)和onMesure(int widthMesureSpec,int heightMesureSpec)方法. 否则ViewGroup中加入组件是不会显示的。

package com.example.testrefreshview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyViewGroup(this)); } public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context) {
super(context);
Button button1 = new Button(context);
button1.setText("button1"); Button button2 = new Button(context);
button2.setText("button2"); TextView textView = new TextView(context);
textView.setText("textView"); addView(button1);
addView(button2);
addView(textView);
} @Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3,
int arg4) { }
}
}

View的layout(int left,int top,int right,int bottom)方法负责把该view放在參数指定位置,所以如果我们在自己定义的ViewGroup::onLayout中遍历每个子view并用view.layout()指定其位置。每个子View又会调用onLayout,这就构成了一个递归调用的过程

如果在ViewGroup中重写onDraw方法。须要在构造方法中调用this.setWillNoDraw(flase); 此时,系统才会调用重写过的onDraw(Canvas cancas)方法。否则系统不会调用onDraw(Canvas canvas)方法.

将上面代码改动一下,就能够显示出来。

package com.example.testrefreshview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyViewGroup(this)); } public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context) {
super(context);
Button button1 = new Button(context);
button1.setText("button1"); Button button2 = new Button(context);
button2.setText("button2"); TextView textView = new TextView(context);
textView.setText("textView"); addView(button1);
addView(button2);
addView(textView);
} @Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3,
int arg4) {
int childCount = getChildCount();
int left = 0;
int top = 10;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
child.layout(left, top, left + 60, top + 60);
top += 70;
}
}
}
}

再看一段代码:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childCount = getChildCount();
//设置该ViewGroup的大小
int specSize_width = MeasureSpec.getSize(widthMeasureSpec);
int specSize_height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(specSize_width, specSize_height); for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
childView.measure(80, 80);
}
}

通过重写onMeasure方法不但能够为ViewGroup指定大小,还能够通过遍历为每个子View指定大小。在自己定义ViewGroup中加入上面代码为ViewGroup中的每个子View分配了显示的宽高。 

以下我们让子View动起来吧。加入代码例如以下:

public boolean onTouchEvent(MotionEvent ev) {
final float y = ev.getY();
switch(ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
int detaY = (int)(mLastMotionY - y);
mLastMotionY = y;
scrollBy(0, detaY);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}

在上面用到了一个scrollBy方法,打开官方API能够看到View类有例如以下两个方法:


这两个函数貌似都是移动视图的。那么它们有什么差别呢?带着这个疑问我们向下看

首先 ,我们必须明确在Android View视图是没有边界的,Canvas是没有边界的,仅仅只是我们通过绘制特定的View时对 Canvas对象进行了一定的操作。比如 : translate(平移)、clipRect(剪切)等,以便达到我们的对该Canvas对象绘制的要求 。我们能够将这样的无边界的视图称为“视图坐标”-----它不受物理屏幕限制。通常我们所理解的一个Layout布局文件仅仅是该视图的显示区域,超过了这个显示区域将不能显示到父视图的区域中 ,相应的,我们能够将这样的有边界的视图称为“布局坐标”------ 父视图给子视图分配的布局(layout)大小。并且, 一个视图的在屏幕的起始坐标位于视图坐标起始处。例如以下图所看到的。


由于布局坐标仅仅能显示特定的一块内容。所以我们仅仅有移动布局坐标的坐标原点就能够将视图坐标的不论什么位置显示出来。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#888888"> <TextView
android:id="@+id/txt"
android:layout_width="300dip"
android:layout_height="120dip"
android:background="#cccccc"
android:text="textview" /> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>


当我点击按钮触发事件后例如以下:


上面代码中触发点击事件后,运行了textView.scrollTo(-200, -100);scrollTo中的两个參数的含义不是坐标位置。而是相对于视图坐标位置的偏移量,如果我们要移动到(200,100)的位置则偏移量为(0,0)-(200,100) = (-200。 -100)。

如果我们将上面代码换成scrollBy则会发现点击多次按钮后textview就会移出可见界面,这是由于scrollBy是相对我们当前坐标进行偏移。

以下我们来看看源码中对这两个方法是怎样实现的:

    /**
* Set the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
*/
public void scrollTo(int x, int y) {
if (mScrollX != x || mScrollY != y) {
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (!awakenScrollBars()) {
invalidate(true);
}
}
}

能够看到 mScrollX = x; mScrollY = y; 

    /**
* Move the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
*/
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}

能够看到 mScrollX + x, mScrollY + y;

mScrollX和mScrollY是相对于视图坐标的当前偏移量。





Android自己定义组件系列【1】——自己定义View及ViewGroup的更多相关文章

  1. Android自己定义组件系列【7】——进阶实践(4)

    上一篇<Android自己定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...

  2. Android自己定义组件系列【6】——进阶实践(3)

    上一篇<Android自己定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计 ...

  3. Android自己定义组件系列【5】——进阶实践(2)

    上一篇<Android自己定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这 ...

  4. Android自己定义组件系列【4】——自己定义ViewGroup实现双側滑动

    在上一篇文章<Android自己定义组件系列[3]--自己定义ViewGroup实现側滑>中实现了仿Facebook和人人网的側滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布 ...

  5. Android自己定义组件系列【3】——自己定义ViewGroup实现側滑

    有关自己定义ViewGroup的文章已经非常多了,我为什么写这篇文章,对于刚開始学习的人或者对自己定义组件比較生疏的朋友尽管能够拿来主义的用了,可是要一步一步的实现和了解当中的过程和原理才干真真脱离别 ...

  6. Android自己定义组件系列【2】——Scroller类

    在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...

  7. Android自己定义组件系列【9】——Canvas绘制折线图

    有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了非常多插件,可是非常多时候我们须要依据详细项目自己定义这些图表,这一篇文章我们一起来看看怎样在Android中使用Can ...

  8. Android自己定义组件系列【5】——高级实践(1)

    在接下来的几篇文章将任老师的博文<您可以下拉PinnedHeaderExpandableListView实现>骤来具体实现.来学习一下大神的代码并记录一下. 原文出处:http://blo ...

  9. Android自己定义组件系列【8】——面膜文字动画

    我们掩盖文字动画Flash中非经货共同体共同,由于Android应用程序开发人员做你想要做这个动画在应用程序中去?本文中,我们看的是如何自己的定义ImageView来实现让一张文字图片实现文字的遮罩闪 ...

随机推荐

  1. Java系列学习(十三)-字符串

    1.字符串基础 概念:字符串本质是打包字符数组的对象,是java.lang.String类的实例 2.字符串的构造方法 public String() public String(byte[] byt ...

  2. nginx下如何配置 ssl证书?腾讯云ssl证书为例!

    nginx下如何配置 ssl证书?腾讯云ssl证书为例! 目前为止,https已经成为一种趋势,想要开启https就需要ssl证书. 首先,为域名注册ssl证书. 腾讯云注册地址:https://cl ...

  3. 第一次android混淆实战

    第一次混淆,主要是因为引用本地第三方jar包的问题.虽然说本地第三方jar包自动避免混淆,但一些本地第三方jar包下的一些包要避免混淆.比如: 文中的com.org 这些包名都要避免混淆. 下面是我用 ...

  4. Ajax——异步基础知识(三)

    封装异步请求 1.将函数作为参数进行使用 2.因为获取数据是在一个注册事件中获取的,所以只有事件触发的时候才会调用此函数 <!DOCTYPE html> <html lang=&qu ...

  5. Linux内存压力测试-memtester工具

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. 03--QT教程(转自:豆子)

    http://blog.51cto.com/zt/20

  7. (转)分布式文件存储FastDFS(五)FastDFS常用命令总结

    http://blog.csdn.net/xingjiarong/article/details/50561471 1.启动FastDFS tracker: /usr/local/bin/fdfs_t ...

  8. comdlg32.dll

    dll的应用,目前还不知道要怎么查看dll里的功能,暂且试着用了一个, 下面的Declare 分32位office软件和64位,如果是64位,要在Declare 后面加上PtrSafe ,定义的Typ ...

  9. (5.2.1)配置服务器参数——即时文件初始化(IFI)

    关键词:零填充,即时文件初始化 转自:https://www.cnblogs.com/gaizai/p/3516905.html 概念: 所有新申请的空间,sql server都要以0来填充完磁盘文件 ...

  10. ZOJ 3180 Number Game(模拟,倒推)

    题目 思路: 先倒推!到最后第二步,然后: 初始状态不一定满足这个状态.所以我们要先从初始状态构造出它出发的三种状态.那这三种状态跟倒推得到的状态比较即可. #include<stdio.h&g ...