androiddeviceattr
1. android自定义控件,一般遵循什么样的原则
开发自定义控件的步骤:
1、了解View的工作原理
2、 编写继承自View的子类
3、 为自定义View类增加属性
4、 绘制控件
5、 响应用户消息
6 、自定义回调函数
一、View结构原理
Android系统的视图结构的设计也采用了组合模式,即View作为所有图形的基类,Viewgroup对View继承扩展为视图容器类。
View定义了绘图的基本操作
基本操作由三个函数完成:measure()、layout()、draw(),其内部又分别包含了onMeasure()、onLayout()、onDraw()三个子方法。具体操作如下:
1、measure操作
measure操作主要用于计算视图的大小,即视图的宽度和长度。在view中定义为final类型,要求子类不能修改。measure()函数中又会调用下面的函数:
(1)onMeasure(),视图大小的将在这里最终确定,也就是说measure只是对onMeasure的一个包装,子类可以覆写onMeasure()方法实现自己的计算视图大小的方式,并通过setMeasuredDimension(width, height)保存计算结果。
2、layout操作
layout操作用于设置视图在屏幕中显示的位置。在view中定义为final类型,要求子类不能修改。layout()函数中有两个基本操作:
(1)setFrame(l,t,r,b),l,t,r,b即子视图在父视图中的具体位置,该函数用于将这些参数保存起来;
(2)onLayout(),在View中这个函数什么都不会做,提供该函数主要是为viewGroup类型布局子视图用的;
3、draw操作
draw操作利用前两部得到的参数,将视图显示在屏幕上,到这里也就完成了整个的视图绘制工作。子类也不应该修改该方法,因为其内部定义了绘图的基本操作:
(1)绘制背景;
(2)如果要视图显示渐变框,这里会做一些准备工作;
(3)绘制视图本身,即调用onDraw()函数。在view中onDraw()是个空函数,也就是说具体的视图都要覆写该函数来实现自己的显示(比如TextView在这里实现了绘制文字的过程)。而对于ViewGroup则不需要实现该函数,因为作为容器是“没有内容“的,其包含了多个子view,而子View已经实现了自己的绘制方法,因此只需要告诉子view绘制自己就可以了,也就是下面的dispatchDraw()方法;
(4)绘制子视图,即dispatchDraw()函数。在view中这是个空函数,具体的视图不需要实现该方法,它是专门为容器类准备的,也就是容器类必须实现该方法;
(5)如果需要(应用程序调用了setVerticalFadingEdge或者setHorizontalFadingEdge),开始绘制渐变框;
(6)绘制滚动条;
从上面可以看出自定义View需要最少覆写onMeasure()和onDraw()两个方法。
二、View类的构造方法
创建自定义控件的3种主要实现方式:
1)继承已有的控件来实现自定义控件: 主要是当要实现的控件和已有的控件在很多方面比较类似, 通过对已有控件的扩展来满足要求。
2)通过继承一个布局文件实现自定义控件,一般来说做组合控件时可以通过这个方式来实现。
注意此时不用onDraw方法,在构造广告中通过inflater加载自定义控件的布局文件,再addView(view),自定义控件的图形界面就加载进来了。
3)通过继承view类来实现自定义控件,使用GDI绘制出组件界面,一般无法通过上述两种方式来实现时用该方式。
View(Context context)
Simple constructor to use when creating a view from code.
View(Context context, AttributeSet attrs)
Constructor that is called when inflating a view from XML.
View(Context context, AttributeSet attrs, int defStyle)
Perform inflation from XML and apply a class-specific base style.
三、自定义View增加属性的两种方法:
1)在View类中定义。通过构造函数中引入的AttributeSet 去查找XML布局的属性名称,然后找到它对应引用的资源ID去找值。
案例:实现一个带文字的图片(图片、文字是onDraw方法重绘实现)
public class MyView extends View {
private String mtext;
private int msrc;
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
int resourceId = 0;
int textId = attrs.getAttributeResourceValue(null, "Text",0);
int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
mtext = context.getResources().getText(textId).toString();
msrc = srcId;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setColor(Color.RED);
InputStream is = getResources().openRawResource(msrc);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, 0,0, paint);
//canvas.drawCircle(40, 90, 15, paint);
canvas.drawText(mtext, bw/2, 30, paint);
}
}
布局文件:
<?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:orientation="vertical" >
<com.example.myimageview2.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Text="@string/hello_world"
Src="@drawable/xh"/>
</LinearLayout>
属性Text, Src在自定义View类的构造方法中读取。
2)通过XML为View注册属性。与Android提供的标准属性写法一样。
案例: 实现一个带文字说明的ImageView (ImageView+TextView组合,文字说明,可在布局文件中设置位置)
public class MyImageView extends LinearLayout {
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
int resourceId = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.MyImageView);
ImageView iv = new ImageView(context);
TextView tv = new TextView(context);
int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.MyImageView_Oriental:
resourceId = typedArray.getInt(
R.styleable.MyImageView_Oriental, 0);
this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
: LinearLayout.VERTICAL);
break;
case R.styleable.MyImageView_Text:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Text, 0);
tv.setText(resourceId > 0 ? typedArray.getResources().getText(
resourceId) : typedArray
.getString(R.styleable.MyImageView_Text));
break;
case R.styleable.MyImageView_Src:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Src, 0);
iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
break;
}
}
addView(iv);
addView(tv);
typedArray.recycle();
}
}
attrs.xml进行属性声明, 文件放在values目录下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyImageView">
<attr name="Text" format="reference|string"></attr>
<attr name="Oriental" >
<enum name="Horizontal" value="1"></enum>
<enum name="Vertical" value="0"></enum>
</attr>
<attr name="Src" format="reference|integer"></attr>
</declare-styleable>
</resources>
MainActivity的布局文件:先定义命名空间 xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
然后可以像使用系统的属性一样使用:uview:Oriental="Vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.example.myimageview2.MyImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
uview:Text="这是一个图片说明"
uview:Src="@drawable/tw"
uview:Oriental="Vertical">
</com.example.myimageview2.MyImageView>
</LinearLayout>
四、控件绘制 onDraw()
五、
六、自定义View的方法
onFinishInflate() 回调方法,当应用从XML加载该组件并用它构建界面之后调用的方法
onMeasure() 检测View组件及其子组件的大小
onLayout() 当该组件需要分配其子组件的位置、大小时
onSizeChange() 当该组件的大小被改变时
onDraw() 当组件将要绘制它的内容时
onKeyDown 当按下某个键盘时
onKeyUp 当松开某个键盘时
onTrackballEvent 当发生轨迹球事件时
onTouchEvent 当发生触屏事件时
onWindowFocusChanged(boolean) 当该组件得到、失去焦点时
onAtrrachedToWindow() 当把该组件放入到某个窗口时
onDetachedFromWindow() 当把该组件从某个窗口上分离时触发的方法
onWindowVisibilityChanged(int): 当包含该组件的窗口的可见性发生改变时触发的方法
2. android 角标红点怎么设置
角标实现
要实现这个角标:
1、放置一个隐藏的图片在app中,在需要它显示的时候,显示该图片,并以该图片为背景,显示新增消息数;
2、自定义一个控件,用于显示该角标信息。例如,角标相对于控件的位置、底色、数字等;
对比或者使用后,你会发现,自定义一个控件,无疑更符合我们的使用习惯。无需特定的图片,减少了app的大小等,更重要的是,这样方便我们的使用。
在角标实现中,有个开源代码BadgeView写的很符合我们的心理预期。实现了我们常用的所有功能。
BadgeView
这是一个继承TextView控件,自定义而成的一个简单控件。我们通过它,可轻易实现对角标位置、角标底色、角标内容等控制。
java">
packagecom.readystatesoftware.viewbadger;
importandroid.content.Context;
importandroid.content.res.Resources;
importandroid.graphics.Color;
importandroid.graphics.Typeface;
importandroid.graphics.drawable.ShapeDrawable;
importandroid.graphics.drawable.shapes.RoundRectShape;
importandroid.util.AttributeSet;
importandroid.util.TypedValue;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.ViewGroup.LayoutParams;
importandroid.view.ViewParent;
importandroid.view.animation.AccelerateInterpolator;
importandroid.view.animation.AlphaAnimation;
importandroid.view.animation.Animation;
importandroid.view.animation.DecelerateInterpolator;
importandroid.widget.FrameLayout;
importandroid.widget.TabWidget;
importandroid.widget.TextView;
/**
*"badge"toanygiven{@linkandroid.view.View}.
*outs.
*
*@authorJeffGilfelt
*/
{
publicstaticfinalintPOSITION_TOP_LEFT=1;
publicstaticfinalintPOSITION_TOP_RIGHT=2;
publicstaticfinalintPOSITION_BOTTOM_LEFT=3;
publicstaticfinalintPOSITION_BOTTOM_RIGHT=4;
publicstaticfinalintPOSITION_CENTER=5;
privatestaticfinalintDEFAULT_MARGIN_DIP=5;
privatestaticfinalintDEFAULT_LR_PADDING_DIP=5;
privatestaticfinalintDEFAULT_CORNER_RADIUS_DIP=8;
privatestaticfinalintDEFAULT_POSITION=POSITION_TOP_RIGHT;
privatestaticfinalintDEFAULT_BADGE_COLOR=Color.parseColor("#CCFF0000");//Color.RED;
privatestaticfinalintDEFAULT_TEXT_COLOR=Color.WHITE;
privatestaticAnimationfadeIn;
privatestaticAnimationfadeOut;
privateContextcontext;
privateViewtarget;
privateintbadgePosition;
privateintbadgeMarginH;
privateintbadgeMarginV;
privateintbadgeColor;
privatebooleanisShown;
privateShapeDrawablebadgeBg;
privateinttargetTabIndex;
publicBadgeView(Contextcontext){
this(context,(AttributeSet)null,android.R.attr.textViewStyle);
}
publicBadgeView(Contextcontext,AttributeSetattrs){
this(context,attrs,android.R.attr.textViewStyle);
}
/**
*Constructor-
*
*{@linkandroid.view.View}.
*
*@.
*@.
*/
publicBadgeView(Contextcontext,Viewtarget){
this(context,null,android.R.attr.textViewStyle,target,0);
}
/**
*Constructor-
*
*{@linkandroid.widget.TabWidget}
*tabatagivenindex.
*
*@.
*@.
*@.
*/
publicBadgeView(Contextcontext,TabWidgettarget,intindex){
this(context,null,android.R.attr.textViewStyle,target,index);
}
publicBadgeView(Contextcontext,AttributeSetattrs,intdefStyle){
this(context,attrs,defStyle,null,0);
}
publicBadgeView(Contextcontext,AttributeSetattrs,intdefStyle,Viewtarget,inttabIndex){
super(context,attrs,defStyle);
init(context,target,tabIndex);
}
privatevoidinit(Contextcontext,Viewtarget,inttabIndex){
this.context=context;
this.target=target;
this.targetTabIndex=tabIndex;
//applydefaults
badgePosition=DEFAULT_POSITION;
badgeMarginH=dipToPixels(DEFAULT_MARGIN_DIP);
badgeMarginV=badgeMarginH;
badgeColor=DEFAULT_BADGE_COLOR;
setTypeface(Typeface.DEFAULT_BOLD);
intpaddingPixels=dipToPixels(DEFAULT_LR_PADDING_DIP);
setPadding(paddingPixels,0,paddingPixels,0);
setTextColor(DEFAULT_TEXT_COLOR);
fadeIn=newAlphaAnimation(0,1);
fadeIn.setInterpolator(newDecelerateInterpolator());
fadeIn.setDuration(200);
fadeOut=newAlphaAnimation(1,0);
fadeOut.setInterpolator(newAccelerateInterpolator());
fadeOut.setDuration(200);
isShown=false;
if(this.target!=null){
applyTo(this.target);
}else{
show();
}
}
privatevoidapplyTo(Viewtarget){
LayoutParamslp=target.getLayoutParams();
ViewParentparent=target.getParent();
FrameLayoutcontainer=newFrameLayout(context);
if(targetinstanceofTabWidget){
//
target=((TabWidget)target).getChildTabViewAt(targetTabIndex);
this.target=target;
((ViewGroup)target).addView(container,
newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
this.setVisibility(View.GONE);
container.addView(this);
}else{
//
ViewGroupgroup=(ViewGroup)parent;
intindex=group.indexOfChild(target);
group.removeView(target);
group.addView(container,index,lp);
container.addView(target);
this.setVisibility(View.GONE);
container.addView(this);
group.invalidate();
}
}
/**
*MakethebadgevisibleintheUI.
*
*/
publicvoidshow(){
show(false,null);
}
/**
*MakethebadgevisibleintheUI.
*
*@-inanimation.
*/
publicvoidshow(booleananimate){
show(animate,fadeIn);
}
/**
*MakethebadgevisibleintheUI.
*
*@.
*/
publicvoidshow(Animationanim){
show(true,anim);
}
/**
*Makethebadgenon-visibleintheUI.
*
*/
publicvoidhide(){
hide(false,null);
}
/**
*Makethebadgenon-visibleintheUI.
*
*@-outanimation.
*/
publicvoidhide(booleananimate){
hide(animate,fadeOut);
}
/**
*Makethebadgenon-visibleintheUI.
*
*@-visible.
*/
publicvoidhide(Animationanim){
hide(true,anim);
}
/**
*.
*
*/
publicvoidtoggle(){
toggle(false,null,null);
}
/**
*.
*
*@-in/outanimation.
*/
publicvoidtoggle(booleananimate){
toggle(animate,fadeIn,fadeOut);
}
/**
*.
*
*@.
*@-visible.
*/
publicvoidtoggle(AnimationanimIn,AnimationanimOut){
toggle(true,animIn,animOut);
}
privatevoidshow(booleananimate,Animationanim){
if(getBackground()==null){
if(badgeBg==null){
badgeBg=getDefaultBackground();
}
setBackgroundDrawable(badgeBg);
}
applyLayoutParams();
if(animate){
this.startAnimation(anim);
}
this.setVisibility(View.VISIBLE);
isShown=true;
}
privatevoidhide(booleananimate,Animationanim){
this.setVisibility(View.GONE);
if(animate){
this.startAnimation(anim);
}
isShown=false;
}
privatevoidtoggle(booleananimate,AnimationanimIn,AnimationanimOut){
if(isShown){
hide(animate&&(animOut!=null),animOut);
}else{
show(animate&&(animIn!=null),animIn);
}
}
/**
*Incrementthenumericbadgelabel.
*anintegervalue,itslabelwillbesetto"0".
*
*@paramoffsettheincrementoffset.
*/
publicintincrement(intoffset){
CharSequencetxt=getText();
inti;
if(txt!=null){
try{
i=Integer.parseInt(txt.toString());
}catch(NumberFormatExceptione){
i=0;
}
}else{
i=0;
}
i=i+offset;
setText(String.valueOf(i));
returni;
}
/**
*Decrementthenumericbadgelabel.
*anintegervalue,itslabelwillbesetto"0".
*
*@paramoffsetthedecrementoffset.
*/
publicintdecrement(intoffset){
returnincrement(-offset);
}
(){
intr=dipToPixels(DEFAULT_CORNER_RADIUS_DIP);
float[]outerR=newfloat[]{r,r,r,r,r,r,r,r};
RoundRectShaperr=newRoundRectShape(outerR,null,null);
ShapeDrawabledrawable=newShapeDrawable(rr);
drawable.getPaint().setColor(badgeColor);
returndrawable;
}
privatevoidapplyLayoutParams(){
FrameLayout.LayoutParamslp=newFrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
switch(badgePosition){
casePOSITION_TOP_LEFT:
lp.gravity=Gravity.LEFT|Gravity.TOP;
lp.setMargins(badgeMarginH,badgeMarginV,0,0);
break;
casePOSITION_TOP_RIGHT:
lp.gravity=Gravity.RIGHT|Gravity.TOP;
lp.setMargins(0,badgeMarginV,badgeMarginH,0);
break;
casePOSITION_BOTTOM_LEFT:
lp.gravity=Gravity.LEFT|Gravity.BOTTOM;
lp.setMargins(badgeMarginH,0,0,badgeMarginV);
break;
casePOSITION_BOTTOM_RIGHT:
lp.gravity=Gravity.RIGHT|Gravity.BOTTOM;
lp.setMargins(0,0,badgeMarginH,badgeMarginV);
break;
casePOSITION_CENTER:
lp.gravity=Gravity.CENTER;
lp.setMargins(0,0,0,0);
break;
default:
break;
}
setLayoutParams(lp);
}
/**
*.
*
*/
publicViewgetTarget(){
returntarget;
}
/**
*?
*
*/
@Override
publicbooleanisShown(){
returnisShown;
}
/**
*.
*
*oneofPOSITION_TOP_LEFT,POSITION_TOP_RIGHT,POSITION_BOTTOM_LEFT,POSITION_BOTTOM_RIGHT,POSTION_CENTER.
*
*/
publicintgetBadgePosition(){
returnbadgePosition;
}
/**
*Setthepositioningofthisbadge.
*
*@_TOP_LEFT,POSITION_TOP_RIGHT,POSITION_BOTTOM_LEFT,POSITION_BOTTOM_RIGHT,POSTION_CENTER.
*
*/
publicvoidsetBadgePosition(intlayoutPosition){
this.badgePosition=layoutPosition;
}
/**
*.
*
*/
(){
returnbadgeMarginH;
}
/**
*.
*
*/
(){
returnbadgeMarginV;
}
/**
*Setthehorizontal/.
*
*@.
*/
publicvoidsetBadgeMargin(intbadgeMargin){
this.badgeMarginH=badgeMargin;
this.badgeMarginV=badgeMargin;
}
/**
*Setthehorizontal/.
*
*@paramhorizontalmargininpixels.
*@paramverticalmargininpixels.
*/
publicvoidsetBadgeMargin(inthorizontal,intvertical){
this.badgeMarginH=horizontal;
this.badgeMarginV=vertical;
}
/**
*.
*
*/
(){
returnbadgeColor;
}
/**
*.
*
*@.
*/
(intbadgeColor){
this.badgeColor=badgeColor;
badgeBg=getDefaultBackground();
}
privateintdipToPixels(intdip){
Resourcesr=getResources();
floatpx=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,r.getDisplayMetrics());
return(int)px;
}
}
3. Android开发中,长按事件怎么获得屏幕坐标点
对于很多游戏使用屏幕控制一般需要考虑长按事件,比如在动作类的游戏中需要长按发射武器,结合Android Button模型,我们实现一个带图片的Button的长按,为了更清晰的显示原理,Android开发网这里使用ImageButton作为基类.
public class RepeatingImageButton extends ImageButton {
private long mStartTime; //记录长按开始
private int mRepeatCount; //重复次数计数
private RepeatListener mListener;
private long mInterval = 500; //Timer触发间隔,即每0.5秒算一次按下
public RepeatingImageButton(Context context) {
this(context, null);
}
public RepeatingImageButton(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.imageButtonStyle);
}
public RepeatingImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true); //允许获得焦点
setLongClickable(true); //启用长按事件
}
public void setRepeatListener(RepeatListener l, long interval) { //实现重复按下事件listener
mListener = l;
mInterval = interval;
}
@Override
public boolean performLongClick() {
mStartTime = SystemClock.elapsedRealtime();
mRepeatCount = 0;
post(mRepeater);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) { // 本方法原理同onKeyUp的一样,这里处理屏幕事件,下面的onKeyUp处理Android手机上的物理按键事件
removeCallbacks(mRepeater);
if (mStartTime != 0) {
doRepeat(true);
mStartTime = 0;
}
}
return super.onTouchEvent(event);
}
//处理导航键事件的中键或轨迹球按下事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
super.onKeyDown(keyCode, event);
return true;
}
return super.onKeyDown(keyCode, event);
}
//当按键弹起通知长按结束
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
removeCallbacks(mRepeater); //取消重复listener捕获
if (mStartTime != 0) {
doRepeat(true); //如果长按事件累计时间不为0则说明长按了
mStartTime = 0; //重置长按计时器
}
}
return super.onKeyUp(keyCode, event);
}
private Runnable mRepeater = new Runnable() { //在线程中判断重复
public void run() {
doRepeat(false);
if (isPressed()) {
postDelayed(this, mInterval); //计算长按后延迟下一次累加
}
}
};
private void doRepeat(boolean last) {
long now = SystemClock.elapsedRealtime();
if (mListener != null) {
mListener.onRepeat(this, now - mStartTime, last ? -1 : mRepeatCount++);
}
}
下面是重复Button Listener接口的定义,调用时在Button中先使用setRepeatListener()方法实现RepeatListener接口
public interface RepeatListener {
void onRepeat(View v, long ration, int repeatcount); //参数一为用户传入的Button对象,参数二为延迟的毫秒数,第三位重复次数回调。
}
}
本类大家可以直接在自己的View中implements实现RepeatListener接口即可.
4. android 自定义switch样式
修改后的MySwitch控件接口基本与原Switch控件一致,并且除了可支持所有SDK外,增加了2项小功能:
1. 支持用Track背景图片的方式代替Texton Textoff等文字方式表现开关状态
2.支持调整控制Switch的高度
下面贴出Switch修改的关键代码:
/**
* <p>
* modified from android SDK 14(4.0) android.widget.Switch.
* <br/>
* <strong>new feature: </strong>
* <ol>
* <li>support SDK 1 or higher. </li>
* <li>you can use track drawable instead of text to display the changes of off-on state!</li>
* <li>you can control the Switch minimum height. </li>
* </ol>
* </p>
*
* @see {@link Switch}
* @author Wison
*/
public class MySwitch extends CompoundButton {
// Support track drawable instead of text
private Drawable mTrackOnDrawable;
private Drawable mTrackOffDrawable;
// Support minimum height
private int mSwitchMinHeight;
/**
* Construct a new Switch with a default style determined by the given theme attribute,
* overriding specific style attributes as requested.
*
* @param context The Context that will determine this widget's theming.
* @param attrs Specification of attributes that should deviate from the default styling.
* @param defStyle An attribute ID within the active theme containing a reference to the
* default style for this widget. e.g. android.R.attr.switchStyle.
*/
public MySwitch(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
Resources res = getResources();
mTextPaint.density = res.getDisplayMetrics().density;
//float scaledDensity = res.getDisplayMetrics().scaledDensity;
//mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Switch, defStyle, 0);
// off-on 模式: 图片模式或文字模式,图片模式是用Track背景图片表示off-on的状态,文字模式是用文字来表示off-on状态。
mTrackOnDrawable = a.getDrawable(R.styleable.Switch_trackOn);
mTrackOffDrawable = a.getDrawable(R.styleable.Switch_trackOff);
if (checkTrackOffOnDrawable()) {
// 如果设定图片模式,则默认显示off状态
mTrackDrawable = mTrackOffDrawable;
} else {
mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
}
mThumbDrawable = a.getDrawable(R.styleable.Switch_thumb);
mTextOn = a.getText(R.styleable.Switch_textOn);
mTextOff = a.getText(R.styleable.Switch_textOff);
mThumbTextPadding = a.getDimensionPixelSize(R.styleable.Switch_thumbTextPadding, 0);
mSwitchMinWidth = a.getDimensionPixelSize(R.styleable.Switch_switchMinWidth, 0);
mSwitchMinHeight = a.getDimensionPixelSize(R.styleable.Switch_switchMinHeight, 0);
mSwitchPadding = a.getDimensionPixelSize(R.styleable.Switch_switchPadding, 0);
int appearance = a.getResourceId(R.styleable.Switch_switchTextAppearance, 0);
if (appearance != 0) {
setSwitchTextAppearance(context, appearance);
}
a.recycle();
ViewConfiguration config = ViewConfiguration.get(context);
mTouchSlop = config.getScaledTouchSlop();
mMinFlingVelocity = config.getScaledMinimumFlingVelocity();
// Refresh display with current params
refreshDrawableState();
setChecked(isChecked());
}
private boolean checkTrackOffOnDrawable() {
return mTrackOnDrawable != null && mTrackOffDrawable != null;
}
@SuppressLint("NewApi")
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mOnLayout == null) {
mOnLayout = makeLayout(mTextOn);
}
if (mOffLayout == null) {
mOffLayout = makeLayout(mTextOff);
}
mTrackDrawable.getPadding(mTempRect);
final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth());
final int switchWidth = Math.max(mSwitchMinWidth,
maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right);
// final int switchHeight = mTrackDrawable.getIntrinsicHeight();
int switchHeight;
if (mSwitchMinHeight <= 0) {
switchHeight = mTrackDrawable.getIntrinsicHeight();
} else {
switchHeight = Math.max(mSwitchMinHeight, mTempRect.top + mTempRect.bottom);
}
mThumbWidth = maxTextWidth + mThumbTextPadding * 2;
mSwitchWidth = switchWidth;
mSwitchHeight = switchHeight;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredHeight = getMeasuredHeight();
if (measuredHeight < switchHeight) {
if (Build.VERSION.SDK_INT >= 11) {
setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);
} else {
setMeasuredDimension(getMeasuredWidth(), switchHeight);
}
}
}
@Override
public void setChecked(boolean checked) {
if (checkTrackOffOnDrawable()) {
mTrackDrawable = checked ? mTrackOnDrawable : mTrackOffDrawable;
refreshDrawableState();
}
super.setChecked(checked);
mThumbPosition = checked ? getThumbScrollRange() : 0;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw the switch
int switchLeft = mSwitchLeft;
int switchTop = mSwitchTop;
int switchRight = mSwitchRight;
int switchBottom = mSwitchBottom;
if (checkTrackOffOnDrawable()) {
mTrackDrawable = getTargetCheckedState() ? mTrackOnDrawable : mTrackOffDrawable;
refreshDrawableState();
}
mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);
mTrackDrawable.draw(canvas);
canvas.save()
mTrackDrawable.getPadding(mTempRect);
int switchInnerLeft = switchLeft + mTempRect.left;
int switchInnerTop = switchTop + mTempRect.top;
int switchInnerRight = switchRight - mTempRect.right;
int switchInnerBottom = switchBottom - mTempRect.bottom;
canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);
mThumbDrawable.getPadding(mTempRect);
final int thumbPos = (int) (mThumbPosition + 0.5f);
int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;
mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
mThumbDrawable.draw(canvas);
// mTextColors should not be null, but just in case
if (mTextColors != null) {
mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(),
mTextColors.getDefaultColor()));
}
mTextPaint.drawableState = getDrawableState();
Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
if (switchText != null) {
canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,
(switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);
switchText.draw(canvas);
}
canvas.restore();
}
}
下面是关键属性声明:
<declare-styleable name="Switch">
<!-- Drawable to use when the switch is in the checked/"on" state. -->
<attr name="trackOn" format="reference" />
<!-- Drawable to use when the switch is in the unchecked/"off" state. -->
<attr name="trackOff" format="reference" />
<!-- Minimum height for the switch component -->
<attr name="switchMinHeight" format="dimension" />
<!-- Drawable to use as the "thumb" that switches back and forth. -->
<attr name="thumb" format="reference" />
<!-- Drawable to use as the "track" that the switch thumb slides within. -->
<attr name="track" format="reference" />
<!-- Text to use when the switch is in the checked/"on" state. -->
<attr name="textOn" format="string" />
<!-- Text to use when the switch is in the unchecked/"off" state. -->
<attr name="textOff" format="string" />
<!-- Amount of padding on either side of text within the switch thumb. -->
<attr name="thumbTextPadding" format="dimension" />
<!-- TextAppearance style for text displayed on the switch thumb. -->
<attr name="switchTextAppearance" format="reference" />
<!-- Minimum width for the switch component -->
<attr name="switchMinWidth" format="dimension" />
<!-- Minimum space between the switch and caption text -->
<attr name="switchPadding" format="dimension" />
</declare-styleable>
5. android 自定义控件 属性怎么用
自定义属性设置
public class lei extends RelativeLayout {
private TextView tv1
public lei(Context context) {
super(context);
}
public lei(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.item,this);
tv1 = findViewById(R.id.tv1);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.lei);
String str1 = (String) array.getText(R.styleable.lei_settitle);
int str2 = array.getColor(R.styleable.lei_setbackgroudcolor,Color.BLACK);
tv1.setText(str1);
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "toast", Toast.LENGTH_SHORT).show();
}
});
array.recycle();
}
}
布局控件
<com.example.administrator.myapplication.lei
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
cs:setbackgroudcolor="#158616"
cs:settitle="这个是标题"
cs:settitletextcolor="#FFFFFF"
cs:settextrcolor="#FFFFFF"
>
</com.example.administrator.myapplication.lei>
属性设置
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="lei" >
<attr name="setbackgroudcolor" format="color|reference"/>
<attr name="settitle" format="string|reference"/>
<attr name="settextrcolor" format="color"/>
<attr name="settitletextcolor" format="color"/>
</declare-styleable>
</resources>
6. 有哪些android开发技巧
1、android:clipToPadding
意思是控件的绘制区域是否在padding里面。默认为true。如果你设置了此属性值为false,就能实现一个在布局上事半功陪的效果。先看一个效果图。
上图中的ListView顶部默认有一个间距,向上滑动后,间距消失,如下图所示。
如果使用margin或padding,都不能实现这个效果。加一个headerView又显得大材小用,而且过于麻烦。此处的clipToPadding配合paddingTop效果就刚刚好。
同样,还有另外一个属性也很神奇:android:clipChildren,具体请参考:【Android】神奇的android:clipChildren属性
2、match_parent和wrap_content
按理说这两个属性一目了然,一个是填充布局空间适应父控件,一个是适应自身内容大小。但如果在列表如ListView中,用错了问题就大了。ListView中的getView方法需要计算列表条目,那就必然需要确定ListView的高度,onMesure才能做测量。如果指定了wrap_content,就等于告诉系统,如果我有一万个条目,你都帮我计算显示出来,然后系统按照你的要求就new了一万个对象出来。那你不悲剧了?先看一个图。
假设现在ListView有8条数据,match_parent需要new出7个对象,而wrap_content则需要8个。这里涉及到View的重用,就不多探讨了。所以这两个属性的设置将决定getView的调用次数。
由此再延伸出另外一个问题:getView被多次调用。
什么叫多次调用?比如position=0它可能调用了几次。看似很诡异吧。GridView和ListView都有可能出现,说不定这个祸首就是wrap_content。说到底是View的布局出现了问题。如果嵌套的View过于复杂,解决方案可以是通过代码测量列表所需要的高度,或者在getView中使用一个小技巧:parent.getChildCount == position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (parent.getChildCount() == position) {
// does things here
}
return convertView;
}
3、IllegalArgumentException: pointerIndex out of range
出现这个Bug的场景还是很无语的。一开始我用ViewPager + PhotoView(一个开源控件)显示图片,在多点触控放大缩小时就出现了这个问题。一开始我怀疑是PhotoView的bug,找了半天无果。要命的是不知如何try,老是crash。后来才知道是android遗留下来的bug,源码里没对pointer index做检查。改源码重新编译不太可能吧。明知有exception,又不能从根本上解决,如果不让它crash,那就只能try-catch了。解决办法是:自定义一个ViewPager并继承ViewPager。请看以下代码:
/**
* 自定义封装android.support.v4.view.ViewPager,重写onInterceptTouchEvent事件,捕获系统级别异常
*/
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context context) {
this(context, null);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
LogUtil.e(e);
} catch ( e) {
LogUtil.e(e);
}
return false;
}
}
把用到ViewPager的布局文件,替换成CustomViewPager就OK了。
4、ListView中item点击事件无响应
listView的Item点击事件突然无响应,问题一般是在listView中加入了button、checkbox等控件后出现的。这个问题是聚焦冲突造成的。在android里面,点击屏幕之后,点击事件会根据你的布局来进行分配的,当你的listView里面增加了button之后,点击事件第一优先分配给你listView里面的button。所以你的点击Item就失效了,这个时候你就要根据你的需求,是给你的item的最外层layout设置点击事件,还是给你的某个布局元素添加点击事件了。
解决办法:在ListView的根控件中设置(若根控件是LinearLayout, 则在LinearLayout中加入以下属性设置)descendantFocusability属性。
android:descendantFocusability="blocksDescendants"
官方文档也是这样说明。
5、getSupportFragmentManager()和getChildFragmentManager()
有一个需求,Fragment需要嵌套3个Fragment。基本上可以想到用ViewPager实现。开始代码是这样写的:
mViewPager.setAdapter(new CustomizeFragmentPagerAdapter(getActivity().getSupportFragmentManager(), subFragmentList));
导致的问题是嵌套的Fragment有时会莫名其妙不显示。开始根本不知道问题出现在哪,当你不知道问题的原因时,去解决这个问题显然比较麻烦。经过一次又一次的寻寻觅觅,终于在stackoverflow上看到了同样的提问。说是用getChildFragmentManager()就可以了。真是这么神奇!
mViewPager.setAdapter(new CustomizeFragmentPagerAdapter(getChildFragmentManager, subFragmentList));
让我们看一下这两个有什么区别。首先是getSupportFragmentManager(或者getFragmentManager)的说明:
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
然后是getChildFragmentManager:
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments. The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity.
已经说得比较明白了。
6、ScrollView嵌套ListView
这样的设计是不是很奇怪?两个同样会滚动的View居然放到了一起,而且还是嵌套的关系。曾经有一个这样的需求:界面一共有4个区域部分,分别是公司基本信息(logo、名称、法人、地址)、公司简介、公司荣誉、公司口碑列表。每部分内容都需要根据内容自适应高度,不能写死。鄙人首先想到的也是外部用一个ScrollView包围起来。然后把这4部分分别用4个自定义控件封装起来。基本信息和公司简介比较简单,荣誉需要用到RecyclerView和TextView的组合,RecyclerView(当然,用GridView也可以,3列多行的显示)存放荣誉图片,TextView显示荣誉名称。最后一部分口碑列表当然是ListView了。这时候,问题就出来了。需要解决ListView放到ScrollView中的滑动问题和RecyclerView的显示问题(如果RecyclerView的高度没法计算,你是看不到内容的)。
当然,网上已经有类似的提问和解决方案了。
给一个网址:
四种方案解决ScrollView嵌套ListView问题
ListView的情况还比较好解决,优雅的做法无非写一个类继承ListView,然后重写onMeasure方法。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
ListView可以重写onMeasure解决,RecyclerView重写这个方法是行不通的。
说到底其实计算高度嘛。有两种方式,一种是动态计算RecycleView,然后设置setLayoutParams;另外一种跟ListView的解决方式类似,定义一个类继承LinearLayoutManager或GridLayoutManager(注意:可不是继承RecyclerView),重写onMeasure方法(此方法比较麻烦,此处不表,下次写一篇文章再作介绍)。
动态计算高度如下:
int heightPx = DensityUtil.dip2px(getActivity(), (imageHeight + imageRowHeight) * lines);
MarginLayoutParams mParams = new MarginLayoutParams(LayoutParams.MATCH_PARENT, heightPx);
mParams.setMargins(0, 0, 0, 0);
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(mParams);
honorImageRecyclerView.setLayoutParams(lParams);
思路是这样的:服务端返回荣誉图片后,由于是3列显示的方式,只需要计算需要显示几行,然后给定行间距和图片的高度,再设置setLayoutParams就行了。
int lines = (int) Math.ceil(totalImages / 3d);
至此,这个奇怪的需求得到了解决。
可是在滑动的时候,感觉出现卡顿的现象。聪明的你肯定想到是滑动冲突了。应该是ScrollView的滑动干扰到了ListView的滑动。怎么办呢?能不能禁掉ScrollView的滑动?
网络一下,你肯定能搜索到答案的。先上代码:
/**
* @author Leo
*
* Created in 2015-9-12
* 拦截ScrollView滑动事件
*/
public class CustomScrollView extends ScrollView {
private int downY;
private int touchSlop;
public CustomScrollView(Context context) {
this(context, null);
}
public CustomScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY - downY) > touchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}
}
只要理解了getScaledTouchSlop()这个方法就好办了。这个方法的注释是:Distance in pixels a touch can wander before we think the user is scrolling。说这是一个距离,表示滑动的时候,手的移动要大于这个距离才开始移动控件,如果小于此距离就不触发移动。
看似很完美了。
但是还有另外一个问题:我每次加载这个界面花的时间太长了,每次由其它界面启动这个界面时,都要卡上1~2秒,而且因手机性能时间不等。并不是由于网络请求,取数据由子线程做,跟UI线程毫无关系。这样的体验自己看了都很不爽。
几天过去了,还是那样。马上要给老板演示了。这样的体验要被骂十次呀。
难道跟ScrollView的嵌套有关?
好吧,那我重构代码。不用ScrollView了。直接用一个ListView,然后add一个headerView存放其它内容。因为控件封装得还算好,没改多少布局就OK了,一运行,流畅顺滑,一切迎刃而解!
本来就是这么简单的问题,为什么非得用ScrollView嵌套呢?
stackoverflow早就告诉你了,不要这样嵌套!不要这样嵌套!不要这样嵌套!重要的事情说三遍。
ListView inside ScrollView is not scrolling on Android
当然,从android 5.0 Lollipop开始提供了一种新的API支持嵌入滑动,此时,让像这样的需求也能很好实现。
此处给一个网址,大家有兴趣自行了解,此处不再讨论。
Android NestedScrolling 实战
7、EmojiconTextView的setText(null)
这是开源表情库com.rockerhieu.emojicon中的TextView加强版。相信很多人用到过这个开源工具包。TextView用setText(null)完全没问题。但EmojiconTextView setText(null)后就悲剧了,直接crash,显示的是null pointer。开始我怀疑时这个view没初始化,但并不是。那就调试一下呗。
@Override
public void setText(CharSequence text, BufferType type) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize);
super.setText(builder, type);
}
EmojiconTextView中的setText看来没什么问题。点SpannableStringBuilder进去看看,源码原来是这样的:
/**
* Create a new SpannableStringBuilder containing a of the
* specified text, including its spans if any.
*/
public SpannableStringBuilder(CharSequence text) {
this(text, 0, text.length());
}
好吧。问题已经找到了,text.length(),不空指针才怪。
text = text == null ? "" : text;
SpannableStringBuilder builder = new SpannableStringBuilder(text);
加一行判断就行了。
先想到这么多,以后再补充。
7. Android8.1系统修改串口节点
应客户需求,需要将Android系统里面的串口4(/dev/ttyS4)节点名称修改为/dev/ttyS9。
由于设备节点都是在驱动里面注册生成的于是查看串口驱动。根据log信息找到了系统串口驱动\kernel\drivers\tty\serial\8250\8250_dw.c中的注册函数dw8250_probe。该函数中找到serial8250_register_8250_port,这个是注册设备节点的函数。然后查看serial8250_register_8250_port函数定义(kernel\drivers\tty\serial\老山租8250\8250_core.c),找到了后续注册节点的函数uart_add_one_port,继续查看该函数(位于kernel\drivers\tty\serial\serial_core.c)定义,该函数又是调用tty_port_register_device_attr进行注册的,继续跟踪该函数(侍兆kernel\drivers\tty\tty_port.c),该函数又是调用了tty_register_device_attr(位于kernel\drivers\tty\tty_io.c)进行注册。添加log分析得知串口节点唯虚的名字获取是由tty_line_name产生的。于是查看tty_line_name的实现:
static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
{
if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
return sprintf(p, "%s", driver->name);
else
return sprintf(p, "%s%d", driver->name,
index + driver->name_base);
}
}
通过打印得知串口节点设备名称是由驱动名称 driver->name和串口序号index 组成的,于是添加判断 driver->name是否为"ttyS",index是否为4,如果是就替换。
8. Android自定义属性TypedArray详解
大家好,我是程序员双木L,后续会发专题类的文章,这是自定义控件的第一篇,之后也会陆续更新相关的文章,欢迎关注。
自定义属性在自定义控件过程中属于比较常见的操作,我们可以回想一下这样的场景:自定义view的过程中,我们需要在不同的情况下设置不同的文字大小,那么我们是不是就需要提供对外的方法来设置,这样就比较灵活操作。而我们自定义对外的方法,就是我们自定义的属性啦,那我们来分析一下其原理及作用。
下面我们根据例子来进行分析:
1、首先我们需要在res->values目录下新建attrs.xml文件,该文件就是用来声明属性名及其接受的数据格式的,如下:
attr名词解析:
name表示属性名,上面的属性名是我自己定义的。
format表示接受的输入格式,format格式集合如下:
2、自定义属性的使用,这里我们使用两种方式进行对比解析
最最最原始的使用方式
(1)、自定义文件如下:
我们可以在TestAttrsView方法的参数AttributeSet是个xml解析工具类,帮助我们从布局的xml里提取属性名和属性值。
(2)、在布局文件xml中的使用
这里使用自定义属性需要声明xml的命名空间,其中app是命名空间,用来加在自定义属性前面。
xmlns:app=" http://schemas.android.com/apk/res-auto "
声明xml命名空间,xmlns意思为“xml namespace”.冒号后面是给这个引用起的别名。
schemas是xml文档的两种约束文件其中的一种,规定了xml中有哪些元素(标签)、
元素有哪些属性及各元素的关系,当然从面向对象的角度理解schemas文件可以
认为它是被约束的xml文档的“类”或称为“模板”。
(3)、将属性名与属性值打印结果如下:
从打印结果我们可以看出,AttributeSet将布局文件xml下的属性全部打印出来了,细心的童鞋可能已经看出来:
这个属性我们设置的是一个整数尺寸,可最后打印出来的是资源编号。
那如果我们想要输出我们设置的整数尺寸,需要怎么操作呢?
这个时候就该我们这篇的主角出场了,使用TypedArray方式。
(1)、这里我们需要将attrs.xml使用“declare-styleable”标签进行改造,如下:
从改造后的attrs文件可以看出,我们将属性声明归结到TestStyleable里面,也就意味着这些属性是属于TestStyleable下的。
(2)、属性的解析:
这里我直接打印出解析结果,这里可以获取我们想要的自定义属性,而系统有的属性可以忽略。
(3)、运行结果如下
从解析的结果可以看出,尺寸的结果已经转换为实际值了:
这个时候有童鞋又问了,我设置的是15dp,为啥最后打印是41.25了呢?其实解析出来的值单位是px,所以这里输出的是转换后的值。
解析的过程中用到了这个方法:
我们来看一下这个方法的源码:
源码中我们可以看到这个方法有两个参数:
obtainStyledAttributes方法返回值类型为TypedArray。该类型记录了获取到的属性值集合,而通过数组下标索引即可找到对应的属性值。索引下标通过R.styleable.TestStyleable_xx获取,"xx"表示属性名,一般命名为"styleable名" + "_" + "属性名"。
而TypedArray提供了各种Api,如getInteger,getString,getDimension等方法来获取属性值,这些方法都需要传入对应属性名在obtainStyledAttributes中的int数组的位置索引,通过下标获取数组里属性值。
这个TypedArray的作用就是资源的映射作用,把自定义属性在xml设置值映射到class,这样怎么获取都很简单啦。
到这里就分析完啦!