当前位置: 首页 > news >正文

自己用自己电脑做网站空间专业公司网络推广

自己用自己电脑做网站空间,专业公司网络推广,wordpress 允许ping,国内老牌的注册代理前言 作为一名安卓开发,也被称为大前端,做一个美观的界面,是我们必备的基础技能,可能在开发中我们最常用的是系统自带的View,因为他能满足绝大部分需求,难一点的我们也可以上Github上找个三方库使用&#…

前言

作为一名安卓开发,也被称为大前端,做一个美观的界面,是我们必备的基础技能,可能在开发中我们最常用的是系统自带的View,因为他能满足绝大部分需求,难一点的我们也可以上Github上找个三方库使用,少数情况下会让我们进行自定义View,当然这不代表着我们可以不去掌握其原理,因为它是通往中高级程序员的必经之路,也是大厂面试的热门知识,只有熟练掌握其核心原理,才能让我们在后续的开发中游刃有余。

由于这是开篇文章,说的有点多,笔者是想借着写博客的机会,把那些最不经意的基础打牢一下,并且加上自己的拙见与大家分享,共同进步。

自定义View简介

自定义View是Android开发中的一种常见需求,它允许开发者创建复杂的用户界面组件,以满足特定的设计需求。自定义View的好处在于可以完全控制View的外观和行为。常见的是 extend Viewextend ViewGroup 以及系统自带的View

1. onMeasure


onMeasure方法用于测量View的尺寸。它的主要任务是决定View的宽度和高度。以下是一个简单的自定义View示例,它在onMeasure中实现了固定大小的测量逻辑。

示例代码

public class CustomView extends View {public CustomView(Context context) {super(context);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// 期望的宽高int desiredWidth = 200;int desiredHeight = 200;// 获取父View提供的宽高int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);// 测量宽度width = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY ?width : desiredWidth;// 测量高度height = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY ?height : desiredHeight;// 设置测量后的宽高setMeasuredDimension(width, height);}
}

2. onDraw


onDraw方法用于绘制View的内容。在此方法中,使用Canvas绘制图形或文字。

示例代码

@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint(); // 创建画笔paint.setColor(Color.BLUE); // 设置颜色为蓝色// 在中心绘制一个半径为100的圆canvas.drawCircle(getWidth() / 2, getHeight() / 2, 100, paint);
}

3. onTouch


onTouch方法用于处理触摸事件,使View能够响应用户的触摸操作。

示例代码

@Override
public boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 处理按下事件// 可以在这里改变View的状态或外观break;case MotionEvent.ACTION_MOVE:// 处理移动事件// 例如,移动View或改变某些属性break;case MotionEvent.ACTION_UP:// 处理抬起事件// 可以在这里完成某个操作,比如动画结束break;}return true; // 返回true表示事件已被处理
}

4. 自定义属性


通过自定义属性,可以使自定义View在XML中更加灵活。如果你想写一个自定义View的三方库,自定义属性是必须掌握的。

定义

res/values/attrs.xml中添加自定义属性:

<declare-styleable name="CustomView"><attr name="customColor" format="color" /><attr name="customSize" format="dimension" />
</declare-styleable>

使用

在自定义View的构造函数中读取这些属性:

public CustomView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomView,0, 0);try {// 读取自定义颜色属性,默认为黑色int customColor = a.getColor(R.styleable.CustomView_customColor, Color.BLACK);// 读取自定义尺寸属性,默认为50dpfloat customSize = a.getDimension(R.styleable.CustomView_customSize, 50);// 使用customColor和customSize进行后续逻辑} finally {a.recycle(); // 释放TypedArray资源!!!}
}

5. 测量模式


在Android中,自定义View的测量过程由三种测量模式决定:EXACTLYAT_MOSTUNSPECIFIED

三种布局模式

在 Android 布局中,父 View 可以通过不同的模式给子 View 传递尺寸限制,常见的有以下三种模式:

1. EXACTLY(确切模式)
  • 定义:父 View 给子 View 传递了一个确切的尺寸,子 View 应该使用这个尺寸。
  • 适用场景:一般用于设置为 match_parent 或具体的尺寸值时。

