android 自定义园view,Android中如何优雅地自定义一个View

Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到一些坑,我把自己遇到的一些问题和解决方法总结一下,希望对广大码友们有所帮助。

注意点① 用xml定义Layout时,Root element 最好使用merge

当我们需要继承一个布局比较复杂的ViewGroup(比较多的是LinearLayout、RelativeLayout)时,通常会用xml来写布局,然后在自定义的View类中inflate这个定义了layout的xml文件。

首先新建一个名为 MyLayout 的 class 文件,在 init 方法中解析稍后定义的xml文件。

/** * Created by liangfei on 4/14/15. */

public class MyLayout extends LinearLayout {

public MyLayout(Context context) {

super(context);

init();

}

private void init() {

setOrientation(VERTICAL);

View rootView = inflate(getContext(), R.layout.my_layout, this);

((TextView) rootView.findViewById(R.id.title)).setText("MyLayout");

((TextView) rootView.findViewById(R.id.desc)).setText("A customized layout");

}

}

然后新建一个取名为my_layout的布局文件, 并把 Root element 设置成merge。

用 Android SDK 附带的 Monitor 工具查看一下运行时的布局信息。

0818b9ca8b590ca3270a3433284dd417.png

最顶层是一个FrameLayout,然后是一个LinearLayout,里面有两个TextView,可以看出布局没有冗余。

但是,如果把 Root element 换成 LinearLayout,效果会怎么样呢?

0818b9ca8b590ca3270a3433284dd417.png

很明显,用 LinearLayout 做 Root element 后,布局多了一个层级,成了影响性能的一个因素。

注意点② 重载子类构造函数时要弄清楚父类做了哪些操作

先从我一个惨痛的教训开始,当时我这样自定义了一个Button:

/** * Created by liangfei on 4/14/15. */

public class MyButton extends Button {

public MyButton(Context context) {

this(context, null);

}

public MyButton(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

}

乍一看貌似没什么问题,构造函数的调用方式都是正确的,但是无论我怎么修改 MyButton 的属性,显示方式就是不正确。

其实问题就出在Button类在构造函数中使用了一个defStyleAttr, 而我这种写法会忽略掉这个defStyleAttr - com.android.internal.R.attr.buttonStyle,稍读源码就知道了。

@RemoteView

public class Button extends TextView {

public Button(Context context) {

this(context, null);

}

public Button(Context context, AttributeSet attrs) {

this(context, attrs, com.android.internal.R.attr.buttonStyle);

}

public Button(Context context, AttributeSet attrs, int defStyleAttr) {

this(context, attrs, defStyleAttr, 0);

}

public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

}

后来写代码的时候,我都是看了父类的源码之后才敢这么写,如果不确定就老老实实地写成下面这种形式。

/** * Created by liangfei on 4/14/15. */

public class MyButton extends Button {

public MyButton(Context context) {

super(context);

init();

}

public MyButton(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

}

其实,还有很多其他的坑,比如 Button 的高度,后面抽时间再总结一下,太困了,睡觉。

おやすみなさい

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义一个水波进度 View,你需要完成以下几个步骤: 1. 创建一个自定义 View 类,并在构造函数初始化一些必要的属性,如颜色、线宽等。 2. 重写 onSizeChanged() 方法,在该方法获取 View 的宽度和高度,并计算出进度条的半径、圆心等相关参数。 3. 重写 onDraw() 方法,在该方法绘制水波纹效果。 4. 在自定义 View 添加一个 setProgress() 方法,用于设置进度条的进度。 5. 在布局文件引入自定义 View,设置 layout_width 和 layout_height 属性,并在代码调用 setProgress() 方法设置进度条的进度。 下面是一个简单的自定义水波进度 View 的代码示例: ```java public class WaterWaveProgressView extends View { private Paint mPaint; private int mWidth, mHeight; private float mRadius; private float mProgress; public WaterWaveProgressView(Context context) { super(context); init(); } public WaterWaveProgressView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaterWaveProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(5); mPaint.setStyle(Paint.Style.STROKE); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; mRadius = Math.min(mWidth, mHeight) / 2 * 0.8f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mPaint); float angle = mProgress / 100 * 360; canvas.drawArc(mWidth / 2 - mRadius, mHeight / 2 - mRadius, mWidth / 2 + mRadius, mHeight / 2 + mRadius, -90, angle, false, mPaint); } public void setProgress(float progress) { mProgress = progress; invalidate(); } } ``` 通过调用 setProgress() 方法来更新进度条的进度,如下所示: ```java WaterWaveProgressView progressView = findViewById(R.id.progress_view); progressView.setProgress(50); // 设置进度为 50% ``` 在布局文件引入自定义 View: ```xml <com.example.waterwaveprogressview.WaterWaveProgressView android:id="@+id/progress_view" android:layout_width="150dp" android:layout_height="150dp" /> ``` 这样就能够实现一个简单的水波进度 View 了。如果需要更加复杂的效果,可以在 onDraw() 方法绘制多个水波纹,或者使用 Path 绘制波形等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值