我正在尝试为圆形imageView制作3个圆形的边界,首先我尝试获取imageView的坐标来绘制圆,以使其中心与我的imageView的中心完全匹配。 但是我同时使用getGlobalVisibleRect和getLocationOnScreen获得的坐标似乎令人困惑。请查看屏幕截图
最后是代码:
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/img_profile"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerInParent="true"
android:src="@drawable/profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
渐变依赖项
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.mikhaellopez:circularimageview:3.2.0'
}
MainActivity onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
img_profile.post(Runnable {
// var coords = IntArray(4)
// img_profile.getLocationOnScreen(coords)
// for (location in coords){
// Log.d(TAG, "location - " + location.toString())
// }
//
// var left =coords[0].toFloat()
// var top = coords[1].toFloat()
var rectangle: Rect = Rect()
img_profile.getGlobalVisibleRect(rectangle)
var left = rectangle.left.toFloat()
var top = rectangle.top.toFloat()
var width = rectangle.width().toFloat()
var height = rectangle.height().toFloat()
var right = rectangle.right.toFloat()
var bottom = rectangle.bottom.toFloat()
Log.d(TAG, "left - " + rectangle.left.toString())
Log.d(TAG, "top - " + rectangle.top.toString())
Log.d(TAG, "width - " + rectangle.width().toString())
Log.d(TAG, "height - " + rectangle.height().toString())
var background = CirclesBackground(this,
left,
top, right, bottom)
root_view.addView(background)
})
}
自定义视图
class CirclesBackground(context: Context?, left: Float, top: Float, width: Float, height: Float) : View(context) {
private var coordX : Float = left
private var coordY : Float = top
private var width : Float = width
private var height : Float = height
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
var paint = Paint()
paint.style = Paint.Style.STROKE
paint.strokeWidth = 5f
paint.color = ContextCompat.getColor(context, R.color.orange)
canvas?.drawRect(coordX, coordY, width, height, paint)
// canvas?.drawLine(coordX, coordY, width, height, paint)
paint.color = ContextCompat.getColor(context, R.color.pink)
// canvas?.drawLine(coordX, coordY, coordX + width, coordY, paint)
// canvas?.drawCircle(coordX, coordY, radius, paint)
}
}
我在这里做错了什么或想念什么?请指导。