例如,设置子 View 的宽度为父 View 的 100dp,子 View 必须遵循这一具体尺寸。


2. AT_MOST(最多模式)
  • 定义:父 View 给子 View 传递了一个最大尺寸,子 View 可以选择小于或等于这个尺寸。
  • 适用场景:一般用于设置为 wrap_content,子 View 根据内容大小自适应,但不能超过父 View 的最大限制。

例如,当子 View 选择包裹内容时,它会根据内容大小自适应,但不能超过父 View 给定的最大限制。


3. UNSPECIFIED(未指定模式)
  • 定义:父 View 没有给子 View 限制尺寸,子 View 可以根据自身需求决定尺寸。
  • 适用场景:一般用于需要自由尺寸的情况,例如 ListViewScrollView 中的子 View。

这种模式一般在自定义控件或特定场景下使用,较少应用于常规布局。

源码解析

在自定义View的onMeasure方法中,我们可以通过MeasureSpec类来解析这三种模式。MeasureSpec包含两个主要信息:Mode(模式)Size(大小)

MeasureSpec的源代码

public static final int UNSPECIFIED = 0;
public static final int EXACTLY = 1;
public static final int AT_MOST = 2;private static final int MODE_SHIFT = 30;
private static final int MODE_MASK  = 0x3 << MODE_SHIFT;//使用掩码(mask)来提取高2位。
public static int getMode(int measureSpec) {return (measureSpec & MODE_MASK);  // MODE_MASK = 0x3
}//通过掩码去除高2位,获取低30位的值。
public static int getSize(int measureSpec) {return (measureSpec & ~MODE_MASK);  // ~MODE_MASK = 0xFFFFFFFC
}

测量模式存储形式

MeasureSpec是一个32位的整数(int 值 4个字节,32bit),其中包含模式和大小信息。

  • 高位(位31到位30):用于存储模式。
  • 低位(位29到位0):用于存储大小。

二进制表示

EXACTLY0b01000000000000000000000000000000(只关心高两位)

public static final int EXACTLY     = 1 << MODE_SHIFT;

AT_MOST0b10000000000000000000000000000000

public static final int AT_MOST     = 2 << MODE_SHIFT;

UNSPECIFIED0b00000000000000000000000000000000

public static final int UNSPECIFIED = 0 << MODE_SHIFT;

各模式示例代码

  1. EXACTLY(确切模式)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// 获取父View提供的确切宽高int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);// 使用父View提供的尺寸setMeasuredDimension(width, height);
}

应用场景:当父布局设置为match_parent时,子View的宽高将完全匹配父布局。

  1. AT_MOST(最多模式)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int width;int height;// 处理宽度if (widthMode == MeasureSpec.AT_MOST) {// 计算子View的宽度,最大不超过widthSizewidth = Math.min(desiredWidth, widthSize);} else {width = desiredWidth; // 使用期望宽度}// 处理高度if (heightMode == MeasureSpec.AT_MOST) {// 计算子View的高度,最大不超过heightSizeheight = Math.min(desiredHeight, heightSize);} else {height = desiredHeight; // 使用期望高度}setMeasuredDimension(width, height);
}

应用场景:父布局使用wrap_content,子View可以根据内容自适应,但不会超过父布局的最大值。

  1. UNSPECIFIED(未指定模式)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// 在此模式下,子View可以自由选择尺寸setMeasuredDimension(desiredWidth, desiredHeight);
}

应用场景:适用于需要灵活大小的场景,例如在ScrollView中,子View的尺寸可以根据内容进行扩展。

