搜档网
当前位置:搜档网 › android 自定义控件的过程

android 自定义控件的过程

android 自定义控件的过程
android 自定义控件的过程

android 自定义控件的过程

invalidate()会导致computeScroll()以及onDraw()方法的执行computeScroll()方法是在屏幕流动的时候不停的去调用,scrollTo(int x,int y)则是滚动到相应的位置;

scrollBy(int x, int y)则是移动一些距离,X为正是向左移动,为负时向右移动,Y与X的意义一个,只是是上下移动而已View对象显示在屏幕上,有几个重要步骤:

1.构造方法创建对象

2.测量View的大小onMeasure(int,int);

3.确定View的位置,View自身有一些权,决定权在父View手中. onLayout();基本上不常用,在继承View的时候基本上用不着,但在继承ViewGroup的时候的就要用到了,因为要对View进行布局,确定View的位置,确定的时候使用

指定子View的位置,左,上,右,下,是指在ViewGroup坐标系中的位置https://www.sodocs.net/doc/363013953.html,yout(int xtop,int ytop, int xbottom, int ybottom);

4.绘制View的内容onDraw(Canvas)

实现过程:

1、构造方法:

/**

* 在布局文件中声名的view,创建的时候由系统调用

*

* @param context

* 上下文对象

* @param attrs

* 属性集

*/

public MyToggleButton(Context context, AttributeSet attrs) {

super(context, attrs);

initView();

}

2、测量View的大小:

/**

* 测量尺寸时的回调方法

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

/**

* 设置当前View的大小Width:view的宽度Height: view的调度(单位:像素)

*/

setMeasuredDimension(backgroundBitmap.getWidth(),

backgroundBitmap.getHeight());

}

3、确定View的位置

1、这是继承View的时候,一般不需要进行处理

/**

* 确定位置的时候调用此方法自定义View的时候作用不大

*/

@Override

protected void onLayout(boolean changed, int left, int top, int right,

int bottom) {

super.onLayout(changed, left, top, right, bottom);

}

2、这是继承ViewGroup的时候,这个时候就要确定ViewGroup里面各个View的位置了

/**

* 对子View 进行布局,确定子View的位置changed 若为true ,则说明布局发生了变化l\t\r\b

* 是指当前ViewGroup在其父View中的位置

*/

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

for (int i = 0; i < getChildCount(); i++) {

View view = getChildAt(i);// 依次取得下标为i的子View

// 指定子View的位置,左,上,右,下,是指在ViewGroup坐标系

中的位置

https://www.sodocs.net/doc/363013953.html,yout(i * getWidth(), 0, (i + 1) * getWidth(), getHeight());

}

}

4、绘制View的内容:

@Override

protected void onDraw(Canvas canvas) {

/**

* lefe 图片的左边界top 图片的上边界paint 绘制图片要使用的画笔

*/

// 绘制背景

canvas.drawBitmap(backgroundBitmap, 0, 0, paint);

// 绘制可滑动的按钮

canvas.drawBitmap(slideBtn, slideBtn_left, 0, paint); }

android 自定义圆角头像以及使用declare-styleable进行配置属性解析

android 自定义圆角头像以及使用declare-styleable进行配置属性解析由于最新项目中正在检查UI是否与效果图匹配,结果关于联系人模块给的默认图片是四角稍带弧度的圆角,而我们截取的图片是正方形的,现在要给应用统一替换。应用中既用到大圆角头像(即整个头像是圆的)又用到四角稍带弧度的圆角头像,封装一下以便重用。以下直接见代码 [java] view plain copy 在CODE上查看代码片派生到我的代码片 package com.test.demo; import com.test.demo.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.widget.ImageView; /** * 圆角imageview */ public class RoundImageView extends ImageView { private static final String TAG = "RoundImageView"; /** * 图片的类型,圆形or圆角 */ private int type; public static final int TYPE_CIRCLE = 0; public static final int TYPE_ROUND = 1; /** * 圆角大小的默认值

android studio 控件常用属性

android studio 控件常用属性 下面是RelativeLayout各个属性 1.android:layout_above="@id/xxx" --将控件置于给定ID控件之上 2.android:layout_below="@id/xxx" --将控件置于给定ID控件之下 3. android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐 4.android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐 5. android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐 6.android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐 7.android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐 8.android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐 9.android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐 10. android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐 11. android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐 12.android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐 13.android:layout_centerInParent="true" --将控件置于父控件的中心位置 14.android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置 15.android:layout_centerVertical="true" --将控件置于垂直方向的中心位置 android:layout_width 设置组件的宽度 android:layout_height 设置组件的高度 android:id 给组件定义一个id值,供后期使用 android:background 设置组件的背景颜色或背景图片 android:text 设置组件的显示文字 android:textColor 设置组件的显示文字的颜色 android:layout_below 组件在参考组件的下面 android:alignTop 同指定组件的顶平行

Android平台我的日记设计文档

Android平台我的日记 设计文档 项目名称:mydiray 项目结构示意: 阶段任务名称(一)布局的设计 开始时间: 结束时间: 设计者: 梁凌旭 一、本次任务完成的功能 1、各控件的显示 二、最终功能及效果 三、涉及知识点介绍 四、代码设计 activity_main.xml:

android:layout_centerHorizontal="true" android:layout_marginTop="88dp" android:text="@string/wo" android:textSize="35sp"/>