PapawView
: 可灵活定义凸起位置、凸起形状的消息背景View
使用:
Step 1. Add it in your root ./build.gradle
at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.jiarWang:PapawView:1.0.0'
}
PapawTextView
继承自TextView
;
通过使用PapawTextView
的额外属性来定义羊角的位置、高度以及背景色
属性 | 类型 | 说明 |
---|---|---|
radius | dimension | 四个边角的圆弧半径 |
horn_side | enum | ^ 羊角所在边的位置 (top ¦ right ¦ bottom ¦ left) |
color | color | 背景颜色 |
horn_alpha | float | 背景透明度 |
horn_height | dimension | ^羊角高度 |
horn_start | dimension | 羊角起始点距离所在边的起始点的相对距离 |
horn_center | dimension | 羊角中点距离所在边的起始点的相对距离 |
horn_end | dimension | 羊角结束点距离所在边的起始点的相对距离 |
提示:羊角的空间需要使用
padding
手动腾出来即: 羊角所在的边的
padding
需要比对边的padding
值大一个羊角高度,即使对边没有padding
,例如: 羊角所在边为top
,则需要满足:paddingTop - paddingBottom = horn_height
,即使paddingBottom = 0
PapawLayout
继承自CoordinatorLayout
,这样便可以在Papaw
图形上做更多的事情。
PapawLayout
的额外属性的使用同PapawTextView
一样。
- Samele 如下: paddingTop - paddingBottom = horn_height
<com.jiarwang.papaw.PapawLayout 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="100dp" android:layout_gravity="center" android:paddingTop="50dp" android:paddingBottom="30dp" app:horn_alpha="0.71" app:horn_center="30dp" app:horn_end="50dp" app:horn_height="20dp" app:horn_side="top" app:horn_start="20dp" app:radius="12dp" tools:context=".Main2Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="alpha = 0.71" android:textSize="20sp" /> </com.jiarwang.papaw.PapawLayout>
如果PapawLayout
和PapawTextView
不能满足你的需求,可是使用PapawHelper
来直接帮助你完成这种带羊角背景的自定义View
(或ViewGroup
);
- setPaint(Paint paint) : 设置绘制背景的画笔
- setAlpha(float alpha) :设置背景的透明度(0~1)
- setHornHeight(float hornHeight) : 设置羊角高度
- setRadius(float radius) : 设置矩形边角半径
- setHornPoint(@Side int hornSide, float start, float center, float end) : 设置羊角所在边、羊角起始位置、中间点位置、结束位置。
使用PapawHelper
让自定义View
带上羊角,只需要两行代码:
- 为自定义的
View
添加一个PapawHelper
类型成员变量mPapawHelper
;
private PapawHelper mPapawHelper = new PapawHelper(this)
. setHornHeight(5)//设置羊角高度
.setHornPoint(PapawHelper.TOP, 5, 15, 20);
- 重写
onDraw
方法,并在super.onDraw(canvas)
前,调用mPapawHelper.drawBackground(canvas);
//CustomView.java
@Override
public void onDraw(Canvas canvas) {
mPapawHelper.drawBackground(canvas);
super.onDraw(canvas);
}
- 同样可以通过继承
PapawHelper
来为你的PapawView
添加更多功能。lineHorn(final Path path, @Side int side, PointF startPoint, PointF endPoint)
里可以重定义自己的羊角画法onDrawPath(Canvas canvas, Paint paint, Path path)
里可以重新定义背景的绘制
Copyright 2014 wangjian
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.