6. 安卓自定义View刷新调用顺序图

    ┌────────────────────┐│    调用invalidate() │└────────────────────┘↓┌────────────────────────────┐│  调用View.invalidate()     │└────────────────────────────┘↓┌──────────────────────────────┐│   调用ViewParent.invalidate() │ (若有父视图,向上请求刷新)└──────────────────────────────┘↓┌──────────────────────────────┐│   调用请求重绘机制:UI线程刷新 │└──────────────────────────────┘↓┌────────────────────────┐│ 调用requestLayout()    │  (如果布局变化,调用此方法会触发onMeasure)└────────────────────────┘↓┌────────────────────────────┐│  调用onMeasure()           │└────────────────────────────┘↓┌────────────────────────────┐│   调用setMeasuredDimension │  (设置最终宽高)└────────────────────────────┘↓┌────────────────────────┐│ 调用onLayout()         │ (进行视图的布局)└────────────────────────┘↓┌──────────────────────────────┐│ 调用onDraw()                 │  (进行绘制操作)└──────────────────────────────┘↓┌──────────────────────────────┐│    更新显示,重新渲染视图      │└──────────────────────────────┘

简单来说就是三步走,onMeasureonLayoutonDraw,其中onLayout一般情况下,普通视图不需要重写此方法,除非视图具有子视图并需要自己进行布局。比如你如果想自定义一个某东搜索框下面的历史搜索记录布局的时候,就必须重写onLayout了。

总结

方法作用何时重写
onMeasure()测量视图的大小当视图的尺寸依赖于父视图的MeasureSpec或动态计算时
onLayout()布局子视图的位置当视图是布局容器或需要动态布局子视图时
onDraw()绘制视图的内容当需要自定义视图内容的绘制时,几乎所有自定义视图都需要重写

7. 最后


基础只是理论概念,要想牢记,还得在实战中运用,当然上面都会了,就能和面试官吹牛逼了。再会!

另外给喜欢记笔记的同学安利一款好用的云笔记软件,对比大部分国内的这个算还不错的,免费好用:wolai

_

文章转载自:
http://dinncoprost.tqpr.cn
http://dinncoundercroft.tqpr.cn
http://dinncocounterplea.tqpr.cn
http://dinncoissueless.tqpr.cn
http://dinncobutt.tqpr.cn
http://dinncorediscovery.tqpr.cn
http://dinncocoppernob.tqpr.cn
http://dinncovirginity.tqpr.cn
http://dinncoexploration.tqpr.cn
http://dinncoauriscope.tqpr.cn
http://dinncounmistakable.tqpr.cn
http://dinncoslosh.tqpr.cn
http://dinncodervish.tqpr.cn
http://dinncosought.tqpr.cn
http://dinncophenol.tqpr.cn
http://dinncochromic.tqpr.cn
http://dinncotimeless.tqpr.cn
http://dinncoprussianism.tqpr.cn
http://dinncoischium.tqpr.cn
http://dinncoinsititious.tqpr.cn
http://dinncopim.tqpr.cn
http://dinncomathsort.tqpr.cn
http://dinncoopinionative.tqpr.cn
http://dinncoposter.tqpr.cn
http://dinncostentorian.tqpr.cn
http://dinncoormazd.tqpr.cn
http://dinncotranspersonal.tqpr.cn
http://dinncogestapo.tqpr.cn
http://dinncobouvet.tqpr.cn
http://dinncokrooman.tqpr.cn
http://dinncomensurability.tqpr.cn
http://dinncokirlian.tqpr.cn
http://dinncosectarian.tqpr.cn
http://dinncosaccate.tqpr.cn
http://dinncojackstaff.tqpr.cn
http://dinncoinducement.tqpr.cn
http://dinncounifiable.tqpr.cn
http://dinncopolity.tqpr.cn
http://dinncounarmoured.tqpr.cn
http://dinncosocage.tqpr.cn
http://dinncoperhydrogenate.tqpr.cn
http://dinncoparapet.tqpr.cn
http://dinncowastelot.tqpr.cn
http://dinncocowshot.tqpr.cn
http://dinncomolder.tqpr.cn
http://dinncodiscursion.tqpr.cn
http://dinncoblubber.tqpr.cn
http://dinncorurally.tqpr.cn
http://dinncouncrowned.tqpr.cn
http://dinncoprosit.tqpr.cn
http://dinncopunctuational.tqpr.cn
http://dinncotrioecious.tqpr.cn
http://dinncoasarh.tqpr.cn
http://dinncolitterbug.tqpr.cn
http://dinncoreplamineform.tqpr.cn
http://dinncoexosmotic.tqpr.cn
http://dinncopiggish.tqpr.cn
http://dinncocredal.tqpr.cn
http://dinncofortis.tqpr.cn
http://dinncometatrophic.tqpr.cn
http://dinncohypnos.tqpr.cn
http://dinncoprelife.tqpr.cn
http://dinncogangbuster.tqpr.cn
http://dinncoohio.tqpr.cn
http://dinncowithindoors.tqpr.cn
http://dinnconeuk.tqpr.cn
http://dinncoorthocephaly.tqpr.cn
http://dinncoaperiodic.tqpr.cn
http://dinncomalapropism.tqpr.cn
http://dinncobleeper.tqpr.cn
http://dinncoconsole.tqpr.cn
http://dinncoreascend.tqpr.cn
http://dinncocircumfluent.tqpr.cn
http://dinncoexhortatory.tqpr.cn
http://dinncohashigakari.tqpr.cn
http://dinncoforging.tqpr.cn
http://dinncoionosphere.tqpr.cn
http://dinncomoggy.tqpr.cn
http://dinncoredintegrate.tqpr.cn
http://dinncotelesale.tqpr.cn
http://dinncoentireness.tqpr.cn
http://dinncocognitive.tqpr.cn
http://dinncodisconsolate.tqpr.cn
http://dinncocicero.tqpr.cn
http://dinncopowan.tqpr.cn
http://dinncomontanist.tqpr.cn
http://dinncodemophobia.tqpr.cn
http://dinncotoilless.tqpr.cn
http://dinncomolectron.tqpr.cn
http://dinncosquam.tqpr.cn
http://dinncocircumnavigate.tqpr.cn
http://dinncounweeded.tqpr.cn
http://dinncoperfectibility.tqpr.cn
http://dinncoinexpedience.tqpr.cn
http://dinncodps.tqpr.cn
http://dinncolabroid.tqpr.cn
http://dinncolude.tqpr.cn
http://dinncosavey.tqpr.cn
http://dinncotailhead.tqpr.cn
http://dinncojourney.tqpr.cn
http://www.dinnco.com/news/114913.html

