简介
与继承系统控件的自定义View不同,继承View的自定义View实现起来要稍微复杂一些。其不只是要实现onDraw()方法,而且在实现过程中还要考虑到padding 属性的设置;为了方便配置自己的自定义 View,还会对外提供自定义的属性。另外,如果要改变触控的逻辑,还要重写 onTouchEvent()等触控事件的方法。
简单的例子
下面的自定义View画了一个红色矩形。
public class MyView extends View {
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getHeight();
int width = getWidth();
Paint paint = new Paint();
paint.setStrokeWidth(4f);
paint.setColor(Color.RED);
canvas.drawRect(0, 0, height, width, paint);
}
}
布局中使用:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.echo.myview.MyView
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
效果图:
处理Padding属性
在当前的自定义View中,定义Padding属性是没有任何效果的,因此我们需要自行对Padding属性进行处理。
可以在onDraw方法中通过以下方法获得设置的Padding,进行处理:
getPaddingTop()
:获得设置的Padding top的值。getPaddingLeft()
:获得设置的PaddingLeft的值。getPaddingRight()
:获得设置的Padding Right的值。getPaddingBottom()
:获得设置的Padding Bottom的值。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getHeight();
int width = getWidth();
Paint paint = new Paint();
paint.setStrokeWidth(4f);
paint.setColor(Color.RED);
int topPadding = getPaddingTop();
int leftPadding = getPaddingLeft();
int rightPadding = getPaddingRight();
int bottomPadding = getPaddingBottom();
canvas.drawRect(0 + leftPadding, 0 + topPadding, height - bottomPadding, width - rightPadding, paint);
}
自定义属性
首先在values目录下创建 attrs.xml。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyAttrs">
<attr name="rect_color" format="color"></attr>
</declare-styleable>
</resources>
在这个配置文件定义了名为RectView的自定义属性组合。我们定义了rect_color属性,它的格式为color。 接下来在RectView的构造方法中解析自定义属性的值。
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyAttrs); //这里的R.styleable.MyAttrs的MyAttrs就是配置文件中name="MyAttrs"定义的。
mColor = typedArray.getColor(R.styleable.MyAttrs_rect_color, Color.RED); //这里取到我们定义的rect_color值(需要加上前缀MyAttrs_),第二个参数为默认值。
typedArray.recycle(); //最后要回收资源。
}
最后在布局中使用,需要添加schemas:xmlns:MyAttr=“http://schemas.android.com/apk/res-auto”,其中 MyAttr是 我们自定义的名字。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.echo.myview.MyView
xmlns:MyAttr="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
MyAttr:rect_color = "@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>