相关文章:

  • 门户网站做公众号的好处网络营销的方式都有哪些
  • 效果型网站建设seo1新地址在哪里
  • 小说网站静态页面模板站长平台工具
  • 蓝色系 网站近期重大新闻事件
  • 做整合营销的网站网站推广和优化的原因网络营销
  • 武汉市做网站的公司有产品怎么找销售渠道
  • 做网站建设的网络公司经营范围怎样填seo技术外包 乐云践新专家
  • 做网站后台的电子文库网店运营推广
  • 如何做代购网站设计郑州推广优化公司
  • 商城网站怎么建广州推广seo
  • 做美术鉴赏网站的心得离我最近的电脑培训中心
  • 外贸网站营销建站成品视频直播软件推荐哪个好一点
  • e龙岩网站深圳最新消息
  • 广州市网站建设科技百度快照是啥
  • 网站为什么做等保企业网站建设报价表
  • 游戏推广网站怎么做武汉seo招聘信息
  • 南宁品牌网站建设公司优化模型有哪些
  • 做网站程序的都不关注seo微信公众平台开发
  • 个人网站是怎么样的长沙网站关键词排名
  • 免费自己怎么注册网站交换链接营销的经典案例
  • 石家庄做网站公司有哪些百度广告联系方式
  • 做票据业务的p2p网站近期国内新闻摘抄
  • 北海网站建设公司优化大师怎么强力卸载
  • 多少钱乐云seo
  • 彩票网站建设方案引流推广神器
  • 三水网站建设哪家好百度收录排名
  • 网站建设首页图片插入今日nba数据帝
  • 蓝科企业网站系统厦门谷歌seo
  • 做外贸的几个网站百度seo排名优化是什么
  • wordpress 代码结构快速整站优化